微信公众号接入图灵机器人实现自动回复消息

1、创建图灵机器人

    进入图灵机器人网址:http://www.tuling123.com/,.......->点击创建机器人

接下来选择或填写机器人的相关属性,这里我选择的是聊天社交,模拟真人聊天的机器人,应用终端由于是微信公众号接入,机器人设置里面,我们可以拿到接口api相关的信息。直接上图:

2、后端代码开始接入

常量类


  1 public final class Constants {
  2 
  3     /**
  4      * GET或POST必须大写,不可更改
  5      */
  6     public static final String GET = "GET";
  7     public static final String POST = "POST";
  8 
  9    /* 微信请求消息类型(由微信方规定,不可更改) */
 10     /**
 11      * 文本
 12      */
 13     public static final String REQ_TEXT_TYPE = "text";
 14     /**
 15      * 事件
 16      */
 17     public static final String REQ_EVENT_TYPE = "event";
 18     /**
 19      * 订阅
 20      */
 21     public static final String REQ_SUBSCRIBE_TYPE = "subscribe";
 22     /**
 23      * 取消订阅
 24      */
 25     public static final String REQ_UNSUBSCRIBE_TYPE = "unsubscribe";
 26 
 27    /* 微信返回消息类型(由微信方规定,不可更改) */
 28     /**
 29      * 文本
 30      */
 31     public static final String RESP_TEXT_TYPE = "text";
 32     /**
 33      * 图文
 34      */
 35     public static final String RESP_NEWS_TYPE = "news";
 36 
 37    /* 图灵机器人返回数据类型状态码(官方固定) */
 38     /**
 39      * 文本
 40      */
 41     public static final Integer TEXT_CODE = 100000;
 42     /**
 43      * 列车
 44      */
 45     public static final Integer TRAIN_CODE = 305000;
 46     /**
 47      * 航班
 48      */
 49     public static final Integer FLIGHT_CODE = 306000;
 50     /**
 51      * 链接类
 52      */
 53     public static final Integer LINK_CODE = 200000;
 54     /**
 55      * 新闻
 56      */
 57     public static final Integer NEWS_CODE = 302000;
 58     /**
 59      * 菜谱、视频、小说
 60      */
 61     public static final Integer MENU_CODE = 308000;
 62     /**
 63      * key的长度错误(32位)
 64      */
 65     public static final Integer LENGTH_WRONG_CODE = 40001;
 66     /**
 67      * 请求内容为空
 68      */
 69     public static final Integer EMPTY_CONTENT_CODE = 40002;
 70     /**
 71      * key错误或帐号未激活
 72      */
 73     public static final Integer KEY_WRONG_CODE = 40003;
 74     /**
 75      * 当天请求次数已用完
 76      */
 77     public static final Integer NUMBER_DONE_CODE = 40004;
 78     /**
 79      * 暂不支持该功能
 80      */
 81     public static final Integer NOT_SUPPORT_CODE = 40005;
 82     /**
 83      * 服务器升级中
 84      */
 85     public static final Integer UPGRADE_CODE = 40006;
 86     /**
 87      * 服务器数据格式异常
 88      */
 89     public static final Integer DATA_EXCEPTION_CODE = 40007;
 90 
 91     /**
 92      * 获取access_token的接口地址
 93      */
 94     public final static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&secret=";
 95 
 96     /**
 97      * 图灵机器人接口地址
 98      */
 99     public final static String TURING_API_URL = "http://www.tuling123.com/openapi/api";
100 
101     private Constants() {
102     }
103 }
 

服务层处理文本消息


 1 @Service
 2 public class TextMessageHandle {
 3 
 4     /**
 5      * 处理文本消息
 6      *
 7      * @param customName  用户
 8      * @param severName   微信服务器
 9      * @param textContent 文本内容
10      * @return
11      * @throws Exception
12      */
13     public String processMessage(String customName, String severName, String textContent) throws Exception {
14         String fromUserName = customName;
15         String toUserName = severName;
16         String content = textContent;
17         String info = URLEncoder.encode(content, "utf-8");
18         String requestUrl = Constants.TURING_API_URL + "?key=" + AppConstants.API_KEY
19                 + "&info=" + info + "&userid=" + fromUserName;
20         String result = HttpUtil.get(requestUrl);
21         Object obj = MessageUtil.processTuRingResult(result, toUserName,
22                 fromUserName);
23         return MessageUtil.ObjectToXml(obj);
24     }
25 }


文本消息类


 1 @XStreamAlias("xml")
 2 public class TextMessage extends BaseMessage{
 3 
 4     @XStreamAlias("Content")
 5     @XStreamCDATA
 6     private String Content;
 7 
 8     public TextMessage() {
 9 
10     }
11 
12     public TextMessage(String fromUserName, String toUserName, String content) {
13         super(fromUserName, toUserName);
14         super.setMsgType(Constants.RESP_TEXT_TYPE);
15         this.Content = content;
16     }
17 
18     public String getContent() {
19         return Content;
20     }
21 
22     public void setContent(String content) {
23         Content = content;
24     }
25 }
 

基础消息


 1 public class BaseMessage implements Serializable {
 2     @XStreamAlias("ToUserName")
 3     @XStreamCDATA
 4     private String ToUserName;
 5 
 6     @XStreamAlias("FromUserName")
 7     @XStreamCDATA
 8     private String FromUserName;
 9 
10     @XStreamAlias("CreateTime")
11     private Long CreateTime;
12 
13     @XStreamAlias("MsgType")
14     @XStreamCDATA
15     private String MsgType;
16 
17     public BaseMessage() {
18         super();
19     }
20 
21     public BaseMessage(String fromUserName, String toUserName) {
22         super();
23         FromUserName = fromUserName;
24         ToUserName = toUserName;
25         CreateTime = System.currentTimeMillis();
26     }
27 
28     public String getToUserName() {
29         return ToUserName;
30     }
31 
32     public void setToUserName(String toUserName) {
33         ToUserName = toUserName;
34     }
35 
36     public String getFromUserName() {
37         return FromUserName;
38     }
39 
40     public void setFromUserName(String fromUserName) {
41         FromUserName = fromUserName;
42     }
43 
44     public Long getCreateTime() {
45         return CreateTime;
46     }
47 
48     public void setCreateTime(Long createTime) {
49         CreateTime = createTime;
50     }
51 
52     public String getMsgType() {
53         return MsgType;
54     }
55 
56     public void setMsgType(String msgType) {
57         MsgType = msgType;
58     }
59 }

应用常量


 1 public final class AppConstants {
 2 
 3     /**
 4      * 应用id
 5      */
 6     public static String APP_ID = "";
 7     /**
 8      * 应用秘钥
 9      */
10     public static String APP_SECRET = "";
11     /**
12      * 令牌
13      */
14     public static String TOKEN = "";
15     /**
16      * 图灵机器人应用key
17      */
18     public static String API_KEY = "";
19 }
 

最后服务层处理来自文本消息

 1 else if (MsgType.TEXT.getValue().equals(msgType)) {
 2     //点击菜单
 3     //回复微信服务器成功
 4     try {
 5         String result;
 6         result = textMessageHandle.processMessage(custermname, servername, content);
 7         writeText(result, response);
 8         }
 9     } catch (Exception e) {
10         logger.error("接收来至微信服务器的消息出现错误", e);
11         writeText(MessageUtil.ObjectToXml(new TextMessage(custermname,
12                 servername, "我竟无言以对!")), response);
13         e.printStackTrace();
14     }
 1 private void writeText(String content, HttpServletResponse response) {
 2     Writer writer = null;
 3     try {
 4         response.setContentType("text/html");
 5         response.setCharacterEncoding("UTF-8");
 6         writer = response.getWriter();
 7         writer.write(content);
 8         writer.flush();
 9     } catch (IOException e) {
10         logger.error("响应客户端文本内容出现异常", e);
11     } finally {
12         IOUtils.close(writer);
13     }
14 }

结果:

原文地址:https://www.cnblogs.com/PreachChen/p/8652616.html