日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

微信开发系列之二 - 在微信公众号里开发一个自动应答的图灵机器人

發(fā)布時間:2023/12/19 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 微信开发系列之二 - 在微信公众号里开发一个自动应答的图灵机器人 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

In previous blog Wechat development series 1 – setup your development environment I introduce the necessary step to setup environment for Wechat development.

In this blog, let’s try to achieve some features which makes more sense – the Q&A service implementation based on Wechat platform.
Q&A service typically has the following activity flow:

(1) The users of your Wechat subscription account send some text to your subscription account as question;
(2) The Wechat platform delegates the message sent by your user to the given url maintained in the subscription account. See mine below for example:

(3) Now it is your turn: parse the text sent delegate from Wechat platform, and send answer back into the requested HTTP response. Once that has been done, your end user will receive the answer with the help of Wechat platform.

In this blog I will introduce two kinds of Q&A service implemented.
(1) Echo service: Your subscription account users will receive exactly the same text as they send. In order to prove that the text has really reached the nodejs server, I add a prefix “Add by Jerry:” in front of the echo string.
See example below:

(2) Tuning service: this service is more smart than the echo service: it will call tuning API to try to chat with your subscription account user:

Here below are the detail steps to implement these two kinds of Q&A service.

Echo service

(1) implement server.js which is the entry point of your nodejs server logic:

var express = require('express'); var routesEngine = require('./jerryapp/routes/index.js'); var app = express(); routesEngine(app);app.listen(process.env.PORT || 3000, function () {console.log('Listening on port, process.cwd(): ' + process.cwd() ); });

(2) implement index.js which is used in server.js:

var request = require('request'); var echoService = require("../service/echo.js"); module.exports = function (app) {app.route('/').post(function(req,res){echoService(req, res);}); };

The code above shows that when your user send a text to your subscription account, an HTTP post request containing this text will be delegated to your nodejs server by Wechat platform, as a result it is your responsibility to parse the text from HTTP post, do your own logic ( simple echo or tuning handling ) and send the response back. The echo service in this blog is implemented in module echo.js.

(3) Implement echo.js:

var getXMLNodeValue = require("../tool/xmlparse.js"); var replyMessage = require("../tool/replyMessage.js"); const content_pattern = /<!\[CDATA\[(.*)\]\]>/; module.exports = function(req, res){var _da;req.on("data",function(data){_da = data.toString("utf-8");});req.on("end",function(){var Content = getXMLNodeValue('Content',_da);var body = content_pattern.exec(Content);if( body.length === 2) {Content = "Add by Jerry: " + body[1];} var xml = replyMessage(_da, Content);res.send(xml);}); };

Here I simply add the hard coded prefix “Add by Jerry:” to the original text and send it back.
The source code of utility module xmlparse.js:

module.exports = function(node_name, xml){var tmp = xml.split("<"+node_name+">");var _tmp = tmp[1].split("</"+node_name+">");return _tmp[0]; };

replyMessage.js:

var getXMLNodeValue = require("./xmlparse.js"); module.exports = function(originalBody, contentToReply){var ToUserName = getXMLNodeValue('ToUserName', originalBody);var FromUserName = getXMLNodeValue('FromUserName',originalBody);var CreateTime = getXMLNodeValue('CreateTime',originalBody);var MsgType = getXMLNodeValue('MsgType',originalBody);var Content = contentToReply;var MsgId = getXMLNodeValue('MsgId', originalBody);var xml = '<xml><ToUserName>'+FromUserName+'</ToUserName><FromUserName>'+ToUserName+'</FromUserName><CreateTime>'+CreateTime+'</CreateTime><MsgType>'+MsgType+'</MsgType><Content>'+Content+'</Content></xml>';console.log("xml to be sent: " + xml);return xml; };

Tuning Service

It has almost the same steps as done for Echo service except some small enhancement.
In index.js, simply replace echoService call with tuningService.

The implementation of tuning service module:

var request = require('request'); var getXMLNodeValue = require("../tool/xmlparse.js"); var replyMessage = require("../tool/replyMessage.js"); const content_pattern = /<!\[CDATA\[(.*)\]\]>/;const url = "http://www.tuling123.com/openapi/api?key=de4ae9269c7438c33de5806562a35cac&info=";module.exports = function(req, res){var _da;req.on("data",function(data){_da = data.toString("utf-8");});req.on("end",function(){console.log("original text: " + _da);var Content = getXMLNodeValue('Content',_da);console.log("content: " + Content);var body = content_pattern.exec(Content);console.log("result size: " + body.length);var requesturl = "";if( body.length === 2){requesturl = url + encodeURI(body[1]);} var options = {url: requesturl,method: "GET"};request(options,function(error,response,data){if(data){var text = JSON.parse(data).text;var xml = replyMessage(_da, text);res.send(xml);}else {res.send("Error when calling Tuning API: " + error);console.log(error);}});}); };

In this module I just use a free tuning service provided by http://www.tulin123.com, which is very convenient to consume via Restful API call.

要獲取更多Jerry的原創(chuàng)文章,請關(guān)注公眾號"汪子熙":

總結(jié)

以上是生活随笔為你收集整理的微信开发系列之二 - 在微信公众号里开发一个自动应答的图灵机器人的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 少妇高潮一区二区三区99小说 | 99久久久成人国产精品 | 中字幕一区二区三区乱码 | 日韩国产网站 | 婷婷亚洲五月 | 国语对白91 | 毛片毛片毛片毛片毛片毛片毛片 | 伊人网综合视频 | 99久久人妻无码精品系列 | 久久三级网 | h视频在线看| 中文字幕av免费在线观看 | 精品国产成人亚洲午夜福利 | 黄色av电影在线观看 | 午夜成人在线视频 | 夜夜精品视频 | 欧美怡红院视频 | 狂野欧美性猛交xxⅹ李丽珍 | 自拍偷拍视频网 | 精品国产一区二区三区av性色 | 嫩草社区| 欧美色女人 | 国产成人无码久久久精品天美传媒 | h片在线观看视频 | 欧美日韩一级二级三级 | 久久艹伊人 | 欧美一级不卡 | 国产精品久久久久久久 | 夜色导航 | 中文字幕2区 | 噜噜色av | 欧美精品一线 | aaaaa毛片 | 男人插女人下面视频 | 女性裸体瑜伽无遮挡 | 国产又粗又深又猛又爽又在线观看 | 国产精品人成 | 色香蕉网 | 久久精品国产亚洲av久 | 国产丝袜美腿一区二区三区 | 中文字幕5566 | 免费精品在线视频 | 狼性av懂色av禁果av | 人妻久久久一区二区三区 | 久久久久久久久电影 | 日韩成人高清视频在线观看 | 第一色网站 | 国产一区二区在线看 | 国产一区免费看 | 电影《两个尼姑》免费播放 | 国产成人精品一区二区三区在线 | 99久久久国产精品无码网爆 | 毛片综合 | 91九色在线视频 | 夜夜干天天操 | 天天射av| 大地资源中文第三页 | 海角社区id:1220.7126,10. | 少妇高潮一区二区三区99 | 国产欧美日韩综合 | china国模大尺度pics | 亚洲欧美一区二区激情 | 精品一区二区免费看 | 精品一区二区三区在线免费观看 | 黄色网战入口 | 国产大片aaa | 亚洲精品97 | 男人天堂一区 | 精品一二三区久久aaa片 | 中文字幕欧美在线观看 | 涩涩在线播放 | 两个人看的www视频免费完整版 | 久久69| 亚洲男人的天堂网 | 在线波多野结衣 | 日韩精品福利视频 | 禁断介护av| 乱淫的女高中暑假调教h | 日日日日日日bbbbbb | 国产午夜福利一区二区 | 久热精品视频 | 麻豆av电影在线观看 | av.www| 午夜性视频 | free性护士vidos猛交 | www精品国产 | 欧美日韩亚洲色图 | 国产午夜不卡 | 成人毛片一区二区三区 | 成人免费在线观看 | 农民人伦一区二区三区 | 国产毛片18 | 精久久久久久久 | 亚洲精品国产suv一区 | 视频一区二区三区四区五区 | 污漫在线观看 | 99在线小视频 | 黄a在线观看 | 日韩免费不卡视频 |