微信小程序,已经在后台配置好了客服的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
基于gpt
可能原因是在发送回复消息时,没有正确构造回复消息的格式。微信小程序要求回复消息的格式必须符合微信公众平台的消息格式要求,否则无法正确回复消息。
可以参考以下代码修改:
import json
import hashlib
import requests
from django.http import HttpResponse, JsonResponse
def hvac_wechat(request):
# 服务器验证
if request.method == 'GET':
signature = request.GET.get("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()
if tmpStr == signature:
return HttpResponse(echostr, content_type="text/plain")
else:
return HttpResponse("", content_type="text/plain")
# POST则回复消息
else:
msg = json.loads(request.body)
openid = msg['FromUserName']
access_token = get_access_token()
url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' + access_token
if msg['MsgType'] == 'text':
content = '收到'
else:
content = '未知类型消息'
data = {
"touser": openid,
"msgtype": "text",
"text": {
"content": content
}
}
headers = {
'content-type': 'application/json'
}
result = requests.post(url, data=json.dumps(data), headers=headers)
print(result.json())
return HttpResponse('', content_type="text/plain")
在以上代码中,我们使用了headers来指定content-type为application/json,然后在发送消息时使用json.dumps()将数据转换为JSON格式。这样就能够正确回复消息了。
引用chatGPT作答,有多种可能导致消息无法传递到手机应用程序:
1.确保API配置中提供的URL与Webhook端点的URL匹配。如果它们不匹配,则无法传递消息。
2.检查您在POST请求中发送的数据是否符合正确的格式。您可以尝试使用像Postman这样的工具手动发送消息,以验证API是否正常工作。
3.检查API调用返回的响应代码。200状态代码表示请求成功,但它并不保证消息已传递。您可以尝试添加错误处理到代码中,以检查响应中的任何错误。
4.确保在API调用中使用的访问令牌是有效的且未过期。如果令牌已过期,则需要在发送消息之前获取一个新令牌。
5.验证您的微信帐户是否具有发送消息所需的权限。如果您的帐户未经授权发送消息,则可能需要从微信请求其他权限。
6.检查消息是否发送到正确的用户。您可以尝试向自己的微信帐户发送测试消息,以验证消息是否发送到正确的用户。
7.检查消息是否被用户的设备或微信设置阻止。用户可能启用了阻止来自未知来源或特定类型消息的设置。
8.确保您的Webhook端点在指定的时间限制内做出响应。微信对Webhook请求有5秒超时时间,因此,如果您的服务器响应时间超过5秒,消息将无法传递。您可以尝试优化服务器响应时间,以确保消息在限制时间内传递。
希望这可以帮助您识别问题并解决它。
小程序调用access-token 是有限制的,且access-token 有效时间只有两个小时,首先你要解决配置问题,其次保证access_token的一致性和有效生命周期,获取access_token方式如下:为了保密appsecrect,第三方需要一个access_token获取和刷新的中控服务器。而其他业务逻辑服务器所使用的access-token均来自于该中控服务器,不应该各自刷新,否则会造成access_token覆盖而影响业务。有用请点个有用!