from flask import Flask
from flask import request
import hashlib
from wechatpy import parse_message, create_reply
from wechatpy.utils import check_signature
from wechatpy.exceptions import InvalidSignatureException
from wechatpy.replies import TextReply
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
signature = request.args.get('signature')
timestamp = request.args.get('timestamp')
nonce = request.args.get('nonce')
token = 'zzmtkj'
echostr = request.args.get('echostr')
try:
echostr = check_signature(token, signature, timestamp, nonce)
except InvalidSignatureException:
print('error')
if signature == sha1_signature:
return echostr
elif request.method == 'POST':
msg = parse_message(request.get_data())
if msg.type == 'text':
reply = create_reply('这是条文字消息', msg)
if msg.type == 'image':
reply = create_reply('这是条图片消息', msg)
return reply.render()
flask使用wechatpy回复消息
最新推荐文章于 2024-08-12 08:44:09 发布