您的位置:首页 > 移动开发 > 微信开发

微信订阅后台自动回复功能的实现

2016-11-29 00:00 756 查看

3.1. 订阅号后台自动回复功能的实现

3.1.1. 官网文档地址

https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1472017492_58YV5&token=&lang=zh_CN

3.1.2. 订阅号管理平台的配置

因为微信只支持80端口,所以通过ittun.com搭建模拟80端口的服务器

ittun官网网址:http://www.ittun.com/

下载客户端文件并解压缩


修改startup.bat文件的内容如下


双击启动startup.bat文件


来到订阅号后台并进入开发-基本配置页面进行配置


3.1.3. node后端接入

3.1.3.1. 原生代码实现

// 使用原生模块的写法
const express = require("express");
const {parseString} = require("xml2js");
const app = express();

// 接收微信服务器发过来的消息
app.post("/wx", (req, res, next) => {
// 微信订阅号传过来的数据都是xml格式
req.on("data", (buffer)=> {
let msg = buffer.toString();
//console.log(buffer.toString());
//<xml><ToUserName><![CDATA[gh_fdfc0bf0bb5a]]></ToUserName>
//<FromUserName><![CDATA[oryIWwNJ0Sgn8LyyxHsD6OLGxR9g]]></FromUserName>
//<CreateTime>1479613848</CreateTime>
//<MsgType><![CDATA[text]]></MsgType>
//<Content><![CDATA[大好河山]]></Content>
//<MsgId>6354893088312947331</MsgId>
//</xml>

//解析xml
parseString(msg, (err, result) => {
//console.log(result);

//console.log(result.xml.Content[0]);
//{ xml:
//{ ToUserName: [ 'gh_fdfc0bf0bb5a' ],
//    FromUserName: [ 'oryIWwAKciQIskVv3TCcI6LsY8kY' ],
//    CreateTime: [ '1479613991' ],
//    MsgType: [ 'text' ],
//    Content: [ '你那了' ],
//    MsgId: [ '6354893702493270691' ] } }

let touser = result.xml.FromUserName[0];
let fromuser = result.xml.ToUserName[0];
//如果收到的是文本消息
if (result.xml.MsgType[0] === "text") {
if (result.xml.Content[0] === "你好") {
let txt = "你也好";

let sendmsg = `
<xml>
<ToUserName><![CDATA[${touser}]]></ToUserName>
<FromUserName><![CDATA[${fromuser}]]></FromUserName>
<CreateTime>${Date.now()}</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[${txt}]]></Content>
</xml>
`;
res.send(sendmsg);
} else if (result.xml.Content[0] === "吃了么") {
let txt = "没吃呢,走一起食堂";

let sendmsg = `
<xml>
<ToUserName><![CDATA[${touser}]]></ToUserName>
<FromUserName><![CDATA[${fromuser}]]></FromUserName>
<CreateTime>${Date.now()}</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[${txt}]]></Content>
</xml>
`;
res.send(sendmsg);
} else if (result.xml.Content[0] === "看牙") {
//发送图文消息

let sendmsg = `
<xml>
<ToUserName><![CDATA[${touser}]]></ToUserName>
<FromUserName><![CDATA[${fromuser}]]></FromUserName>
<CreateTime>${Date.now()}</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[预约看牙]]></Title>
<Description><![CDATA[我们一起看牙去吧]]></Description>
<PicUrl><![CDATA[http://wx001.ittun.com/img/logo.png]]></PicUrl>
<Url><![CDATA[http://wx001.ittun.com/order.html]]></Url>
</item>
</Articles>
</xml>
`;

res.send(sendmsg);
} else {
// 不符合条件的回复
let txt = "滚蛋";

let sendmsg = `
<xml>
<ToUserName><![CDATA[${touser}]]></ToUserName>
<FromUserName><![CDATA[${fromuser}]]></FromUserName>
<CreateTime>${Date.now()}</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[${txt}]]></Content>
</xml>
`;
res.send(sendmsg);
}

}
else if (result.xml.MsgType[0] === "event") {
//是点击事件click
if (result.xml.Event[0] === "CLICK") {
if (result.xml.EventKey[0] === "V1001_TODAY_MUSIC") {
let txt = "收到今日歌曲";

let sendmsg = `
<xml>
<ToUserName><![CDATA[${touser}]]></ToUserName>
<FromUserName><![CDATA[${fromuser}]]></FromUserName>
<CreateTime>${Date.now()}</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[${txt}]]></Content>
</xml>
`
res.send(sendmsg);
}
}
}

});
});
})

// 启动server
const server = app.listen(3002, function () {
const host = server.address().address;
const port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});


3.1.3.2. wechat模块的实现

wechat模块:https://github.com/node-webot/wechat

代码

// 使用wechat模块的写法
const wechat = require('wechat');
const express = require("express");

const app = express();
const config = {
token: 'dingyuehaohoutai',
appid: 'wx263a41ae72347',
encodingAESKey: 'T5766o1ql3lXrPIN0C4Tv1Udf3CkHhzFG'
};

app.use('/wx', wechat(config, function (req, res, next) {
// 微信输入信息都在req.weixin上
const message = req.weixin;
console.log(message.Content);
// {
//      ToUserName: 'gh_dbd933de052c',
//      FromUserName: 'oB3MDvytZho2Xs6ed-U5-QEeDJ3o',
//      CreateTime: '1479632080',
//      MsgType: 'text',
//      Content: 'hehe',
//      MsgId: '6354971394159447836'
//  }

if (message.Content === 'diaosi') {
// 回复屌丝(普通回复)
res.reply('hehe');
} else if (message.Content === 'text') {
//你也可以这样回复text类型的信息
res.reply({
content: 'text object',
type: 'text'
});
} else if (message.Content === 'hehe') {
// 回复一段音乐
res.reply({
type: "music",
content: {
title: "来段音乐吧",
description: "一无所有",
musicUrl: "http://m5.file.xiami.com/884/884/1706253944/1773374767_15591128_l.mp3?auth_key=d26a837c4b087ee74ec512b8206d9dc1-1479870000-0-null",
hqMusicUrl: "http://m5.file.xiami.com/884/884/1706253944/1773374767_15591128_l.mp3?auth_key=d26a837c4b087ee74ec512b8206d9dc1-1479870000-0-null"

}
});
} else {
// 回复高富帅(图文回复)
res.reply([
{
title: '你来我家接我吧',
description: '这是女神与高富帅之间的对话',
picurl: 'http://nodeapi.cloudfoundry.com/qrcode.jpg',
url: 'http://nodeapi.cloudfoundry.com/'
}
]);
}
}));

// 启动server
const server = app.listen(3002, function () {
const host = server.address().address;
const port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});


3.1.3.3. 其他第三方模块的实现

https://cnodejs.org/topic/537974fccbcc3963492b50ff

免责说明

本博客中的文章摘自网上的众多博客,仅作为自己知识的补充和整理,并分享给其他需要的coder,不会用于商用。

因为很多博客的地址看完没有及时做保存,所以很多不会在这里标明出处,非常感谢各位大牛的分享,也希望大家理解。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  wechat-sdk