node implements wechat code scanning and mass sending message "attach github code"

This article is to introduce how I use node to realize the function of scanning code and group sending. The source code address is at the back

Get login QR code - > scan code to login server

         

First, introduce the main process and attach the key code

1. Get UUID and request QR code picture

Calling interface: https://login.wx.qq.com/jslogin

Return data: code 200 indicates success and uuid

function getUUID(){
         var hreq = https.get('https://login.wx.qq.com/jslogin?appid=wx782c26e4c19acffb&redirect_uri=https%3A%2F%2Fwx.qq.com%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxnewloginpage&fun=new&lang=zh_CN&_=1508239448402',function(ress){  
        ress.setEncoding('utf-8');  
        var str = '';
        ress.on('end',function(){

            var regxp = new RegExp(/^""$/)
            var uuid = str.split('"')[1];
            console.log('uuid=='+uuid)
            var img = '<img src="https://login.weixin.qq.com/qrcode/'+uuid + '"/>'
            res.send(img)
            getTicket(uuid)

        });  
        ress.on('data',function(chunk){
            str+=chunk;
        });  
         });
    }

2. Get ticket through uuid training

Calling interface: https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login

If the return code is 200, it means that the user scans the code. The returned information, such as the ticket of login wechat, is in the redirect URI

function getTicket(uuid){
        var hreq = https.get('https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid='+uuid+'&tip=0&r=-880061546&_='+Date.now(),function(ress){  
        ress.setEncoding('utf-8');  
        var str = '';
        ress.on('end',function(){
            console.log('request ticket. . . . . . ');
            console.log(str);
            var code = str.split(';')[0].split('=')[1];
            console.log('code='+code);
            if(code == 200){
                if(str.match(/wx2.qq.com/) != null) {
                    wx2 = "2";
                    headers.Host = "wx2.qq.com";
                    headers.Referer = "https://wx2.qq.com/"
                }
                var ticket = str.split('ticket=')[1].split('&uuid')[0];
                console.log('ticket=' + ticket);
                getPassTicket(ticket,uuid);
            }else if(code == 408 || code==201){
                getTicket(uuid);
            }else{
                console.log(str);
                console.log('overtime');
            }
            
        });  
        ress.on('data',function(chunk){
            str+=chunk;
        });  
         });
    }

 

3. Get the wechat login unique flag information wxsid, skey, pass_ticket

Calling interface: https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage

Return the information wxsid, skey, pass ﹐ ticket, and the cookie will be planted

All subsequent requests for information obtained at this step will go smoothly

function getPassTicket(ticket,uuid){
        request.get({
          url:'https://wx'+wx2+'.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage?ticket='+ticket+'&uuid='+uuid+'&lang=zh_CN&scan='+parseInt(Date.now())+'&fun=new&version=v2&lang=zh_CN',
        }, function(error, response, body){
            console.log('request PassTicket------->>>>>>>>');
            console.log(body);
            var str = body.toString();
        pass_ticket = str.split('<pass_ticket>')[1].split('</pass_ticket>')[0];
        skey = str.split('<skey>')[1].split('</skey>')[0];
        sid = str.split('<wxsid>')[1].split('</wxsid>')[0];
        uin = str.split('<wxuin>')[1].split('</wxuin>')[0];
        setCookie(response.headers['set-cookie'])
        wxInit()
        });
    }

 

4. Get the contact list and send messages in groups

Contact list calling interface: https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact

Return the contact information of all users. Each user has a unique ID in this login

 

Send message calling interface: https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg

If BaseResponse.ErrorMsg is empty, the message is sent successfully

function getAllUsers(){
        request.get({
            headers: headers,
          url:'https://wx'+wx2+'.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact?lang=zh_CN&pass_ticket='+pass_ticket+'&r='+Date.now()+'&seq=0&skey='+skey
        }, function(error, response, body){
            console.log('getAllUsers..........')
            console.log(body)
          var list = JSON.parse(body).MemberList;
          console.log(list.length)
          for (var i = 0; i < list.length; i++) {
              var member = list[i];
                console.log(member.NickName,member.UserName);
                // Be careful when sending messages in groups
              // if(member.NickName == 'North wind blowing snow') {
                   // postMsg(myUserName,member.UserName,'Message content');
                   // break;
              //  }
          }
        });
    }

Source code GitHub address https://github.com/ColdDay/wxPro (if it helps you, please give a star A kind of , only when the stars are in place can they have more interesting functions.)

Tags: Javascript github QRCode JSON

Posted on Sun, 03 May 2020 20:04:40 -0400 by Calimero