微信订阅号关注回复

微信订阅号

关注和取消关注的xml

app.all('/wxserver', (req, res) => {
req.on('data',(requestBody)=>{
let xml = requestBody.toString()
console.log(xml)
})
})

会输出三次








1514954008




//unsubscribe是取消关注




解析xml的包 xml2js

作用:
会自动将xml变成对象
{ xml:
{ ToUserName: [ 'gh_2c0117a22e61' ],
FromUserName: [ 'oyugi1EtuG4F6pzRKyEDcVWT1ggc' ],
CreateTime: [ '1514955132' ],
MsgType: [ 'event' ],
Event: [ 'unsubscribe' ],
EventKey: [ '' ] } }
安装:
cnpm i xml2js -S
文档:
npm里面搜xml2js
demo:
app.all('/wxserver', (req, res) => {
req.on('data', (requestBody) => {
//接微信服务器发送过来的xml
let xml = requestBody.toString()
//解析xml
parseString(xml, function (err, result) {
console.dir(result);
});
})
})

判断:
app.all('/wxserver', (req, res) => {
req.on('data', (requestBody) => {
//接微信服务器发送过来的xml
let xml = requestBody.toString()
//解析xml
parseString(xml, function (err, result) {
console.dir(result)
let xml = result.xml
if(xml.MsgType[0] == 'event' && xml.Event[0] == 'subscribe'){
console.log('我被关注了')
}else if(xml.MsgType[0] == 'event' && xml.Event[0] == 'unsubscribe'){
console.log('我被取消关注了')
}
});
})
})

被动回复消息

参考网址:
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140543

关注时自动回复:

if (xml.MsgType[0] == 'event' && xml.Event[0] == 'subscribe') {
let gfxml = <xml> <ToUserName> <![CDATA[${xml.FromUserName}]]> </ToUserName> <FromUserName> <![CDATA[${xml.ToUserName}]]> </FromUserName> <CreateTime>12345678</CreateTime> <MsgType> <![CDATA[text]]> </MsgType> <Content> <![CDATA[谢谢您的关注!]]> </Content> </xml>
res.send(gfxml)
}

注意:
xml是严格标记语言,不允许有空格,得把空格全部去掉

配合mongoDB来返回消息

  1. 抽取xml
    新建一个文件夹proccess-subscribe.js

    'use strict'
    exports.proccess = (result,res)=>{
    let gfxml = <xml> <ToUserName> <![CDATA[${result.FromUserName}]]> </ToUserName> <FromUserName> <![CDATA[${result.ToUserName}]]> </FromUserName> <CreateTime>12345678</CreateTime> <MsgType> <![CDATA[text]]> </MsgType> <Content> <![CDATA[谢谢您的关注!]]> </Content> </xml>
    res.send(gfxml)
    }

  2. 在app,js中用到的地方导入

    if (xml.MsgType[0] == 'event' && xml.Event[0] == 'subscribe') {
    var gzObj = require('./proccess/subscribe.js')
    gzObj.proccess(xml,res)
    }

    3.导入mongodb

    cnpm i mongodb@2.2.33 -S //最新的版本有bug,指的是我现在这个时候的最新版本

    步骤:

    网址:
    https://www.npmjs.com/package/mongodb

    快速使用:

    'use strict'
    const MongoClient = require('mongodb').MongoClient;
    const assert = require('assert');

    exports.proccess = (result, res) => {

    // Connection URL
    const url = 'mongodb://localhost:27017';

    // Database Name
    const dbName = 'myproject';

    // Use connect method to connect to the server
    MongoClient.connect(url, function (err, client) {
    assert.equal(null, err);
    console.log("Connected successfully to server");

    const db = client.db(dbName);

    let gfxml = <xml> <ToUserName> <![CDATA[${result.FromUserName}]]> </ToUserName> <FromUserName> <![CDATA[${result.ToUserName}]]> </FromUserName> <CreateTime>12345678</CreateTime> <MsgType> <![CDATA[text]]> </MsgType> <Content> <![CDATA[谢谢您的关注!]]> </Content> </xml>
    res.send(gfxml)
    //关闭数据库链接
    client.close();
    });
    }

    打开数据库Robo

    在wxMessage表中插入insert document
    {
    'id':1
    'content':'谢谢您的关注'
    }

    后面通过查找,获取到该content

    示例代码:

    'use strict'
    const MongoClient = require('mongodb').MongoClient;
    const assert = require('assert');

    exports.proccess = (result, res) => {

    // Connection URL
    const url = 'mongodb://localhost:27017';

    // Database Name
    const dbName = 'wxdata';

    // Use connect method to connect to the server
    MongoClient.connect(url, function (err, client) {
    assert.equal(null, err);
    console.log("Connected successfully to server");

    const db = client.db(dbName);

    let collection = db.collection('wxMessage')
    collection.findOne({
    id: 1
    }, (err, doc) => {
    let gfxml = <xml> <ToUserName> <![CDATA[${result.FromUserName}]]> </ToUserName> <FromUserName> <![CDATA[${result.ToUserName}]]> </FromUserName> <CreateTime>12345678</CreateTime> <MsgType> <![CDATA[text]]> </MsgType> <Content> <![CDATA[${doc.content}]]> </Content> </xml>
    res.send(gfxml)
    //关闭数据库链接
    client.close();
    })

    });
    }

github地址:

https://github.com/echoorx/wx-

posted on 2018-01-04 12:51  ouruixi  阅读(265)  评论(0)    收藏  举报

导航