[监控] 实时消息接收发送
[TOC]
简要描述
- 接收实时消息, 本接口需自行搭建。用来接收微信实时消息。
如果没有指定消息回调URL。默认发送给以下URL地址
http://127.0.0.1:30000/api/user/recive_msg
发送方式
- POST
发送参数 当https://www.showdoc.com.cn/ebaiwl/9204125262722344 接口传递参数为2 时
{
"ServerPort":"30001",
"selfwxid":"wxid_123456",
"sendorrecv":"2",
"pb_data":"0A058C6EA223445F1C...",
}
发送参数 当https://www.showdoc.com.cn/ebaiwl/9204125262722344 接口传递参数为1 时
PC端 发送的消息
PC发出文本消息 第一次 发送的内容
{
"ServerPort":"30001",
"selfwxid":"themid",
"sendorrecv":"1",
"msgnumber":"1",
"msglist":[{
"index":"1",
"time":"2023-03-05 13:07:54",
"msgtype":"1",
"msgsvrid":"",
"msg":"大佬666~",
"fromwxid":"themid",
"toid":"filehelper"
}]
}
第二次 发送成功后 收到 服务器返回的消息ID (msgid)
{
"ServerPort":"30001",
"selfwxid":"themid",
"sendorrecv":"2",
"msgnumber":"1",
"msglist":[{
"index":"1",
"time":"2023-03-05 13:07:54",
"msgtype":"1",
"msgsvrid":"7029814402633277009",
"msg":"PC发文本消息成功",
"fromwxid":"themid",
"toid":"filehelper"
}]
}
PC发出图片消息 第一次 发送的内容
{
"ServerPort":"30001",
"selfwxid":"themid",
"sendorrecv":"1",
"msgnumber":"0",
"msglist":[{
"index":"1",
"time":"2023-03-05 13:09:19",
"msgtype":"3",
"msgsvrid":"",
"msg":"8bb306d0ad78a2aa8cafe229dc304f38",
"fromwxid":"themid",
"toid":"filehelper",
"img_path":"C:\\\\Users\\\\Administrator\\\\Documents\\\\WeChat Files\\\\thexed\\\\FileStorage\\\\Temp\\\\1677992958058.jpg"
}]
}
第二次 发送成功后 收到 服务器返回的消息ID (msgid)
{
"ServerPort":"30001",
"selfwxid":"themid",
"sendorrecv":"2",
"msgnumber":"1",
"msglist":[{
"index":"1",
"time":"2023-03-05 13:09:20",
"msgtype":"3",
"msgsvrid":"8676825236427018921",
"msg":"PC发图片消息成功",
"fromwxid":"themid",
"toid":"filehelper"
}]
}
PC发出文件/app消息 第一次 发送的内容
{
"ServerPort":"30001",
"selfwxid":"themid",
"sendorrecv":"1",
"msgnumber":"0",
"msglist":[{
"index":"1",
"time":"2023-03-05 13:11:27",
"msgtype":"49",
"msgsvrid":"",
"msg":"version.dll",
"fromwxid":"themid",
"toid":"filehelper",
"file_path":"C:\\\\Users\\\\Administrator\\\\Desktop\\\\version.dll"
}]
}
第二次 发送成功后 收到 服务器返回的消息ID (msgid)
{
"ServerPort":"30001",
"selfwxid":"themid",
"sendorrecv":"2",
"msgnumber":"1",
"msglist":[{
"index":"1",
"time":"2023-03-05 13:11:30",
"msgtype":"99",
"msgsvrid":"476728224034532762",
"msg":"PC发app/文件消息成功",
"fromwxid":"themid",
"toid":"filehelper"
}]
}
PC发出视频消息 第一次 发送的内容
{
"ServerPort":"30001",
"selfwxid":"themid",
"sendorrecv":"1",
"msgnumber":"0",
"msglist":[{
"index":"1",
"time":"2023-03-05 13:14:07",
"msgtype":"4",
"msgsvrid":"",
"msg":"3057020100044b30490201000204911740bc02032f77f50204ab5b90db02046404251f042438373463386566632d396161312d346263642d383838392d3335613736613865396634610204011400030201000405004c550500",
"fromwxid":"themid",
"toid":"filehelper",
"video_path":"C:\\\\Windows\\\\SystemApps\\\\Microsoft.Windows.CloudExperienceHost_cw5n1h2txyewy\\\\media\\\\oobe-intro.mp4"
}]
}
第二次 发送成功后 收到 服务器返回的消息ID (msgid)
{
"ServerPort":"30001",
"selfwxid":"themid",
"sendorrecv":"2",
"msgnumber":"1",
"msglist":[{
"index":"1",
"time":"2023-03-05 13:14:11",
"msgtype":"36",
"msgsvrid":"3847230082234559699",
"msg":"PC发视频文件成功",
"fromwxid":"themid",
"toid":""
}]
}
切换聊天对象
{
"ServerPort":"30001",
"selfwxid":"themid",
"sendorrecv":"1",
"msgnumber":"1",
"msglist":[{
"index":"1",
"time":"2023-03-05 13:04:26",
"msgtype":"10000",
"msgsvrid":"",
"msg":"切换聊天对象",
"current_wxid":"filehelper",
"name":"文件传输助手",
"nickname":""
}]
}
PC端 收到的消息(文字)
{
"ServerPort":"30001",
"selfwxid":"themid",
"sendorrecv":"2",
"msgnumber":"1",
"msglist":[{
"index":"1",
"time":"2023-03-14 11:18:07",
"msgtype":"1",
"msgsvrid":"2936072064472736442",
"msg":"wxid_kqi...:\n111",
"fromtype":"2",
"fromgid":"438191..@chatroom",
"toid":"themid"
}]
}
{
"ServerPort":"30001",
"selfwxid":"themid",
"sendorrecv":"2",
"msgnumber":"1",
"msglist":[{
"index":"1",
"time":"2023-03-14 11:18:07",
"msgtype":"1",
"msgsvrid":"13430535177462202152",
"msg":"111",
"fromtype":"2",
"fromgid":"438..@chatroom",
"fromid":"wxid_k..",
"toid":"themid"
}]
}
PC端 收到的消息(图片)
{
"ServerPort":"30001",
"selfwxid":"themid",
"sendorrecv":"2",
"msgnumber":"2",
"msglist":[{
"index":"1",
"time":"2023-03-05 13:25:47",
"msgtype":"51",
"msgsvrid":"6229442408328493980",
"msg":"<msg>\n<op id='1'>\n<username>wxid_kq...</username>\n<name>lastMessage</name>\n<arg>{\"messageSvrId\":\"0\",\"MsgCreateTime\":\"1677993947\"}</arg>\n</op>\n</msg>",
"fromwxid":"themid",
"toid":"wxid_kq..."
},{
"index":"2",
"time":"2023-03-05 13:25:47",
"msgtype":"3",
"msgsvrid":"1205604146658880283",
"msg":"<?xml version=\"1.0\"?>\n<msg>\n\t<img aeskey=\"7c2724b850f88a..f40631abe\" encryver=\"1\" cdnthumbaeskey=\"7c2...f40631abe\" cdnthumburl=\"3057020..4640427db042461656539336534642d376661302d343136312d3..3738313836020401150a020201000405004c4f2900\" cdnthumblength=\"1994\" cdnthumbheight=\"120\" cdnthumbwidth=\"67\" cdnmidheight=\"0\" cdnmidwidth=\"0\" cdnhdheight=\"0\" cdnhdwidth=\"0\" cdnmidimgurl=\"3057020100..0a0202c4f2900\" length=\"273129\" md5=\"67025..90cc261\" hevc_mid_size=\"273129\" />\n\t<platform_signature></platform_signature>\n\t<imgdatahash></imgdatahash>\n</msg>\n",
"img_path":"C:\\Users\\Administrator\\Documents\\WeChat Files\\themid\\FileStorage\\MsgAttach\\81a7f2695cd..599f\\Image\\2023-03\\e0f480bb..bfdd1e634.dat",
"fromwxid":"themid",
"toid":"wxid_kq.."
}]
}
{
"ServerPort":"30001",
"selfwxid":"themid",
"sendorrecv":"2",
"msgsvrid":"1957754625548008208",
"img_path":"C:\\Users\\Administrator\\Documents\\WeChat Files\\themid\\FileStorage\\MsgAttach\\81a7..599f\\Image\\2023-03\\ba649..df2.dat",
"time":"2023-03-05 13:25:47",
"msg":"<?xml version=\"1.0\"?>\n<msg>\n\t<img aeskey=\"7c2724b..1abe\" encryver=\"1\" cdnthumbaeskey=\"7c272..abe\" cdnthumburl=\"3057020..c4f2900\" cdnthumblength=\"1994\" cdnthumbheight=\"120\" cdnthumbwidth=\"67\" cdnmidheight=\"0\" cdnmidwidth=\"0\" cdnhdheight=\"0\" cdnhdwidth=\"0\" cdnmidimgurl=\"3057020..2900\" length=\"273129\" md5=\"67..c261\" hevc_mid_size=\"273129\" />\n\t<platform_signature></platform_signature>\n\t<imgdatahash></imgdatahash>\n</msg>\n"
}
PC端 收到的消息(GIF动图)
{
"ServerPort":"30001",
"selfwxid":"themid",
"sendorrecv":"2",
"msgnumber":"1",
"msglist":[{
"index":"1",
"time":"2022-12-22 18:15:08",
"msgtype":"47",
"msgsvrid":"9168432254856816012",
"msg":"<msg><emoji fromusername = \"thekid\" tousername = \"themid\" type=\"2\" idbuffer=\"media:0_0\" md5=\"97365663bee2eb41ea0b3ad50cbe9dd2\" len = \"439919\" productid=\"\" androidmd5=\"97365663bee2eb41ea0b3ad50cbe9dd2\" androidlen=\"439919\" s60v3md5 = \"97365663bee2eb41ea0b3ad50cbe9dd2\" s60v3len=\"439919\" s60v5md5 = \"97365663bee2eb41ea0b3ad50cbe9dd2\" s60v5len=\"439919\" cdnurl = \"http://vweixinf.tc.qq.com/110/20401/stodownload?m=97365663bee2eb41ea0b3ad50cbe9dd2&filekey=30440201010430302e02016e0402535a04203937333635363633626565326562343165613062336164353063626539646432020306b66f040d00000004627466730000000131&hy=SZ&storeid=323032313035303731393134333530303033653932336261623532663833353335383538303930303030303036653031303034666231&ef=1&bizid=1022\" designerid = \"\" thumburl = \"\" encrypturl = \"http://vweixinf.tc.qq.com/110/20402/stodownload?m=ea1bd1936ce3281e92a72ed45653a768&filekey=30440201010430302e02016e0402535a04206561316264313933366365333238316539326137326564343536353361373638020306b670040d00000004627466730000000131&hy=SZ&storeid=323032313035303731393134333530303034376139626261623532663833353335383538303930303030303036653032303034666232&ef=2&bizid=1022\" aeskey= \"658d8cd15a6d4573990cd63ebb0d4e70\" externurl = \"http://vweixinf.tc.qq.com/110/20403/stodownload?m=bf191d0d4bc5d7de00a08b16960c3c74&filekey=30440201010430302e02016e0402535a0420626631393164306434626335643764653030613038623136393630633363373402030422c0040d00000004627466730000000131&hy=SZ&storeid=323032313035303731393134333530303036353639326261623532663833353335383538303930303030303036653033303034666233&ef=3&bizid=1022\" externmd5 = \"870b23321c54bce14f843a037a746989\" width= \"512\" height= \"512\" tpurl= \"\" tpauthkey= \"\" attachedtext= \"\" attachedtextcolor= \"\" lensid= \"\" emojiattr= \"\" linkid= \"\" desc= \"\" ></emoji> <gameext type=\"0\" content=\"0\" ></gameext></msg>",
"gif_url":"http://vweixinf.tc.qq.com/110/20401/stodownload?m=97365663bee2eb41ea0b3ad50cbe9dd2&filekey=30440201010430302e02016e0402535a04203937333635363633626565326562343165613062336164353063626539646432020306b66f040d00000004627466730000000131&hy=SZ&storeid=323032313035303731393134333530303033653932336261623532663833353335383538303930303030303036653031303034666231&ef=1&bizid=1022",
"fromwxid":"thekid",
"toid":"themid",
"tonickname":"",
"toname":"",
"tohead":""
}]
}
{
"ServerPort":"30001",
"selfwxid":"themid",
"sendorrecv":"2",
"msgsvrid":"10116766129219189887",
"gif_url":"http://vweixinf.tc.qq.com/110/20401/stodownload?m=97365663bee2eb41ea0b3ad50cbe9dd2&filekey=30440201010430302e02016e0402535a04203937333635363633626565326562343165613062336164353063626539646432020306b66f040d00000004627466730000000131&hy=SZ&storeid=323032313035303731393134333530303033653932336261623532663833353335383538303930303030303036653031303034666231&ef=1&bizid=1022",
"time":"2022-12-22 18:15:08",
"msg":"<msg><emoji fromusername = \"thekid\" tousername = \"themid\" type=\"2\" idbuffer=\"media:0_0\" md5=\"97365663bee2eb41ea0b3ad50cbe9dd2\" len = \"439919\" productid=\"\" androidmd5=\"97365663bee2eb41ea0b3ad50cbe9dd2\" androidlen=\"439919\" s60v3md5 = \"97365663bee2eb41ea0b3ad50cbe9dd2\" s60v3len=\"439919\" s60v5md5 = \"97365663bee2eb41ea0b3ad50cbe9dd2\" s60v5len=\"439919\" cdnurl = \"http://vweixinf.tc.qq.com/110/20401/stodownload?m=97365663bee2eb41ea0b3ad50cbe9dd2&filekey=30440201010430302e02016e0402535a04203937333635363633626565326562343165613062336164353063626539646432020306b66f040d00000004627466730000000131&hy=SZ&storeid=323032313035303731393134333530303033653932336261623532663833353335383538303930303030303036653031303034666231&ef=1&bizid=1022\" designerid = \"\" thumburl = \"\" encrypturl = \"http://vweixinf.tc.qq.com/110/20402/stodownload?m=ea1bd1936ce3281e92a72ed45653a768&filekey=30440201010430302e02016e0402535a04206561316264313933366365333238316539326137326564343536353361373638020306b670040d00000004627466730000000131&hy=SZ&storeid=323032313035303731393134333530303034376139626261623532663833353335383538303930303030303036653032303034666232&ef=2&bizid=1022\" aeskey= \"658d8cd15a6d4573990cd63ebb0d4e70\" externurl = \"http://vweixinf.tc.qq.com/110/20403/stodownload?m=bf191d0d4bc5d7de00a08b16960c3c74&filekey=30440201010430302e02016e0402535a0420626631393164306434626335643764653030613038623136393630633363373402030422c0040d00000004627466730000000131&hy=SZ&storeid=323032313035303731393134333530303036353639326261623532663833353335383538303930303030303036653033303034666233&ef=3&bizid=1022\" externmd5 = \"870b23321c54bce14f843a037a746989\" width= \"512\" height= \"512\" tpurl= \"\" tpauthkey= \"\" attachedtext= \"\" attachedtextcolor= \"\" lensid= \"\" emojiattr= \"\" linkid= \"\" desc= \"\" ></emoji> <gameext type=\"0\" content=\"0\" ></gameext></msg>"
}
PC端 收到的消息(语音消息)
{
"ServerPort":"30001",
"selfwxid":"themid",
"sendorrecv":"2",
"msgnumber":"1",
"msglist":[{
"index":"1",
"time":"2023-03-05 13:19:05",
"msgtype":"34",
"msgsvrid":"3921037281184911662",
"msg":"<msg><voicemsg endflag=\"1\" cancelflag=\"0\" forwardflag=\"0\" voiceformat=\"4\" voicelength=\"1980\" length=\"2972\" bufid=\"0\" aeskey=\"18987a920fd...ae5b2b6a245\" voiceurl=\"305202010004...4000f0201000400\" voicemd5=\"\" clientmsgid=\"49026462e24bee876...3cb70cthemid_19_1677993542\" fromusername=\"wxid_kqi...\" /></msg>",
"voip_data":"02232153494C4B5F56331200A738B8AACFF7B149A2093860..",
"fromwxid":"wxid_kqi..2",
"toid":"themid"
}]
}
PC端 收到的消息(退出登陆微信)
{
"ServerPort":"30001",
"selfwxid":"themid",
"sendorrecv":"1",
"msgnumber":"1",
"msglist":[{
"index":"1",
"time":"2023-03-05 13:23:06",
"msgtype":"10000",
"msgsvrid":"",
"msg":"退出登录微信"
}]
}
PC端 拉人进群通知
{
"ServerPort":"30001",
"selfwxid":"wxid_kqis...",
"sendorrecv":"2",
"msgnumber":"1",
"msglist":[{
"index":"1",
"time":"2023-01-09 17:23:26",
"msgtype":"20001",
"msg":"Everything is OK",
"gid":"43202784070@chatroom"
}]
}
参数说明
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
ServerPort | 是 | string | 发送消息的端口默认30001,如果是30002则代表第二个微信 |
selfwxid | 是 | string | 本人微信ID |
sendorrecv | 是 | string | 1=收到的消息 </br>2=发送的消息 |
msgnumber | 是 | string | 本次收到/发送的消息数量 |
businesstype | 是 | string | 业务类型代码 |
businessdata | 是 | string | 业务数据(16进制protobuf文本) |
time | 是 | string | 收到消息的时间 |
msgtype | 是 | string | 消息类型代码 |
device | 是 | string | 设备代码 手机=1 其他=0 |
fromtype | 是 | string | 个人消息=1 群消息=2 |
fromwxid | 是 | string | 发送方微信ID |
toid | 是 | string | 接收人微信ID/群ID |
msg | 是 | string | 消息内容 |
msgsvrid | 是 | string | 服务器消息ID,用于撤回,或者下载图片/视频/文件 |
img_path | 是 | string | 如果当前收到的是图片,则会显示图片路径图片为.dat格式 |
gif_path | 是 | string | 如果当前收到的是gif表情,则会显示gif路径 |
file_path | 是 | string | 如果当前收到的是文件,则会显示文件路径 |
video_path | 是 | string | 如果当前收到的是视频,则会显示视频路径 |
voip_data | 是 | string | 如果当前收到的是语音,则会显示语音数据。格式为16进制文本的.silk文件 |
revoke_msg | 是 | string | 如果当前收到的是撤回的消息,则会显示撤回的内容 |
appletcode | 否 | string | 返回小程序code,如果调用了GetMiniAppCode_Sync接口则会返回该数据 |
msgtype 消息类型代码 二级代码参考:
1 文本消息 3 图片消息 34 语音消息 37 好友确认消息 40 好友推荐消息 42 共享名片 43 视频消息 44 主动撤回 47 动画表情 48 位置消息 49 APP分享链接/文件 50 VOIP消息 51 微信初始化消息 52 VOIP结束消息 53 VOIP邀请 62 小视频 9999 SYSNOTICE 10000 系统消息 10002 撤回消息
返回值
返回值将被忽略
Python FLASK 代码示例
获取聊天信息
# encoding:utf-8
from flask import Flask, request, jsonify
from flask_cors import CORS
import logging
from flask_httpauth import HTTPTokenAuth
import requests
import json
import binascii
import os
import sys
import logging
import re
from PIL import Image
import blackboxprotobuf
auth = HTTPTokenAuth(scheme='JWT')
app = Flask(__name__)
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
# 开启消息接收功能
def ConfigureMsgRecive(diyurl):
url = &quot;http://127.0.0.1:30001/ConfigureMsgRecive&quot;
aheaders = {'Content-Type': 'application/json'}
params_1 = {
&quot;isEnable&quot;: &quot;1&quot;,
&quot;url&quot;: diyurl
}
data = json.dumps(params_1)
#print('[JSON格式raw]', data)
r = requests.post(url, headers=aheaders, data=data)
ret = r.text
print(ret)
# 自定义URL获取聊天信息
i = 0
@app.route('/api/msg/recive', methods=[&quot;POST&quot;])
def diy_recive_msg():
if request.method == 'POST':
# payload = request.get_data(as_text=True)
# print('[全数据]\r', payload)
# 按照顺序打印输出
# payload = json.dumps(payload, sort_keys=True, indent=2, ensure_ascii=False)
# print('[全数据]\r', payload)
global i
i = i + 1
print('================= [自定义URL消息接收] ====================')
ServerPort = request.json.get('ServerPort')
print('[服务端口]', ServerPort)
selfwxid = request.json.get('selfwxid')
print('[本人微信ID]', selfwxid)
# 1=收到的消息 2=发送的消息
sendorrecv = request.json.get('sendorrecv')
print('[收发类型]', sendorrecv)
# 本次收到消息的数量
msgnumber = request.json.get('msgnumber')
print('[收到数量]', msgnumber)
# 消息数组
msglist = request.json.get('msglist')
for i in range(len(msglist)):
# 消息序号
index =msglist[i]['index']
print('[消息序号]', index)
# 消息时间
time = msglist[i]['time']
print('[消息时间]', time)
# 消息类型
msgtype = msglist[i]['msgtype']
print('[消息类型]', msgtype)
# 服务器消息ID
msgsvrid = msglist[i]['msgsvrid']
print('[服务器消息ID]', msgsvrid)
# 消息内容
msg = msglist[i]['msg']
print('[消息内容]', msg)
# 发送人微信ID
fromwxid = msglist[i]['fromwxid']
print('[发送人微信ID]', fromwxid)
# 接收方微信ID/群ID
toid = msglist[i]['toid']
print('[消息接收者]', toid)
return '1'
else:
# 请求方式错误
return '0'
if __name__ == '__main__':
# 下面的代码是消息接收的部分
app.config['JSON_AS_ASCII'] = False
CORS(app, supports_credentials=True)
# 开启消息接收
ConfigureMsgRecive(&quot;http://localhost:1029/api/msg/recive&quot;)
app.run(host='0.0.0.0', port=1029, debug=True)