关注后无法自动回复消息,但被动消息可以
来源:4-3 第四天 自动回复各种消息
elewei
2017-08-01
// weixin.js
'use strict'
exports.reply = function *(next) {
var message = this.weixin;
console.log(message);
if(message.MsgType === 'event') {
if(message.Event === 'subscribe') {
if(message.EventKey) {
console.log('扫码关注 ' + message.EventKey + ' ' + message.ticket);
}
this.body = '哈哈,你订阅了这个号';
console.log('哈哈,你订阅了这个号\r\n');
}
else if (message.Event === 'unsubscribe') {
console.log('无情取关');
this.body = '';
}
else if(message.Event === 'LOCATION') {
this.body = '您上报的位置是:' + message.Latitude + '/' + message.Longitude + '-' + message.Precision;
}
else if(message.Event === 'CLICK') {
this.body = '您点击了菜单' + message.EventKey
}
else if(message.Event === 'SCAN') {
console.log('关注后扫码' + message.EventKey + ' ' + message.Ticket);
this.body = '感谢扫码';
}
else if(message.Event === 'VIEW') {
this.body = '您点击了菜单中的链接: ' + message.EventKey;
}
}
else if(message.MsgType === 'text'){
var content = message.Content;
var reply = '你说啥' + message.Content + ' 太复杂了';
if(content === '1') {
reply = '天下第一';
}
else if(content === '2') {
reply = '回复2';
}
else if(content === '3') {
reply = '回复3';
}
this.body = reply;
}
yield next;
}tpl.js
'use strict'
var ejs = require('ejs');
var heredoc = require('heredoc')
var tpl = heredoc(function() { /*
<xml>
<ToUserName><![CDATA[<%= toUserName %>]]></ToUserName>
<FromUserName><![CDATA[<%= fromUserName %>]]></FromUserName>
<CreateTime><%= createTime %></CreateTime>
<MsgType><![CDATA[<%= msgType %>]]></MsgType>
<% if(msgType === 'text') { %>
<Content><![CDATA[<%= content %>]]></Content>
<% } else if(msgType ==='event') { %>
<Content><![CDATA[<%= content %>]]></Content>
<% } else if(msgType ==='image') { %>
<Image>
<MediaId><![CDATA[<%= content.media_id %>]]></MediaId>
</Image>
<% } else if(msgType ==='voice') { %>
<Voice>
<MediaId><![CDATA[<%= content.media_id %>]]></MediaId>
</Voice>
<% } else if(msgType ==='video') { %>
<Video>
<MediaId><![CDATA[<%= content.media_id %>]]></MediaId>
<Title><![CDATA[<%= content.title %>]]></Title>
<Description><![CDATA[<%= content.description %>]]></Description>
</Video>
<% } else if(msgType ==='video') { %>
<Music>
<Title><![CDATA[<%= content.TITLE %>]]></Title>
<Description><![CDATA[<%= content.DESCRIPTION %>]]></Description>
<MusicUrl><![CDATA[<%= content.MUSIC_Url %>]]></MusicUrl>
<HQMusicUrl><![CDATA[<%= content.HQ_MUSIC_Url %>]]></HQMusicUrl>
<ThumbMediaId><![CDATA[<%= content.media_id %>]]></ThumbMediaId>
</Music>
<% } else if(msgType ==='news') { %>
<ArticleCount><%= content.length %></ArticleCount>
<Articles>
<% content.forEach(function(item) { %>
<item>
<Title><![CDATA[<%= item.title %>]]></Title>
<Description><![CDATA[<%= item.description %>]]></Description>
<PicUrl><![CDATA[<%= item.picurl %>]]></PicUrl>
<Url><![CDATA[<%= item.url %>]]></Url>
</item>
<% }); %>
</Articles>
<% } %>
</xml>
*/});
var compiled = ejs.compile(tpl);
exports = module.exports = {
compiled: compiled
}
服务器启来了
{ signature: 'd94912b647e1177e5844fa99c37f2f3efbe16a5d',
timestamp: '1501597612',
nonce: '1773188309',
openid: 'orCvw0WmtuVu4WTFDTvxYLRS-tgI' }
{ ToUserName: 'gh_85b6a32d65a5',
FromUserName: 'orCvw0WmtuVu4WTFDTvxYLRS-tgI',
CreateTime: '1501597612',
MsgType: 'event',
Event: 'unsubscribe',
EventKey: '' }
无情取关
<xml>
<ToUserName><![CDATA[orCvw0WmtuVu4WTFDTvxYLRS-tgI]]></ToUserName>
<FromUserName><![CDATA[gh_85b6a32d65a5]]></FromUserName>
<CreateTime>1501597613027</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Content><![CDATA[]]></Content>
</xml>
{ signature: 'ed0409a4fc72206d2a0a3d482b74b9309d0ee8ad',
timestamp: '1501597625',
nonce: '90413853',
openid: 'orCvw0WmtuVu4WTFDTvxYLRS-tgI' }
{ ToUserName: 'gh_85b6a32d65a5',
FromUserName: 'orCvw0WmtuVu4WTFDTvxYLRS-tgI',
CreateTime: '1501597625',
MsgType: 'event',
Event: 'subscribe',
EventKey: '' }
哈哈,你订阅了这个号
<xml>
<ToUserName><![CDATA[orCvw0WmtuVu4WTFDTvxYLRS-tgI]]></ToUserName>
<FromUserName><![CDATA[gh_85b6a32d65a5]]></FromUserName>
<CreateTime>1501597625956</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Content><![CDATA[哈哈,你订阅了这个号]]></Content>
</xml>
{}
{ signature: '2159ecd38d44c69df4b64e5af12bbcfb82ab56d2',
timestamp: '1501597653',
nonce: '1239035732',
openid: 'orCvw0WmtuVu4WTFDTvxYLRS-tgI' }
{ ToUserName: 'gh_85b6a32d65a5',
FromUserName: 'orCvw0WmtuVu4WTFDTvxYLRS-tgI',
CreateTime: '1501597652',
MsgType: 'text',
Content: ',',
MsgId: '6449312807514822511' }
<xml>
<ToUserName><![CDATA[orCvw0WmtuVu4WTFDTvxYLRS-tgI]]></ToUserName>
<FromUserName><![CDATA[gh_85b6a32d65a5]]></FromUserName>
<CreateTime>1501597653126</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[你说啥, 太复杂了]]></Content>
</xml>关注后,无法自动回复?
写回答
2回答
-
Scott
2017-08-03
看下是不是 Util 里面的 tpl 方法实现的有问题
00 -
elewei
提问者
2017-08-01
在weixin.js 代码中 强制转了 text
if(message.MsgType === 'event') { if(message.Event === 'subscribe') { if(message.EventKey) { console.log('扫码关注 ' + message.EventKey + ' ' + message.ticket); } message.MsgType = 'text'; this.body = '哈哈,你订阅了这个号'; console.log('哈哈,你订阅了这个号\r\n'); }tpl.js
<% if(msgType === 'text' || msgType === 'event') { %> <Content><![CDATA[<%= content %>]]></Content> <% } else if(msgType ==='image') { %>Bash输出
{ signature: '3b875cd6d80adb4556e4d2c8274e37f0590c0887', timestamp: '1501599227', nonce: '1064218291', openid: 'orCvw0WmtuVu4WTFDTvxYLRS-tgI' } { ToUserName: 'gh_85b6a32d65a5', FromUserName: 'orCvw0WmtuVu4WTFDTvxYLRS-tgI', CreateTime: '1501599227', MsgType: 'event', Event: 'subscribe', EventKey: '' } 哈哈,你订阅了这个号 <xml> <ToUserName><![CDATA[orCvw0WmtuVu4WTFDTvxYLRS-tgI]]></ToUserName> <FromUserName><![CDATA[gh_85b6a32d65a5]]></FromUserName> <CreateTime>1501599227855</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[哈哈,你订阅了这个号]]></Content> </xml>我是这样解决的。 总感觉这个方法不规范。
00
相似问题
关注成功后,没有收到设置好的回复消息。
回答 4
ejs提示错误
回答 5