微信公众号自动回复 node
生活随笔
收集整理的這篇文章主要介紹了
微信公众号自动回复 node
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
純屬分享記錄:
app.js
var bodyParser = require('body-parser'); require('body-parser-xml')(bodyParser); var wx = require('./admin/wx/wx.js'); //解析xml app.use(bodyParser.xml({ limit: '1MB', xmlParseOptions: {normalize: true,normalizeTags: true,explicitArray: false } }));//微信 app.post('/wx',function(req,res){wx.replyWx(req,res); });wx.js
//微信自動回復(fù) function wx(){this.replyWx = function(req,res){res.writeHead(200, {'Content-Type': 'application/xml'});var data = req.body.xml;if(data.msgtype === 'event' && data.event === 'subscribe'){data.content = '歡迎關(guān)注淺唱姩華,這里是開發(fā)者試用帳號!';msgText(res,data);}else if(data.msgtype === 'event' && data.event === 'unsubscribe'){//取消關(guān)注了}else if(data.msgtype === 'text'){//文本消息 msgText(res,data);}else if(data.msgtype === 'image'){//圖片消息data.content = '你好暫不支持圖片回復(fù)';msgText(res,data);}else if(data.msgtype === 'video'){//視頻消息data.content = '你好暫不支持視頻回復(fù)';msssage(res,data);}else if(data.msgtype === 'voice'){//語言消息data.content = '你好暫不支語音回復(fù)';msssage(res,data);}} }function msgText(res,data){//fromusername 接受者openid//tousername 發(fā)送者openidvar resMsg = '<xml>' +'<ToUserName><![CDATA[' + data.fromusername + ']]></ToUserName>' +'<FromUserName><![CDATA[' + data.tousername + ']]></FromUserName>' +'<CreateTime>' + parseInt(new Date().valueOf() / 1000) + '</CreateTime>' +'<MsgType><![CDATA[text]]></MsgType>' +'<Content><![CDATA['+data.content+']]></Content>' +'</xml>';res.end(resMsg); }module.exports = new wx();訪問路徑就是你token驗證通過的路徑,這里是自動回復(fù)文本消息,用戶發(fā)什么,你自動回什么
轉(zhuǎn)載于:https://www.cnblogs.com/bruce-gou/p/6367447.html
總結(jié)
以上是生活随笔為你收集整理的微信公众号自动回复 node的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL数据类型--日期时间
- 下一篇: 今天刚刚开通了写播客的功能,以后记录工作