node微信自动回复

Posted maoriaty

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了node微信自动回复相关的知识,希望对你有一定的参考价值。

微信号:laoli4316

let express = require(express),
xmlTojs = require(xml2js).parseString;

let app = express();

app.use(/test,function(req,res){
    let buffer = [];
    req.on(data,function(data){
        buffer.push(data);
    });
    req.on(end,function(){
        let msgXml = Buffer.concat(buffer).toString(utf-8);
        xmlTojs(msgXml,{explicitArray:false},function(err,result){
            if(!err){
                result = result.xml;
                console.log(result);
                let fromUser = result.FromUserName,
                toUser = result.ToUserName;
                if(result.MsgType.toLowerCase()==event){
                    switch(result.Event.toLowerCase()){
                        case subscribe:
                                res.send(msg.txtMsg(fromUser,toUser,欢迎您(+fromUser+)的关注!));
                                break;
                    }
                }else if(result.MsgType.toLowerCase()==text){
                    switch(result.Content){
                        case 1:
                            res.send(msg.txtMsg(fromUser,toUser,你个垃圾,贱货));
                            break;
                        case 2:
                            res.send(msg.txtMsg(fromUser,toUser,真尼玛操蛋));
                            break;
                        case zs:
                            let contentArr = [
                                {
                                    Title: you are the beach,
                                    Description: 你就是个逼!,
                                    PicUrl: http://pic.qiantucdn.com/58pic/15/24/50/43Q58PICkj4_1024.jpg!/fw/780/watermark/url/L3dhdGVybWFyay12MS40LnBuZw==/align/center,
                                    Url: http://maoriaty.top
                                },
                                {
                                    Title: 欢迎你个这条母狗,
                                    Description: common baby,
                                    PicUrl: http://g.hiphotos.baidu.com/image/pic/item/023b5bb5c9ea15ce80434db0bc003af33b87b2de.jpg,
                                    Url: http://maoriaty.top
                                }
                            ]
                            res.send(msg.graphicMsg(fromUser,toUser,contentArr));
                            break;
                        default :
                            res.send(msg.txtMsg(fromUser,toUser,格老子的));
                            break;
                    }
                }
            }else{
                console.log(err);
            }
        });
    });
    
})
let msg = {
    txtMsg: function(fromUser,toUser,content){
        let resXml = "<xml>"+
            "<ToUserName><![CDATA["+ fromUser +"]]></ToUserName>"+
            "<FromUserName><![CDATA["+ toUser +"]]></FromUserName>"+
            "<CreateTime>"+ new Date().getTime() +"</CreateTime>"+
            "<MsgType><![CDATA[text]]></MsgType>"+
            "<Content><![CDATA["+ content +"]]></Content></xml>";
        return resXml;
    },
    graphicMsg: function(fromUser,toUser,contentArr){
        let xmlContent =  "<xml><ToUserName><![CDATA["+ fromUser +"]]></ToUserName>";
            xmlContent += "<FromUserName><![CDATA["+ toUser +"]]></FromUserName>";
            xmlContent += "<CreateTime>"+ new Date().getTime() +"</CreateTime>";
            xmlContent += "<MsgType><![CDATA[news]]></MsgType>";
            xmlContent += "<ArticleCount>"+contentArr.length+"</ArticleCount>";
            xmlContent += "<Articles>";
            contentArr.map(function(item,index){
                xmlContent+="<item>";
                xmlContent+="<Title><![CDATA["+ item.Title +"]]></Title>";
                xmlContent+="<Description><![CDATA["+ item.Description +"]]></Description>";
                xmlContent+="<PicUrl><![CDATA["+ item.PicUrl +"]]></PicUrl>";
                xmlContent+="<Url><![CDATA["+ item.Url +"]]></Url>";
                xmlContent+="</item>";
            });
            xmlContent += "</Articles></xml>";
        return xmlContent;
    }
}

let server = app.listen(8888,function(){
    let port = server.address().port;
    console.log("port === " + port);
})

java返回时需注意设置编码utf-8,不然中文乱码

 @RequestMapping(value = "/",produces = "text/xml;charset=utf-8")

 

let express = require(‘express‘),
xmlTojs = require(‘xml2js‘).parseString;

let app = express();

app.use(‘/test‘,function(req,res){
let buffer = [];
req.on(‘data‘,function(data){
buffer.push(data);
});
req.on(‘end‘,function(){
let msgXml = Buffer.concat(buffer).toString(‘utf-8‘);
xmlTojs(msgXml,{explicitArray:false},function(err,result){
if(!err){
result = result.xml;
console.log(result);
let fromUser = result.FromUserName,
toUser = result.ToUserName;
if(result.MsgType.toLowerCase()==‘event‘){
switch(result.Event.toLowerCase()){
case ‘subscribe‘:
res.send(msg.txtMsg(fromUser,toUser,‘欢迎您(‘+fromUser+‘)的关注!‘));
break;
}
}else if(result.MsgType.toLowerCase()==‘text‘){
switch(result.Content){
case ‘1‘:
res.send(msg.txtMsg(fromUser,toUser,‘你个垃圾,贱货‘));
break;
case ‘2‘:
res.send(msg.txtMsg(fromUser,toUser,‘真尼玛操蛋‘));
break;
case ‘zs‘:
let contentArr = [
{
Title: ‘you are the beach‘,
Description: ‘你就是个逼!‘,
PicUrl: ‘http://pic.qiantucdn.com/58pic/15/24/50/43Q58PICkj4_1024.jpg!/fw/780/watermark/url/L3dhdGVybWFyay12MS40LnBuZw==/align/center‘,
Url: ‘http://maoriaty.top‘
},
{
Title: ‘欢迎你个这条母狗‘,
Description: ‘common baby‘,
PicUrl: ‘http://g.hiphotos.baidu.com/image/pic/item/023b5bb5c9ea15ce80434db0bc003af33b87b2de.jpg‘,
Url: ‘http://maoriaty.top‘
}
]
res.send(msg.graphicMsg(fromUser,toUser,contentArr));
break;
default :
res.send(msg.txtMsg(fromUser,toUser,‘格老子的‘));
break;
}
}
}else{
console.log(err);
}
});
});
 
})
let msg = {
‘txtMsg‘: function(fromUser,toUser,content){
let resXml = "<xml>"+
"<ToUserName><![CDATA["+ fromUser +"]]></ToUserName>"+
"<FromUserName><![CDATA["+ toUser +"]]></FromUserName>"+
"<CreateTime>"+ new Date().getTime() +"</CreateTime>"+
"<MsgType><![CDATA[text]]></MsgType>"+
"<Content><![CDATA["+ content +"]]></Content></xml>";
return resXml;
},
‘graphicMsg‘: function(fromUser,toUser,contentArr){
let xmlContent = "<xml><ToUserName><![CDATA["+ fromUser +"]]></ToUserName>";
xmlContent += "<FromUserName><![CDATA["+ toUser +"]]></FromUserName>";
xmlContent += "<CreateTime>"+ new Date().getTime() +"</CreateTime>";
xmlContent += "<MsgType><![CDATA[news]]></MsgType>";
xmlContent += "<ArticleCount>"+contentArr.length+"</ArticleCount>";
xmlContent += "<Articles>";
contentArr.map(function(item,index){
xmlContent+="<item>";
xmlContent+="<Title><![CDATA["+ item.Title +"]]></Title>";
xmlContent+="<Description><![CDATA["+ item.Description +"]]></Description>";
xmlContent+="<PicUrl><![CDATA["+ item.PicUrl +"]]></PicUrl>";
xmlContent+="<Url><![CDATA["+ item.Url +"]]></Url>";
xmlContent+="</item>";
});
xmlContent += "</Articles></xml>";
return xmlContent;
}
}

let server = app.listen(‘8888‘,function(){
let port = server.address().port;
console.log("port === " + port);
})



以上是关于node微信自动回复的主要内容,如果未能解决你的问题,请参考以下文章

如何使用C#建立响应微信接入和自动回复的代码

微信公众号开发之文本消息自动回复,以及系统关注自动回复,php代码

微信开发消息的自动回复和关注后自动回复

使用NodeJs作为微信公众号后台服务器 使用node回复公众号消息 node验证公众号tonken node微信公众号开发

微信自动回复,Python几行代码就搞定了,消息不在错过

python 实现微信自动回复和好友签名分析