WeChat protocol details

Source: Internet
Author: User
1. open the home page and assign a random uuid. 2. obtain the QR code image based on the uuid. 3. the client scans the image and confirms logon on the client. 4. the browser keeps calling an interface. if logon is successful, the interface is called. 5. You can obtain the contact list and send messages. Then, call the synchronous interface continuously. 6. if the synchronous interface returns a message, you can obtain the new message and continue to call the synchronous interface. Web protocol analysis (web version wx2.qq.com)

1. open the home page and assign a random uuid,
2. obtain the QR code image based on the uuid.
3. the client scans the image and confirms logon on the client.
4. the browser keeps calling an interface. if logon is successful, the interface is called.
5. You can obtain the contact list and send messages. Then, call the synchronous interface continuously.
6. if the synchronous interface returns a message, you can obtain the new message and continue to call the synchronous interface.

Source code address: github.com/biezhi/wechat-robot

Execution process
       +--------------+     +---------------+   +---------------+       |              |     |               |   |               |       |   Get UUID   |     |  Get Contact  |   | Status Notify |       |              |     |               |   |               |       +-------+------+     +-------^-------+   +-------^-------+               |                    |                   |               |                    +-------+  +--------+               |                            |  |       +-------v------+               +-----+--+------+      +--------------+       |              |               |               |      |              |       |  Get QRCode  |               |  Weixin Init  +------>  Sync Check  <----+       |              |               |               |      |              |    |       +-------+------+               +-------^-------+      +-------+------+    |               |                              |                      |           |               |                              |                      +-----------+               |                              |                      |       +-------v------+               +-------+--------+     +-------v-------+       |              | Confirm Login |                |     |               |+------>    Login     +---------------> New Login Page |     |  Weixin Sync  ||      |              |               |                |     |               ||      +------+-------+               +----------------+     +---------------+|             ||QRCode Scaned|+-------------+
WebWechat API1. obtain UUID (refer to getUUID)
API Obtain UUID
Url Https://login.weixin.qq.com/jslogin
Method GET
Data URL Encode
Params Appid: Wx782c26e4c19acffb
Fun: New
Lang: Zh_CN
_: Timestamp

Returned data (String ):

window.QRLogin.code = 200; window.QRLogin.uuid = "xxx"
2. display the QR code (refer to showQrCode)
API Display QR code
Url Login.weixin.qq.com/qrcode/{uuid}
Method POST
Params T: Webwx
_: Timestamp

3. wait for logon (refer to the waitForLogin method). here is the logon confirmation.
API QR code scan logon
Url Login.weixin.qq.com/cgi-bin/mmwebwx-bin/login
Method GET
Params Tip: 1: not scanned 0: scanned
Uuid: Uuid obtained
_: Timestamp

Returned data (String ):

Window. code = xxx; xxx: 408 login timeout 201 scan successful 200 confirm login there will be window. redirect_uri = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage? Ticket = xxx & uuid = xxx & lang = xxx & scan = xxx ";
4. log on to obtain the Cookie (refer to login)
API Webwxnewloginpage
Url Wx2.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage
Method GET
Params Ticket: Xxx
Uuid: Xxx
Lang: Zh_CN
Scan: Xxx
Fun: New

Returned data (XML ):

     
  
   0
      
  
   OK
      
  
   xxx
      
  
   xxx
      
  
   xxx
      
  
   xxx
      
  
   1
  
 

In this step, obtainskey,wxsid,wxuin,pass_ticket

5. initialization (refer to wxInit)
API Webwxinit
Url Wx2.qq.com/cgi-bin/mmwebwx-bin/webwxinit
Method POST
Data JSON
Header Content-Type: application/json; charset = UTF-8
Params {
BaseRequest :{
Uin: xxx,
Sid: xxx,
Skey: xxx,
DeviceID: xxx,
}
}

Returned data (JSON ):

{    "BaseResponse": {        "Ret": 0,        "ErrMsg": ""    },    "Count": 11,    "ContactList": [...],    "SyncKey": {        "Count": 4,        "List": [            {                "Key": 1,                "Val": 635705559            },            ...        ]    },    "User": {        "Uin": xxx,        "UserName": xxx,        "NickName": xxx,        "HeadImgUrl": xxx,        "RemarkName": "",        "PYInitial": "",        "PYQuanPin": "",        "RemarkPYInitial": "",        "RemarkPYQuanPin": "",        "HideInputBarFlag": 0,        "StarFriend": 0,        "Sex": 1,        "Signature": "Apt-get install B",        "AppAccountFlag": 0,        "VerifyFlag": 0,        "ContactFlag": 0,        "WebWxPluginSwitch": 0,        "HeadImgFlag": 1,        "SnsFlag": 17    },    "ChatSet": xxx,    "SKey": xxx,    "ClientVersion": 369297683,    "SystemTime": 1453124908,    "GrayScale": 1,    "InviteStartCount": 40,    "MPSubscribeMsgCount": 2,    "MPSubscribeMsgList": [...],    "ClickReportInterval": 600000}

Obtained in this stepSyncKey,UserFor later message listening.

6. enable status notification (refer to wxStatusNotify)
API Webwxstatusnotify
Url Wx2.qq.com/cgi-bin/mmwebwx-bin/webwxstatusnotify
Method POST
Data JSON
Header Content-Type: application/json; charset = UTF-8
Params {
BaseRequest: {Uin: xxx, Sid: xxx, Skey: xxx, DeviceID: xxx },
Code: 3,
FromUserName: Your own ID,
ToUserName: Your own ID,
ClientMsgId: Timestamp
}

Returned data (JSON ):

{    "BaseResponse": {        "Ret": 0,        "ErrMsg": ""    },    ...}
7. obtain the contact list (refer to the getContact method)
API Webwxgetcontact
Url Wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact
Method POST
Data JSON
Header ContentType: application/json; charset = UTF-8
Params {
BaseRequest :{
Uin: xxx,
Sid: xxx,
Skey: xxx,
DeviceID: xxx,
}
}

Returned data (JSON ):

{"BaseResponse": {"Ret": 0, "ErrMsg": ""}, "MemberCount": 334, "MemberList": [{"Uin": 0, "UserName": xxx, "NickName": "urrent"," HeadImgUrl ": xxx," ContactFlag ": 3," MemberCount ": 0," MemberList ": [], "RemarkName": "", "HideInputBarFlag": 0, "Sex": 0, "Signature": "I am an egg", "VerifyFlag": 8, "OwnerUin ": 0, "PYInitial": "urrent"," PYQuanPin ":" urrent", "RemarkPYInitial": "", "RemarkPYQuanPin": "", "StarFriend": 0, "AppAccountFlag": 0, "Statues": 0, "AttrStatus": 0, "Province": "", "City": "", "Alias": "Urinxs ", "SnsFlag": 0, "UniFriend": 0, "DisplayName": "", "ChatRoomId": 0, "KeyWord": "gh _", "EncryChatRoomId ": ""},...], "Seq": 0}
8. message check (refer to syncCheck)
API Synccheck
Url Webpush2.weixin.qq.com/cgi-bin/mmwebwx-bin/synccheck
Method GET
Data JSON
Header ContentType: application/json; charset = UTF-8
Params {
BaseRequest :{
Uin: xxx,
Sid: xxx,
Skey: xxx,
DeviceID: xxx,
}
}

Returned data (String ):

Window. synccheck = {retcode: "xxx", selector: "xxx"} retcode: 0 normal 1100 failed/logout selector: 0 normal 2 New Message 7 enter/exit chat interface
9. Get the latest message (refer to webwxsync)
API Webwxsync
Url Wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsync? Sid = xxx & skey = xxx & pass_ticket = xxx
Method POST
Data JSON
Header ContentType: application/json; charset = UTF-8
Params {
BaseRequest: {Uin: xxx, Sid: xxx, Skey: xxx, DeviceID: xxx },
SyncKey: xxx,
Rr:Reverse timestamp
}

Returned data (JSON ):

{    'BaseResponse': {'ErrMsg': '', 'Ret': 0},    'SyncKey': {        'Count': 7,        'List': [            {'Val': 636214192, 'Key': 1},            ...        ]    },    'ContinueFlag': 0,    'AddMsgCount': 1,    'AddMsgList': [        {            'FromUserName': '',            'PlayLength': 0,            'RecommendInfo': {...},            'Content': "",             'StatusNotifyUserName': '',            'StatusNotifyCode': 5,            'Status': 3,            'VoiceLength': 0,            'ToUserName': '',            'ForwardFlag': 0,            'AppMsgType': 0,            'AppInfo': {'Type': 0, 'AppID': ''},            'Url': '',            'ImgStatus': 1,            'MsgType': 51,            'ImgHeight': 0,            'MediaId': '',             'FileName': '',            'FileSize': '',            ...        },        ...    ],    'ModChatRoomMemberCount': 0,    'ModContactList': [],    'DelContactList': [],    'ModChatRoomMemberList': [],    'DelContactCount': 0,    ...}
10. send messages (refer to webwxsendmsg)
API Webwxsendmsg
Url Wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg? Pass_ticket = xxx
Method POST
Data JSON
Header ContentType: application/json; charset = UTF-8
Params {
BaseRequest: {Uin: xxx, Sid: xxx, Skey: xxx, DeviceID: xxx },
Msg :{
Type: 1 text message,
Content: The message to be sent,
FromUserName: Your own ID,
ToUserName: Friend ID,
LocalID: same as clientMsgId,
ClientMsgId: shifts the timestamp left by four digits and then adds a random number of four digits.
}
}

Returned data (JSON ):

{    "BaseResponse": {        "Ret": 0,        "ErrMsg": ""    },    ...}

More information:
Github.com/xiangzhai/qwx
Github.com/Urinx/WeixinBot
Www.07net01.com/2016/01/1201188.html
Www.cnblogs.com/xiaozhi_5638/p/4103811.html

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.