• 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")
     
  • 相关阅读:
    Longest Palindromic Substring
    PayPal MLSE job description
    Continuous Median
    Remove Duplicates From Linked List
    Valid IP Address
    Longest substring without duplication
    Largest range
    Subarray sort
    Multi String Search
    Suffix Trie Construction
  • 原文地址:https://www.cnblogs.com/maoriaty/p/7800766.html
Copyright © 2020-2023  润新知