在上一往篇文章《Nodejs微信开发》中,微信后台能够正常的接收到客户端的消息,并能够简单的回复一条消息至客户端。
但我的目录是将微信与Bot Framework进行关联,那么肯定就有一些情况是Bot Framework是有多条消息,或是会回复图片消息的,这个时候只回复一条消息就无法满足我现在的需求了
可以看到在以下代码中,我使用wechat进行微信消息的接收,使用wechat-api对用户进行多次的回复,我就可以解决wechat只能对消息回复一次的问题
简单来说,wechat只负责接收到消息的回复,从bot framework提取到的消息需要通过wechat-api来进行发送
var wechat = require('wechat'); var wechatAPI = require('wechat-api'); //wechat config var config = { token: 'weixin', appid: 'wx1434eed5268660c4', encodingAESKey: 'ZEtViedarf49EUOCDeu45pqhkZhKPFBjSHI2DynP4vq', checkSignature: true // 可选,默认为true。由于微信公众平台接口调试工具在明文模式下不发送签名,所以如要使用该测试工具,请将其设置为false }; //创建wechat-api var api = new wechatAPI(config.appid, '30a5f51682755652e6e02879757a0fb1'); var menu = { "button": [ { "type": "click", "name": "WeChat Bot", "key": "V1001_TODAY_MUSIC" }, { "name": "BotFramework", "sub_button": [ { "type": "view", "name": "botframework", "url": "https://dev.botframework.com/" }, { "type": "click", "name": "赞一下我们", "key": "V1001_GOOD" }, { "name": "发送位置", "type": "location_select", "key": "rselfmenu_2_0" },] }] }; //删除菜单 api.removeMenu(function (err, result) { if (err) { logger.log('error', err); } logger.log('info', 'remove menu success'); }); //创建菜单 api.createMenu(menu, function (err, result) { if (err) { logger.log('error', err); } logger.log('info', 'create menu success'); }); app.use(express.query()); app.use('/wechat', wechat(config, wechat.text(function (message, req, res, next) { //------------------------------------------------------------------------ var message = req.weixin; logger.log("info", message); res.reply('Message Send To Bot Completed , Wait Response.'); api.sendText(message.FromUserName, 'this message from wechat-api', function (err, result) { if (err) { logger.log('error', err); } logger.log('info', 'reply message success'); }); }).image(function (message, req, res, next) { var message = req.weixin; logger.log("info", message); res.reply('功能开发中'); }).voice(function (message, req, res, next) { var message = req.weixin; logger.log("info", message); res.reply('功能开发中'); }).video(function (message, req, res, next) { var message = req.weixin; logger.log("info", message); res.reply('功能开发中'); }).location(function (message, req, res, next) { var message = req.weixin; logger.log("info", message); res.reply('功能开发中'); }).link(function (message, req, res, next) { var message = req.weixin; logger.log("info", message); res.reply('功能开发中'); }).event(function (message, req, res, next) { var message = req.weixin; logger.log("info", message); res.reply('感谢你的关注,你也可以在nodejs npm中查看wechat和wechat-api'); }).device_text(function (message, req, res, next) { var message = req.weixin; logger.log("info", message); res.reply('功能开发中'); }).device_event(function (message, req, res, next) { if (message.Event === 'subscribe' || message.Event === 'unsubscribe') { var message = req.weixin; logger.log("info", message); res.reply("功能开发中"); } else { var message = req.weixin; logger.log("info", message); res.reply('功能开发中'); } })));
参考:https://www.npmjs.com/package/wechat-api
https://www.npmjs.com/package/wechat
http://mp.weixin.qq.com/wiki/16/52e198333f5c127ddd5c45e875191b14.html