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 }