注册

android客服云如何获取机器人欢迎语

1.APP端要想获取到机器人菜单欢迎语,首先在移动客服管理员模式下,智能机器人,机器人设置——自动回复——欢迎语配置菜单欢迎语或者是欢迎语消息。
2、在会话分配规则中,渠道指定一定要指定机器人,APP端才能获取到机器人欢迎语,否则获取不到
具体的代码如下附件
图1中的
MyApplication.tenantId记得替换为自己的tenantId
new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HttpClient httpClient = new DefaultHttpClient();
                        HttpGet httpGet = new HttpGet("http://kefu.easemob.com/v1/Tenants/"+MyApplication.tenantId+"/robots/visitor/greetings/app");
                        try {
                            HttpResponse response = httpClient.execute(httpGet);
                            int code = response.getStatusLine().getStatusCode();
                            if (code == 200) {
                                final String rev  = EntityUtils.toString(response.getEntity());

                                JSONObject obj = new JSONObject(rev);
                                int type = obj.getInt("greetingTextType");
                                final String rob_welcome = obj.getString("greetingText");
                                //type为0代表是文字消息的机器人欢迎语
                                //type为1代表是菜单消息的机器人欢迎语
                                if(type == 0){
                                    //把解析拿到的string保存在本地
                                    shareUtil.saveRobot(rob_welcome);
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(getApplicationContext(),"rob_welcome="+rob_welcome,Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }else if(type == 1){
                                    final String str = rob_welcome.replaceAll(""","\"");
                                    JSONObject json = new JSONObject(str);
                                    JSONObject ext = json.getJSONObject("ext");
                                    final JSONObject msgtype = ext.getJSONObject("msgtype");
                                    //把解析拿到的string保存在本地
                                    shareUtil.saveRobot(msgtype.toString());
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(getApplicationContext(),"rob_welcome="+msgtype,Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }

                            }
                        }catch(final Exception e){
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(),"exception="+e.getMessage(),Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }
                }).start();


图2中的:
//创建消息插入本地
            Message message = Message.createReceiveMessage(Message.Type.TXT);
            //从本地获取保存的string
            String str = shareUtil.getRobot();
            EMTextMessageBody body = null;
            //判断是否是菜单消息的string,这是自己实现的一个方法
            if(!isRobotMenu(str)){
                //文字消息直接去设置给消息
            body = new EMTextMessageBody(str);
            }else{
                //菜单消息需要设置给消息扩展
                try{
                    body = new EMTextMessageBody("");
                    JSONObject msgtype = new JSONObject(str);
                    message.setAttribute("msgtype",msgtype);
                }catch (Exception e){
                    Toast.makeText(this,"exception="+e.getMessage(),Toast.LENGTH_SHORT).show();
                }
}

            message.setFrom(MyApplication.imService);
            message.addBody(body);
            message.setMsgTime(System.currentTimeMillis());
            message.setStatus(Message.Status.SUCCESS);
            message.setMsgId(UUID.randomUUID().toString());
ChatClient.getInstance().chatManager().saveMessage(message);



图3中的:
private boolean isRobotMenu(String str){
        try {
            JSONObject json = new JSONObject(str);
            JSONObject obj = json.getJSONObject("choice");
        }catch (Exception e){
            return false;
        }
        return true;
}
 
 

0 个评论

要回复文章请先登录注册