微信訂閱後臺自動回覆功能的實現

3.1. 訂閱號後臺自動回覆功能的實現

3.1.1. 官網文檔地址

3.1.2. 訂閱號管理平臺的配置

  • 由於微信只支持80端口,因此經過ittun.com搭建模擬80端口的服務器html

    • ittun官網網址:http://www.ittun.com/
    • 下載客戶端文件並解壓縮node

    • 修改startup.bat文件的內容以下 git

    • 雙擊啓動startup.bat文件 github

  • 來到訂閱號後臺並進入開發-基本配置頁面進行配置 web

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模塊的寫法
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. 其餘第三方模塊的實現

免責說明

  1. 本博客中的文章摘自網上的衆多博客,僅做爲本身知識的補充和整理,並分享給其餘須要的coder,不會用於商用。
  2. 由於不少博客的地址看完沒有及時作保存,因此不少不會在這裏標明出處,很是感謝各位大牛的分享,也但願你們理解。
相關文章
相關標籤/搜索