微信小程序,已经在后台配置好了客服的API,验证成功
在小程序的(体验版)中测试是否可以返回消息
用了官方的方法,即使用了微信官方的POST,result=requests.post(url,data) ,打印结果是<Response [200]>成功,但是手机端没有收到任何信息,
然后直接使用return,也是没有反应(我也分开尝试过,都没有响应)
同时,我可以看到后台,小程序请求我的端口3次,应该是触发了“5秒钟内没有响应就再次调用,一共三次”的规则,说明没有回复成功,怎么回事呢?
def hvac_wechat(request):
# 服务器验证
if request.method == 'GET':
signature = request.GET.get("signature","")
print("获取的signature:"+signature)
timestamp = request.GET.get("timestamp","")
nonce = request.GET.get("nonce","")
echostr=request.GET.get("echostr","")
tmpArr = [TOKEN, timestamp, nonce]
tmpArr.sort()
tmpStr = "".join(tmpArr)
tmpStr = hashlib.sha1(tmpStr.encode()).hexdigest()
print("tmpStr:"+tmpStr)
print("signature:"+signature)
if tmpStr == signature:
return HttpResponse(echostr, content_type="text/plain")
else:
return HttpResponse("", content_type="text/plain")
# 服务器验证成功,POST则
else:
msg=json.loads(request.body)
print(msg)
if msg['MsgType']=='text':
query=msg['Content']
openid=msg['FromUserName']
access_token=get_access_token()
url='https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='+access_token
data={
"touser": openid,
"MsgType": "text",
"text":
{
"content":'收到'
}
}
result=requests.post(url,data)
print(result)
return JsonResponse({
"touser": openid,
"MsgType": "text",
"text":
{
"content":'收到'
}
}
)
else:
pass