获取消息
接收获取到消息
1、接收消息会有两条请求是成对的,一条是判断是否有消息,当他的响应内容有'selector:"2"'是说明有消息的,第二条是当他是有前面的值时,才去获取消息的内容,和这条消息是谁发送的,并且前端是不断的去轮询请求:
1、判断是否有消息的请求url:
参数图片:
后台实现的代码:
def get_message(request):
"""
获取消息
:param request:
:return:
"""
#确认是否有信息url
sync_check_url = "https://webpush.wx2.qq.com/cgi-bin/mmwebwx-bin/synccheck"
sysc_data_list = []
for item in USER_INIT_DATA["SyncKey"]["List"]:
temp = "%s_%s"%(item["Key"],item["Val"])
sysc_data_list.append(temp)
sysc_data = "|".join(sysc_data_list)
param_data = {
"r":int(time.time()),
"skey":TICKET_DATA_DICT["skey"],
"sid":TICKET_DATA_DICT["wxsid"],
"uin":TICKET_DATA_DICT["wxuin"],
"deviceid":int(time.time()),
"synckey":sysc_data
}
cookie_all = {}
# 因为不知道用哪个cookie所以上面两个都给加上了
cookie_all.update(LOGIN_COOKIE_DICT)
cookie_all.update(TICKET_COOKIE_DICT)
reponse_check_message = requests.get(url=sync_check_url,params=param_data,cookies=cookie_all)
# print("reponse_check_message_text",reponse_check_message.text)
#有selector:"2"代表有消息
2、判断获取消息
2.1 获取url:
2.2 请求的参数:
2.3获取消息的代码
# print("reponse_check_message_text",reponse_check_message.text)
#有selector:"2"代表有消息
#接收消息
if 'selector:"2"' in reponse_check_message.text:
fetch_msg_url = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid={0}&skey={1}&lang=zh_CN&pass_ticket={2}"
fetch_msg_url = fetch_msg_url.format(TICKET_DATA_DICT["wxsid"],TICKET_DATA_DICT["skey"],TICKET_DATA_DICT["pass_ticket"])
form_data ={
"BaseRequest":{
"DeviceID":int(time.time()),
"Sid":TICKET_DATA_DICT["wxsid"],
"Skey":TICKET_DATA_DICT["skey"],
"Uin":TICKET_DATA_DICT["wxuin"]
},
"SyncKey":USER_INIT_DATA["SyncKey"],
"rr":int(time.time())
}
response_fetch_msg = requests.post(url=fetch_msg_url,json=form_data)
response_fetch_msg.encoding="utf-8"
res_fetch_msg_dict = json.loads(response_fetch_msg.text)
#返回的值更新了SyncKey
USER_INIT_DATA["SyncKey"] = res_fetch_msg_dict["SyncKey"]
#AddMsgList消息列表
#Content消息的内容
#FromUserName谁发来的
#ToUserName发给谁
for item in res_fetch_msg_dict["AddMsgList"]:
print(item["Content"],"::::",item["FromUserName"],"----->",item["ToUserName"],)
#print("res_fetch_msg_dict:",res_fetch_msg_dict)
return HttpResponse("OK")
3、前端用ajax方式不断的轮询请求
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户联系人</title>
</head>
<body>
<div>
<p><input id="user_id" type="text" placeholder="发送的用户"/></p>
<p><input id="user_msg" placeholder="发送的消息"/></p>
<input id="sendmsg" type="button" value="提交">
</div>
<ul>
{% for row in data.MemberList%}
<li>{{row.NickName}}_{{row.UserName}}_{{row.Province}}</li>
{%endfor%}
</ul>
<script src="/static/js/jquery.min.js"></script>
<script>
//页面加载完实现
$(function () {
//发送消息
sendmessage();
//获取消息
getmessage();
});
//发送消息
function sendmessage() {
$("#sendmsg").click(function () {
$.ajax({
url:"/sendmessage/",
type:"POST",
data:{"user_id":$("#user_id").val(),"user_msg":$("#user_msg").val()},
success:function () {
}
})
})
}
//接收消息
function getmessage() {
$.ajax({
url:"/getmessage/",
type:"POST",
success:function () {
getmessage()
}
})
}
</script>
</body>
</html>
4、第一条和第二条连接的代码
def get_message(request):
"""
获取消息
:param request:
:return:
"""
#确认是否有信息url
sync_check_url = "https://webpush.wx2.qq.com/cgi-bin/mmwebwx-bin/synccheck"
sysc_data_list = []
for item in USER_INIT_DATA["SyncKey"]["List"]:
temp = "%s_%s"%(item["Key"],item["Val"])
sysc_data_list.append(temp)
sysc_data = "|".join(sysc_data_list)
param_data = {
"r":int(time.time()),
"skey":TICKET_DATA_DICT["skey"],
"sid":TICKET_DATA_DICT["wxsid"],
"uin":TICKET_DATA_DICT["wxuin"],
"deviceid":int(time.time()),
"synckey":sysc_data
}
cookie_all = {}
# 因为不知道用哪个cookie所以上面两个都给加上了
cookie_all.update(LOGIN_COOKIE_DICT)
cookie_all.update(TICKET_COOKIE_DICT)
reponse_check_message = requests.get(url=sync_check_url,params=param_data,cookies=cookie_all)
# print("reponse_check_message_text",reponse_check_message.text)
#有selector:"2"代表有消息
#接收消息
if 'selector:"2"' in reponse_check_message.text:
fetch_msg_url = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid={0}&skey={1}&lang=zh_CN&pass_ticket={2}"
fetch_msg_url = fetch_msg_url.format(TICKET_DATA_DICT["wxsid"],TICKET_DATA_DICT["skey"],TICKET_DATA_DICT["pass_ticket"])
form_data ={
"BaseRequest":{
"DeviceID":int(time.time()),
"Sid":TICKET_DATA_DICT["wxsid"],
"Skey":TICKET_DATA_DICT["skey"],
"Uin":TICKET_DATA_DICT["wxuin"]
},
"SyncKey":USER_INIT_DATA["SyncKey"],
"rr":int(time.time())
}
response_fetch_msg = requests.post(url=fetch_msg_url,json=form_data)
response_fetch_msg.encoding="utf-8"
res_fetch_msg_dict = json.loads(response_fetch_msg.text)
#返回的值更新了SyncKey
USER_INIT_DATA["SyncKey"] = res_fetch_msg_dict["SyncKey"]
#AddMsgList消息列表
#Content消息的内容
#FromUserName谁发来的
#ToUserName发给谁
for item in res_fetch_msg_dict["AddMsgList"]:
print(item["Content"],"::::",item["FromUserName"],"----->",item["ToUserName"],)
#print("res_fetch_msg_dict:",res_fetch_msg_dict)
return HttpResponse("OK")