给你的公众号添加一个智能机器人( 三 )

reply.py
import timeclass Msg(object):    def __init__(self):        pass    def send(self):        return "success"class TextMsg(Msg):    def __init__(self, toUserName, fromUserName, content):        self.__dict = dict()        self.__dict['ToUserName'] = toUserName        self.__dict['FromUserName'] = fromUserName        self.__dict['CreateTime'] = int(time.time())        self.__dict['Content'] = content    def send(self):        XmlForm = """            <xml>                <ToUserName><![CDATA[{ToUserName}]]></ToUserName>                <FromUserName><![CDATA[{FromUserName}]]></FromUserName>                <CreateTime>{CreateTime}</CreateTime>                <MsgType><![CDATA[text]]></MsgType>                <Content><![CDATA[{Content}]]></Content>            </xml>            """        return XmlForm.format(**self.__dict)class ImageMsg(Msg):    def __init__(self, toUserName, fromUserName, mediaId):        self.__dict = dict()        self.__dict['ToUserName'] = toUserName        self.__dict['FromUserName'] = fromUserName        self.__dict['CreateTime'] = int(time.time())        self.__dict['MediaId'] = mediaId    def send(self):        XmlForm = """            <xml>                <ToUserName><![CDATA[{ToUserName}]]></ToUserName>                <FromUserName><![CDATA[{FromUserName}]]></FromUserName>                <CreateTime>{CreateTime}</CreateTime>                <MsgType><![CDATA[image]]></MsgType>                <Image>                <MediaId><![CDATA[{MediaId}]]></MediaId>                </Image>            </xml>            """        return XmlForm.format(**self.__dict)main.py
from flask import Flaskfrom flask import requestimport hashlibimport receiveimport replyapp = Flask(__name__)@app.route("/")def index():    return "Hello World!"# 公众号后台消息路由入口@app.route("/wechat", methods=["GET", "POST"])def wechat(): # 验证使用的GET方法    if request.method == "GET":        signature = request.args.get('signature')        timestamp = request.args.get('timestamp')        nonce = request.args.get('nonce')        echostr = request.args.get('echostr')        token = "公众号后台填写的token"  # 进行排序        dataList = [token, timestamp, nonce]        dataList.sort()        result = "".join(dataList)  #哈希加密算法得到hashcode        sha1 = hashlib.sha1()        sha1.update(result.encode("utf-8"))        hashcode = sha1.hexdigest()        if hashcode == signature:            return echostr        else:            return "" else:        recMsg = receive.parse_xml(request.data)        if isinstance(recMsg, receive.Msg):            toUser = recMsg.FromUserName            fromUser = recMsg.ToUserName            if recMsg.MsgType == 'text':                content = recMsg.Content                replyMsg = reply.TextMsg(toUser, fromUser, content)                return replyMsg.send()            elif recMsg.MsgType == 'image':                mediaId = recMsg.MediaId                replyMsg = reply.ImageMsg(toUser, fromUser, mediaId)                return replyMsg.send()            else:                return reply.Msg().send()        else:            return reply.Msg().send()if __name__ == "__main__":    app.run(host='0.0.0.0', port=80) #公众号后台只开放了80端口


推荐阅读