Nodejs-based HTTP module for automatic sending and receiving QQ messages via SMARTQQ program

Source: Internet
Author: User

---restore content starts---

Background: February 1 Our laboratory QQ group introduced a chat robot, can achieve check-in, robbery, play games (such as 24 points, guessing puzzles, etc.) and other functions, check-in, robbery success, game victory (such as 24 points to answer the correct) can earn points, winter vacation when no focus on the group so the beginning of their own no points, And the other students have been for the rich side, especially a senior score of 100,000 of the giant, (check-in at about 100, 24 points to answer the correct 100), then puzzled, asked, seniors said: No he, brush the ears. So on April 9, referring to the school of Thought (http://www.zhangzaizai.com/2017/02/08/xiaozi-helper/), began to try to develop a brush sub-auxiliary.

Technology: My backend platform is nodejs, other frameworks are not very deep so use the lowest HTTP module to send post requests. The SMARTQQ that transmits the information in plaintext as the receiving end of the post.

Steps

  1. learn to send only two post requests, one is to send information on behalf of the SEND_QUN_MSG2, you can see in F12 post information is very detailed, then the Nodejs in the scripted.
    var http = require (' http '); exports.post = function (contents) {    var options = {        Host: "D1.web2.qq.com",        Metho D: "POST",        path: "/CHANNEL/SEND_QUN_MSG2",        headers: {            ' Host ': ' d1.web2.qq.com ', '            user-agent ': ' mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) gecko/20100101 firefox/52.0 ',            ' Accept ': ' */* ',            ' accept-language ': ' en-us,en;q=0.5 ',            ' Content-type ': ' application/x-www-form-urlencoded ', '            Referer ': ' https://d1.web2.qq.com/cfproxy.html?v= 20151105001&callback=1 ',            ' content-length ': contents.length,            ' Cookie ': ', */* For security reasons cookie*/            ' Connection ': ' Keep-alive '        }    ;    var req = http.request (options, function (res) {});    Req.write (contents);    Req.end ();};


    I encapsulated it in the module for ease of operation.

  2. However, this seemingly simple code actually has a very big problem, this is I have compiled the code, but at the beginning of my content is not external parameters, but the definition of a JSON (as follows
    var rr = {    "Group_uin": 4111913875,    "content": "[\" start 24 points \ ", [\" font\ ", {\" name\ ": \" arial \ ", \" size\ ": 10,\" style\ " : [0,0,0],\ "color\": \ "000000\"}]] "Face    ":,    "ClientID": 53156849,/* for security reasons to hide the real number, in exchange for the same long number * *    "msg_id ": 62220001,    " Psessionid ":" * * for security reasons psessionid*/};

    ), but the transmission of old failure, later asked seniors, found that the actual transmission of content is not directly JSON, and need to be urlencode encryption, so I called the Nodejs comes with the encodeURI function to transmit the above encryption (), after the success
    var contents2 = encodeURI (' r= ' + json.stringify (RR));

  3. But immediately I ran into another problem, my response is a bunch of garbled, although to send messages to the group does not need to consider the response, but my post is certainly the same way, it means that I accept the message in the group is also a garbled, not to mention processing. This question took me about two days, the first suspicion is charset,utf-8 problem, so in the request head various changes (such as request.setcharacterencoding ("Utf-8")), But there is no use, but also suspected smartqq is not utf-8, a check yes (such as)
  4. Then continue to think, this period consulted a lot of sites, such as (http://blog.csdn.net/zsr_251/article/details/49993911) and then guess the problem in the splicing of buffer, after several setbacks to try various methods (such as
    var buf=buffer.concat (chunks,size); var str=iconv.decode (buf, ' utf-8 ');

    ) or not.
  5. Finally, we're going to have an analysis of the request header information that found this
    ' accept-encoding ': ' gzip, deflate, BR ',

    So seek relevant information, found (http://www.jb51.net/article/61721.htm) This site, try to extract the next response body, such as

6. Then OK, when the screen appears send OK mood is very excited, and then very excited to tell seniors, only listen to seniors a voice "you can send a request for it does not do gzip compression," then deleted to accept-encoding. Send a message this is even successful. I'm sorry for the group I was experimenting with, I didn't mean water.

The next step is to accept the message.

The principle of receiving messages on SMARTQQ is to keep the post poll2 when someone sends you a message, post responds, and the response content is accepted. Then simulate it.

This post is not much different from the above, but note that option's path is changed, and content-length is fixed, option code is as follows,

var options = {    Host: "D1.web2.qq.com",    Method: "POST",    path: "/channel/poll2",/* Note here *    /headers: {        ' Host ': ' d1.web2.qq.com ',        ' user-agent ': ' mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) gecko/20100101 firefox/52.0 ',        ' Accept ': ' */* ',        ' accept-language ': ' en-us,en;q=0.5 ',        ' Content-type ': ' application/x-www-form-urlencoded ', '        Referer ': ' https://d1.web2.qq.com/cfproxy.html?v= 20151105001&callback=1 ',        ' content-length ': ' 395 ',/* I think because of the different parameters you are not necessarily 395, you grab the bag to get the length *        /' Cookie ': ',/ * For security reasons        , cookie*/' Connection ': ' keep-alive '}    ;

The focus is on handling the response. When I enter "Start 24", Little Violet (the robot) will return to this passage: "24 o'clock! Number (total of 6 solutions): 8, 8, 8, 5 the above number subtraction (+,-, *,/) to 24, and the calculation can be listed. such as send: 24 points (4-2) *12/1 ", when I answered correctly will return:" Very tough! The formula is correct. Add 100 points Current figures (total of 20 solutions): 9, 9, 3, 5 The above figures subtraction (+,-, *,/) 24, and the calculation can be listed. If sending: 24 points (4-2) *12/1 ", what I'm going to do is to get four numbers correctly and avoid other information distractions, while not getting the information, i.e. the whole program hangs up when no one is interacting (it will return an HTML page), and the correct string is calculated based on 4 numbers.

  1. starting with the 24-point algorithm, I'm In (http://www.99cankao.com/numbers/ 24game.php) on the Web page directly found the calculation of 24 points of code (such as), the input of the interaction into parameters, and change the return value to a string can be used directly, I encapsulated it in the Calc module convenient to call, the code is as follows.
    function Tdisoper (F0, F1, F2, F3) {this[0] = f0;this[1] = f1;this[2] = f2;this[3] = f3;} Disoper = new Tdisoper ("-", "+", "/", "*"), function oper (f, m, N) {if (f = = = 3) return (M * n); if (f = = 2) return (m/n) if (f = = 1) return (parsefloat (m) + parsefloat (n)), if (f = = 0) return (m-n);} function TB (I1, I2, I4, i8) {this[1] = i1;this[2] = i2;this[4] = i4;this[8] = i8;} function valid (a,b,c,d) {var result = '; n = 1;b = new TB (A, B, C, d); k = 0;var output = ""; for (i1 = 1; I1 <= 8; I1 *= 2) for (i2 = 1; i2 <= 8, I2 *= 2) for (i3 = 1, i3 <= 8; i3 *= 2) for (I4 = 1; I4 <= 8; I4 *= 2) {if (I1 | i2 | i3 | I4)!== 0xf) continue;for (f1 = 0; F1 <= 3; f1++) for (F2 = 0; F2 <= 3; f2++) for (F3 = 0; F3 <= 3; f3++) {m = oper (F3, Oper (F2, Oper (F1, B[i1], B[i2]), [B[i3]), B[i4]), if (Math.Abs (m-24) < 1e-5) {result = result + "((" + b[i1] + dis OPER[F1] + B[i2] + ")" + Disoper[f2] + B[i3] + ")" + disoper[f3 "+ b[i4];if ((n!== 0) && (++k >= N)) return re Sult;} m = oper(F1, B[i1], Oper (F3, Oper (F2, B[i2], B[i3]), B[I4])); if (Math.Abs (m-24) < 1e-5) {result = result + B[i1] + disoper[f1 ] + "((" + b[i2 "+ disoper[f2] + B[i3] +") "+ disoper[f3] + B[i4] +") "if ((n!== 0) && (++k >= N)) return re Sult;} m = Oper (F3, Oper (F1, B[i1], oper (F2, B[i2], B[i3]), B[i4]), if (Math.Abs (m-24) < 1e-5) {result = result + "(" + b[i1 ] + DISOPER[F1] + "(" + B[i2] + DISOPER[F2] + B[i3] + ")" + disoper[f3] + b[i4];if ((n!== 0) && (++k >= N)) r Eturn result;} m = Oper (F1, B[i1], oper (F2, B[i2], Oper (F3, B[i3], B[i4]))); if (Math.Abs (m-24) < 1e-5) {result = result + B[i1] + di SOPER[F1] + "(" + B[i2] + Disoper[f2] + "(" + B[i3] + disoper[f3] + B[i4] + ")"; if ((n!== 0) && (++k >= N)) r Eturn result;} m = Oper (F2, Oper (F1, B[i1], B[i2]), Oper (F3, B[i3], b[i4])); if (Math.Abs (m-24) < 1e-5) {result = result + "(" + b[i1 ] + DISOPER[F1] + B[i2] + ")" + Disoper[f2] + "(" + b[i3 "+ disoper[f3] + B[i4] +") "if ((n!== 0) && (++k >= N)) return result; }}return result;exports.cal = function (a,b,c,d) {return valid (a,b,c,d);};

  2. The response body is processed and sent, the code is as follows, the comments are basically explained, and questions are welcome.
    var req = http.request (options, function (res) {res.on (' data ', function (body) {if (body.tostring (). Search (' &L            T;html> ') = = = 1) {/* Here is to avoid long time not receiving data causing the program to crash directly */var str; str = Json.parse (body.tostring ());/* Parse response body in JSON format */console.log ("Accept message" + str.result[0].value.content);/* Receive content */if (Str.result[0].value.send_uin = = = 8888888/* Here for security reasons: */) {var usethen;/* contains a key 4-digit String */if (str.result[0].value.content.length >= 6)/* Avoid extraneous information interference                */Usethen = str.result[0].value.content[5];                    if (usethen!==undefined) {var use = Usethen.match (/\u89e3[^\n]*\u4ee5/g);/* uses regular expressions to get a string from ' solution ' to ' with ' * *                    var number;  if (use!==undefined) Number = Use[0].match (/[0-9]/g);                    /* Use regular to get 4 important numbers */console.log ("Get Numbers" + number); if (number!== undefined) {var result = Calc.cal(Number[0], number[1], number[2], number[3]);                        Console.log ("Get results" + result);                        var reg =/["][^\n]*[",]/; Rr.content = Rr.content.replace (' Start 24 points ', ' 24 points ' + result);/*rr is the rr*/contents2 = encodeURI (' r= ') above                        + json.stringify (RR)); Contents2 = contents2.replace (' + ', '%2b ');/* Why have these four, I want to put in the following say */contents2 = contents2.replace (' + ', '%                        2b ');                        Contents2 = contents2.replace (' + ', '%2b ');                        Contents2 = contents2.replace (' + ', '%2b ');                    Post.post (CONTENTS2);    }                }            }        }    });    Res.on (' Err ', function (err) {Console.log (err); }); Req.write (contents); Req.end ();

    Why do I have to write four contents = contents.replace (' + ', '%2b '), because the encodeURI function does not convert the special characters such as the plus sign, which leads me to send out the string without a plus sign, here again card me. If there is a better way, hope to have the great God teach me.

  3. Finally, the issue of polling send Poll2, I always have the error display write after end, and later found that I did not give req re-assignment, the equivalent of the request to continue after the write natural error, so the assignment is written in the poll.

At this point, the program is almost compiled, as the Noder is not skilled, naturally there are many flaws, but also hope that the great God can be pointed out.

Nodejs-based HTTP module for automatic sending and receiving QQ messages via SMARTQQ program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.