解決因配置開啟服務(wù)器配置導(dǎo)致關(guān)注微信服務(wù)號自動回復(fù)

  • 報錯信息:由于在開發(fā)者中心開啟了回調(diào)URL和Token,當(dāng)前自動回復(fù)設(shè)置已失效。你可以前往開發(fā)者中心進(jìn)行停用。
  • 報錯截圖.png
# -*- coding: utf-8 -*-
import hashlib
import web
import xml.etree.ElementTree as ET
import time
import json


class Handle(object):
    def GET(self):
        try:
            data = web.input()
            if len(data) == 0:
                return "hello, this is handle view"
            signature = data.signature
            timestamp = data.timestamp
            nonce = data.nonce
            echostr = data.echostr
            token = "xxx12345678"

            list1 = [token, timestamp, nonce]
            list1.sort()
            list2 = [x.encode('utf-8') for x in list1]
            sha1 = hashlib.sha1()
            [sha1.update(x) for x in list2]
            hashcode = sha1.hexdigest()
            print("handle/GET func: hashcode, signature: ", hashcode, signature)
            if hashcode == signature:
                return echostr
            else:
                return ""
        except Exception as Argument:
            return Argument

    def POST(self):
        try:
            webdata = web.data()
            xml_data = ET.fromstring(webdata)

            msg_type = xml_data.find('MsgType').text
            from_user = xml_data.find('FromUserName').text
            to_user = xml_data.find('ToUserName').text

            if msg_type == 'event':
                event = xml_data.find('Event').text
                if event == 'subscribe':
                    # 用戶關(guān)注時的歡迎消息
                    content = "感謝關(guān)注!\n我是您的智能助手,讓我們開始愉快的對話吧!"
                    reply_msg = {
                        'ToUserName': from_user,
                        'FromUserName': to_user,
                        'CreateTime': int(time.time()),
                        'MsgType': 'text',
                        'Content': content
                    }
                    return self.dict_to_xml(reply_msg)
            return 'success'
        except Exception as e:
            return e

    def dict_to_xml(self, dict_data):
        xml_template = """
        <xml>
            <ToUserName><![CDATA[{ToUserName}]]></ToUserName>
            <FromUserName><![CDATA[{FromUserName}]]></FromUserName>
            <CreateTime>{CreateTime}</CreateTime>
            <MsgType><![CDATA[{MsgType}]]></MsgType>
            <Content><![CDATA[{Content}]]></Content>
        </xml>
        """
        return xml_template.format(**dict_data)

這個代碼是需要配置的服務(wù)器上的回調(diào)方法調(diào)用的


image.png

開發(fā)者微信號:


image.png

參考文獻(xiàn):
微信公眾平臺接口調(diào)試工具 (qq.com)

基礎(chǔ)消息能力 / 接收事件推送 (qq.com)

微信開放社區(qū) (qq.com)
微信公眾號開發(fā)系列-獲取微信OpenID_怎么找到自己的openid-CSDN博客

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容