漆黑天使复活 2023-05-05 17:58 采纳率: 44.4%
浏览 42
已结题

小程序客户API,没有应答

微信小程序,已经在后台配置好了客服的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

  • 写回答

4条回答 默认 最新

  • Usinian 2023-05-05 19:48
    关注

    基于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格式。这样就能够正确回复消息了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 5月14日
  • 已采纳回答 5月6日
  • 创建了问题 5月5日

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?