码迷,mamicode.com
首页 > 微信 > 详细

node微信自动回复

时间:2017-11-07 20:48:27      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:white   term   UI   val   rod   ret   art   time   微信   

微信号: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微信自动回复

标签:white   term   UI   val   rod   ret   art   time   微信   

原文地址:http://www.cnblogs.com/maoriaty/p/7800766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!