微信公众号接入图灵机器人实现自动回复消息
生活随笔
收集整理的這篇文章主要介紹了
微信公众号接入图灵机器人实现自动回复消息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
1、創建圖靈機器人
??? 進入圖靈機器人網址:http://www.tuling123.com/
??? 登錄/注冊,進入機器人管理,然后點擊創建機器人
然后我們選擇或填寫機器人的相關屬性,這里我選擇的是聊天社交,模擬真人聊天的機器人,應用終端由于是微信公眾號接入,機器人設置里面,我們可以拿到接口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; } }應用常量
public final class AppConstants {/** * 應用id */ public static String APP_ID = ""; /** * 應用秘鑰 */ public static String APP_SECRET = ""; /** * 令牌 */ public static String TOKEN = ""; /** * 圖靈機器人應用key */ public static String API_KEY = ""; }最后服務層處理來自文本消息
else if (MsgType.TEXT.getValue().equals(msgType)) {//點擊菜單 //回復微信服務器成功 try {String result;result = textMessageHandle.processMessage(custermname, servername, content); writeText(result, response); }} catch (Exception e) {logger.error("接收來至微信服務器的消息出現錯誤", e); writeText(MessageUtil.ObjectToXml(new TextMessage(custermname, servername, "我竟無言以對!")), response); e.printStackTrace(); } private void writeText(String content, HttpServletResponse response) {Writer writer = null; try {response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); writer = response.getWriter(); writer.write(content); writer.flush(); } catch (IOException e) {logger.error("響應客戶端文本內容出現異常", e); } finally {IOUtils.close(writer); } }結果:
轉載于:https://my.oschina.net/inchlifc/blog/1617986
總結
以上是生活随笔為你收集整理的微信公众号接入图灵机器人实现自动回复消息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构免费课程
- 下一篇: MySQL 使用自增ID主键和UUID