def test_msg(request):
# 获取访问token
def get_access_toke():
res = requests.get(
url="https://api.weixin.qq.com/cgi-bin/token",
params={
"grant_type": "client_credential",
"appid": "wx430b37a0080c",
"secret": "c3a142b8d8db0214705d85dc110ab"
}
).json()
print(res)
# {"access_token": "ACCESS_TOKEN", "expires_in": 7200}
if res.get("access_token"):
access_token = res.get("access_token")
else:
access_token = None
return access_token
access_token = get_access_toke()
print("access_token===", access_token)
all_player_num = SeverSum.objects.all().aggregate(Sum("online"))
def send_template_msg():
res = requests.post(
url="https://api.weixin.qq.com/cgi-bin/message/template/send",
params={'access_token': access_token },
json={
"touser": "oWUvO6bUfovhRv4hVDFuN98dycr8",
"template_id": "WQO-_V76YqBEw2cNADcmq6TCU7xQNorIR0PLpY4NYDQ",
"data": {'first': {'value': all_player_num["online__sum"], 'color': '#00FF00'}}
}
)
result = res.json()
return result
result = send_template_msg()
if result.get("errcode") == 0:
return HttpResponse("发送成功")
else:
return HttpResponse("发送失败")