自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

微信公眾平臺(tái)開發(fā)(二)簡單的聊天機(jī)器人

移動(dòng)開發(fā) Android
微信公眾平臺(tái)是騰訊公司在微信的基礎(chǔ)上新增的功能模塊,通過這一平臺(tái),個(gè)人和企業(yè)都可以打造一個(gè)微信的公眾號(hào),并實(shí)現(xiàn)和特定群體的文字、圖片、語音的全方位溝通、互動(dòng)。

1.設(shè)置成為開發(fā)者模式

登錄微信工作平臺(tái),選擇高級(jí)功能-進(jìn)入開發(fā)模式,成為開發(fā)者。需要做如下圖配置。URL配置的信息是指,微信的后臺(tái)服務(wù)器把您的用戶消息發(fā)送到該URL處理。Token是你和微信之間的一個(gè)密碼,用來驗(yàn)證消息是否是從微信的服務(wù)發(fā)送而來,而不是其他來攻擊你的系統(tǒng)。

現(xiàn)在你還不能設(shè)置,在設(shè)置時(shí)微信會(huì)GET請求你設(shè)置的URL,已檢測接口是否可以使用。只有等你準(zhǔn)備好GET方法之后才可以進(jìn)行設(shè)置。

[[70579]]

2.實(shí)現(xiàn)GET方法

從文檔中知道,我們需要實(shí)現(xiàn)POST和GET方法,GET方法用于驗(yàn)證微信和你的通訊驗(yàn)證,POST用于消息處理。

新建Servlet HelloWeChat,先實(shí)現(xiàn)其中的GET方法

  1. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  2.         // TODO 為了簡單起見,先不對消息來源進(jìn)行校驗(yàn) 
  3.         response.setContentType("text/html;charset=UTF-8"); 
  4.         PrintWriter pw = response.getWriter(); 
  5.         String echo = request.getParameter("echostr"); 
  6.         echo = new String(echo.getBytes("ISO-8859-1"),"UTF-8"); 
  7.         pw.println(echo); 
  8.     } 

可以在本地使用http://localhost:8080/QiyadengWeb/HelloWeChat?echostr=hello中文,先進(jìn)行測試,如果沒有問題,可以部署到服務(wù)器上,然后在微信公眾平臺(tái)進(jìn)行設(shè)置了。

3.實(shí)現(xiàn)POST方法

POST方法首先接收到微信公眾平臺(tái)傳送過來的XML,從中提取消息發(fā)送人和消息內(nèi)容。更加消息發(fā)送內(nèi)容,你可以增加自己的處理邏輯,最后拼裝成回復(fù)消息XML,返回給微信公眾平臺(tái)。

  1. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  2.         response.setContentType("text/html;charset=UTF-8"); 
  3.         PrintWriter pw = response.getWriter(); 
  4.         String wxMsgXml = IOUtils.toString(request.getInputStream(),"utf-8"); 
  5.         WeChatTextMessage textMsg = null
  6.         try { 
  7.             textMsg = getWeChatTextMessage(wxMsgXml); 
  8.         } catch (Exception e) { 
  9.             e.printStackTrace(); 
  10.         } 
  11.         StringBuffer replyMsg = new StringBuffer(); 
  12.         if(textMsg != null){ 
  13.             //增加你所需要的處理邏輯,這里只是簡單重復(fù)消息 
  14.             replyMsg.append("您給我的消息是:"); 
  15.             replyMsg.append(textMsg.getContent()); 
  16.         } 
  17.         else
  18.             replyMsg.append(":)不是文本的消息,我暫時(shí)看不懂"); 
  19.         } 
  20.         String returnXml = getReplyTextMessage(replyMsg.toString(), textMsg.getFromUserName()); 
  21.         pw.println(returnXml); 
  22.     } 

關(guān)于調(diào)試,這里推薦一個(gè)工具Fiddler,你可以模擬微信的POST消息到你的本地,而不必每次部署到服務(wù)器上進(jìn)行調(diào)試。關(guān)于Fiddler的POST數(shù)據(jù)使用方法,可以參考下圖標(biāo)注內(nèi)容。

4.部署并測試

完成第一步,并和你的公眾帳號(hào)好進(jìn)行對話,回復(fù)消息沒有問題的話,那就恭喜你了[[70580]]。

[[70581]]

5.依賴庫

使用maven的同學(xué),添加以下依賴即可。非maven用戶,找到這些庫添加到buider path中即可。

  1. <dependency> 
  2.     <groupId>joda-time</groupId> 
  3.     <artifactId>joda-time</artifactId> 
  4.     <version>2.2</version> 
  5. </dependency> 
  6. <dependency> 
  7.     <groupId>org.apache.commons</groupId> 
  8.     <artifactId>commons-io</artifactId> 
  9.     <version>1.3.2</version> 
  10. </dependency> 
  11. <dependency> 
  12.     <groupId>com.thoughtworks.xstream</groupId> 
  13.     <artifactId>xstream</artifactId> 
  14.     <version>1.4.3</version> 
  15. </dependency> 

6.完整的代碼

  1. package com.qiyadeng.wechat; 
  2. import java.io.IOException; 
  3. import java.io.PrintWriter; 
  4. import java.util.Date; 
  5. import javax.servlet.ServletException; 
  6. import javax.servlet.http.HttpServlet; 
  7. import javax.servlet.http.HttpServletRequest; 
  8. import javax.servlet.http.HttpServletResponse; 
  9. import org.apache.commons.io.IOUtils; 
  10. import com.thoughtworks.xstream.XStream; 
  11. import com.thoughtworks.xstream.io.xml.DomDriver; 
  12. /** 
  13.  * Servlet implementation class HelloWeChat 
  14.  */ 
  15. public class HelloWeChat extends HttpServlet { 
  16.     private static final long serialVersionUID = 1L; 
  17.     /** 
  18.      * @see HttpServlet#HttpServlet() 
  19.      */ 
  20.     public HelloWeChat() { 
  21.         super(); 
  22.     } 
  23.     /** 
  24.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
  25.      */ 
  26.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  27.         // TODO 為了簡單起見,先不對消息來源進(jìn)行校驗(yàn) 
  28.         response.setContentType("text/html;charset=UTF-8"); 
  29.         PrintWriter pw = response.getWriter(); 
  30.         String echo = request.getParameter("echostr"); 
  31.         echo = new String(echo.getBytes("ISO-8859-1"),"UTF-8"); 
  32.         pw.println(echo); 
  33.     } 
  34.     /** 
  35.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
  36.      */ 
  37.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  38.         response.setContentType("text/html;charset=UTF-8"); 
  39.         PrintWriter pw = response.getWriter(); 
  40.         String wxMsgXml = IOUtils.toString(request.getInputStream(),"utf-8"); 
  41.         WeChatTextMessage textMsg = null
  42.         try { 
  43.             textMsg = getWeChatTextMessage(wxMsgXml); 
  44.         } catch (Exception e) { 
  45.             e.printStackTrace(); 
  46.         } 
  47.         StringBuffer replyMsg = new StringBuffer(); 
  48.         if(textMsg != null){ 
  49.             //增加你所需要的處理邏輯,這里只是簡單重復(fù)消息 
  50.             replyMsg.append("您給我的消息是:"); 
  51.             replyMsg.append(textMsg.getContent()); 
  52.         } 
  53.         else
  54.             replyMsg.append(":)不是文本的消息,我暫時(shí)看不懂"); 
  55.         } 
  56.         String returnXml = getReplyTextMessage(replyMsg.toString(), textMsg.getFromUserName()); 
  57.         pw.println(returnXml); 
  58.     } 
  59.     private WeChatTextMessage getWeChatTextMessage(String xml){ 
  60.         XStream xstream = new XStream(new DomDriver()); 
  61.         xstream.alias("xml", WeChatTextMessage.class); 
  62.         xstream.aliasField("ToUserName", WeChatTextMessage.class"toUserName"); 
  63.         xstream.aliasField("FromUserName", WeChatTextMessage.class"fromUserName"); 
  64.         xstream.aliasField("CreateTime", WeChatTextMessage.class"createTime"); 
  65.         xstream.aliasField("MsgType", WeChatTextMessage.class"messageType"); 
  66.         xstream.aliasField("Content", WeChatTextMessage.class"content"); 
  67.         xstream.aliasField("MsgId", WeChatTextMessage.class"msgId"); 
  68.         WeChatTextMessage wechatTextMessage = (WeChatTextMessage)xstream.fromXML(xml);  
  69.         return wechatTextMessage; 
  70.     } 
  71.     private String getReplyTextMessage(String content, String weChatUser){ 
  72.         WeChatReplyTextMessage we = new WeChatReplyTextMessage(); 
  73.         we.setMessageType("text"); 
  74.         we.setFuncFlag("0"); 
  75.         we.setCreateTime(new Long(new Date().getTime()).toString()); 
  76.         we.setContent(content); 
  77.         we.setToUserName(weChatUser); 
  78.         we.setFromUserName("shanghaiweather");//TODO 你的公眾帳號(hào)微信號(hào) 
  79.         XStream xstream = new XStream(new DomDriver());  
  80.         xstream.alias("xml", WeChatReplyTextMessage.class); 
  81.         xstream.aliasField("ToUserName", WeChatReplyTextMessage.class"toUserName"); 
  82.         xstream.aliasField("FromUserName", WeChatReplyTextMessage.class"fromUserName"); 
  83.         xstream.aliasField("CreateTime", WeChatReplyTextMessage.class"createTime"); 
  84.         xstream.aliasField("MsgType", WeChatReplyTextMessage.class"messageType"); 
  85.         xstream.aliasField("Content", WeChatReplyTextMessage.class"content"); 
  86.         xstream.aliasField("FuncFlag", WeChatReplyTextMessage.class"funcFlag"); 
  87.         String xml =xstream.toXML(we); 
  88.         return xml; 
  89.     } 

閱讀第三篇:微信公眾平臺(tái)開發(fā)(三)位置信息的識(shí)別

閱讀第一篇:微信公眾平臺(tái)開發(fā)(一)平臺(tái)綜述

 

 

轉(zhuǎn)載自http://www.qiyadeng.com/

本文鏈接地址: 微信公眾平臺(tái)開發(fā)(二)–簡單的聊天機(jī)器人

責(zé)任編輯:閆佳明 來源: cnblogs
相關(guān)推薦

2023-02-13 08:14:45

2022-09-30 13:55:46

Python機(jī)器人

2022-07-05 06:42:01

聊天機(jī)器人人工智能

2019-07-03 10:02:47

聊天機(jī)器人平臺(tái)機(jī)器人

2018-06-08 16:18:43

Python微信聊天機(jī)器人

2017-08-21 13:31:44

AI聊天機(jī)器人facebook

2013-04-12 02:01:55

微信公眾平臺(tái)接口開發(fā)

2020-02-02 09:19:14

聊天機(jī)器人機(jī)器人智能

2017-03-28 12:21:21

機(jī)器人定義

2016-02-16 14:46:33

聊天機(jī)器人機(jī)器學(xué)習(xí)自然語言

2019-12-19 16:08:40

人工智能機(jī)器人數(shù)據(jù)

2022-07-03 10:23:06

機(jī)器人場景個(gè)性化

2023-06-29 15:04:21

微軟ChatGPT

2023-12-18 19:05:34

2024-07-01 07:35:11

2024-09-02 08:17:53

RAG聊天機(jī)器人人工智能

2021-10-28 22:46:13

Python機(jī)器人數(shù)據(jù)

2019-06-04 08:00:00

機(jī)器人聊天機(jī)器人人工智能

2020-02-20 09:00:00

網(wǎng)絡(luò)應(yīng)用數(shù)據(jù)技術(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)