WX Robot

https://github.com/TonyChen56/WeChatRobothtml

https://blog.csdn.net/github_33226586/article/details/50717089git

 

微信web協議分析和實現微信機器人(微信網頁版 wx2.qq.com)
1.打開首頁,分配一個隨機uuid,
2.根據該uuid獲取二維碼圖片。
3.微信客戶端掃描該圖片,在客戶端確認登陸。
4.瀏覽器不停的調用一個接口,若是返回登陸成功,則調用登陸接口
5.此時能夠獲取聯繫人列表,能夠發送消息。而後不斷調用同步接口。
6.若是同步接口有返回,則能夠獲取新消息,而後繼續調用同步接口。github

實現源碼:https://github.com/biezhi/wechat-robotweb

執行流程
+--------------+ +---------------+ +---------------+
| | | | | |
| 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|
+-------------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
WebWechat API
1. 獲取UUID(參考方法 getUUID)
API 獲取 UUID
url https://login.weixin.qq.com/jslogin
method GET
data URL Encode
params appid : wx782c26e4c19acffb
fun : new
lang: zh_CN
_ : 時間戳
返回數據(String):json

window.QRLogin.code = 200; window.QRLogin.uuid = "xxx"
1
2. 顯示二維碼(參考方法 showQrCode)
API 顯示二維碼
url https://login.weixin.qq.com/qrcode/{uuid}
method POST
params t : webwx
_ : 時間戳瀏覽器


3. 等待登陸(參考方法 waitForLogin)這裏是微信確認登陸
API 二維碼掃描登陸
url https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login
method GET
params tip : 1:未掃描 0:已掃描
uuid : 獲取到的uuid
_ : 時間戳
返回數據(String):微信

window.code=xxx;app

xxx:
408 登錄超時
201 掃描成功
200 確認登陸ide

當返回200時,還會有
window.redirect_uri="https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage?ticket=xxx&uuid=xxx&lang=xxx&scan=xxx";
1
2
3
4
5
6
7
8
9
4. 登陸獲取Cookie(參考方法 login)
API webwxnewloginpage
url https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage
method GET
params ticket : xxx
uuid : xxx
lang : zh_CN
scan : xxx
fun : new
返回數據(XML):ui

<error>
<ret>0</ret>
<message>OK</message>
<skey>xxx</skey>
<wxsid>xxx</wxsid>
<wxuin>xxx</wxuin>
<pass_ticket>xxx</pass_ticket>
<isgrayscale>1</isgrayscale>
</error>
1
2
3
4
5
6
7
8
9
在這一步獲取xml中的 skey, wxsid, wxuin, pass_ticket

5. 微信初始化(參考方法 wxInit)
API webwxinit
url https://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,
     }
}
返回數據(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
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
這一步中獲取 SyncKey, User 後面的消息監聽用。

6. 開啓微信狀態通知(參考方法 wxStatusNotify)
API webwxstatusnotify
url https://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: 本身的ID,
     ToUserName: 本身的ID,
     ClientMsgId: 時間戳
}
返回數據(JSON):

{
"BaseResponse": {
"Ret": 0,
"ErrMsg": ""
},
...
}
1
2
3
4
5
6
7
7. 獲取聯繫人列表(參考方法 getContact)
API webwxgetcontact
url https://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,
     }
}
返回數據(JSON):

{
"BaseResponse": {
"Ret": 0,
"ErrMsg": ""
},
"MemberCount": 334,
"MemberList": [
{
"Uin": 0,
"UserName": xxx,
"NickName": "Urinx",
"HeadImgUrl": xxx,
"ContactFlag": 3,
"MemberCount": 0,
"MemberList": [],
"RemarkName": "",
"HideInputBarFlag": 0,
"Sex": 0,
"Signature": "我是二蛋",
"VerifyFlag": 8,
"OwnerUin": 0,
"PYInitial": "URINX",
"PYQuanPin": "Urinx",
"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
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
8.消息檢查(參考方法 syncCheck)
API synccheck
url https://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,
     }
}
返回數據(String):

window.synccheck={retcode:"xxx",selector:"xxx"}

retcode:
0 正常
1100 失敗/登出微信
selector:
0 正常
2 新的消息
7 進入/離開聊天界面
1
2
3
4
5
6
7
8
9
9. 獲取最新消息(參考方法 webwxsync)
API webwxsync
url https://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: 時間戳取反
}
返回數據(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,
...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
10. 發送消息(參考方法 webwxsendmsg)
API webwxsendmsg
url https://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 文字消息,
         Content: 要發送的消息,
         FromUserName: 本身的ID,
         ToUserName: 好友的ID,
         LocalID: 與clientMsgId相同,
         ClientMsgId: 時間戳左移4位隨後補上4位隨機數
     }
}
返回數據(JSON):

{
"BaseResponse": {
"Ret": 0,
"ErrMsg": ""
},
...
}
1
2
3
4
5
6
7
更多資料:
https://github.com/xiangzhai/qwx
https://github.com/Urinx/WeixinBot
http://www.07net01.com/2016/01/1201188.html
http://www.cnblogs.com/xiaozhi_5638/p/4923811.html

點贊 4
收藏
分享

————————————————版權聲明:本文爲CSDN博主「王爵的csdn」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連接及本聲明。原文連接:https://blog.csdn.net/github_33226586/article/details/50717089