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

1、创建图灵机器人

    进入图灵机器人网址:,.......->点击创建机器人

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

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

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

2、后端代码开始接入

常量类

public final class Constants { /** * GET或POST必须大写,不可更改 */ public static final String GET = "GET"; public static final String POST = "POST"; /* 微信请求消息类型(由微信方规定,不可更改) */ /** * 文本 */ public static final String REQ_TEXT_TYPE = "text"; /** * 事件 */ public static final String REQ_EVENT_TYPE = "event"; /** * 订阅 */ public static final String REQ_SUBSCRIBE_TYPE = "subscribe"; /** * 取消订阅 */ public static final String REQ_UNSUBSCRIBE_TYPE = "unsubscribe"; /* 微信返回消息类型(由微信方规定,不可更改) */ /** * 文本 */ public static final String RESP_TEXT_TYPE = "text"; /** * 图文 */ public static final String RESP_NEWS_TYPE = "news"; /* 图灵机器人返回数据类型状态码(官方固定) */ /** * 文本 */ public static final Integer TEXT_CODE = 100000; /** * 列车 */ public static final Integer TRAIN_CODE = 305000; /** * 航班 */ public static final Integer FLIGHT_CODE = 306000; /** * 链接类 */ public static final Integer LINK_CODE = 200000; /** * 新闻 */ public static final Integer NEWS_CODE = 302000; /** * 菜谱、视频、小说 */ public static final Integer MENU_CODE = 308000; /** * key的长度错误(32位) */ public static final Integer LENGTH_WRONG_CODE = 40001; /** * 请求内容为空 */ public static final Integer EMPTY_CONTENT_CODE = 40002; /** * key错误或帐号未激活 */ public static final Integer KEY_WRONG_CODE = 40003; /** * 当天请求次数已用完 */ public static final Integer NUMBER_DONE_CODE = 40004; /** * 暂不支持该功能 */ public static final Integer NOT_SUPPORT_CODE = 40005; /** * 服务器升级中 */ public static final Integer UPGRADE_CODE = 40006; /** * 服务器数据格式异常 */ public static final Integer DATA_EXCEPTION_CODE = 40007; /** * 获取access_token的接口地址 */ public final static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&secret="; /** * 图灵机器人接口地址 */ public final static String TURING_API_URL = "http://www.tuling123.com/openapi/api"; private Constants() { } }

服务层处理文本消息

@Service public class TextMessageHandle { /** * 处理文本消息 * * @param customName 用户 * @param severName 微信服务器 * @param textContent 文本内容 * @return * @throws Exception */ public String processMessage(String customName, String severName, String textContent) throws Exception { String fromUserName = customName; String toUserName = severName; String content = textContent; String info = URLEncoder.encode(content, "utf-8"); String requestUrl = Constants.TURING_API_URL + "?key=" + AppConstants.API_KEY + "&info=" + info + "&userid=" + fromUserName; String result = HttpUtil.get(requestUrl); Object obj = MessageUtil.processTuRingResult(result, toUserName, fromUserName); return MessageUtil.ObjectToXml(obj); } }

文本消息类

@XStreamAlias("xml") public class TextMessage extends BaseMessage{ @XStreamAlias("Content") @XStreamCDATA private String Content; public TextMessage() { } public TextMessage(String fromUserName, String toUserName, String content) { super(fromUserName, toUserName); super.setMsgType(Constants.RESP_TEXT_TYPE); this.Content = content; } public String getContent() { return Content; } public void setContent(String content) { Content = content; } }

基础消息

public class BaseMessage implements Serializable { @XStreamAlias("ToUserName") @XStreamCDATA private String ToUserName; @XStreamAlias("FromUserName") @XStreamCDATA private String FromUserName; @XStreamAlias("CreateTime") private Long CreateTime; @XStreamAlias("MsgType") @XStreamCDATA private String MsgType; public BaseMessage() { super(); } public BaseMessage(String fromUserName, String toUserName) { super(); FromUserName = fromUserName; ToUserName = toUserName; CreateTime = System.currentTimeMillis(); } public String getToUserName() { return ToUserName; } public void setToUserName(String toUserName) { ToUserName = toUserName; } public String getFromUserName() { return FromUserName; } public void setFromUserName(String fromUserName) { FromUserName = fromUserName; } public Long getCreateTime() { return CreateTime; } public void setCreateTime(Long createTime) { CreateTime = createTime; } public String getMsgType() { return MsgType; } public void setMsgType(String msgType) { MsgType = msgType; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zywdzf.html