|
|

楼主 |
发表于 2024-2-4 09:56:15
|
显示全部楼层
- # -*- coding: utf-8 -*-
- #app.py
- from flask import Flask, request
- from flask import make_response
- import hashlib
- from wechatpy import parse_message, create_reply
- # 实例化Flask
- app = Flask(__name__)
- global reply
- @app.route('/wx', methods=['GET', 'POST']) # 路由
- def wx0204():
- if request.method == 'GET':
- token = '37ck20200808'
- data = request.args
- signature = data.get('signature', '')
- timestamp = data.get('timestamp', '')
- nonce = data.get('nonce', '')
- echostr = data.get('echostr', '')
- s = sorted([timestamp, nonce, token])
- # 字典排序
- s = ''.join(s)
- if hashlib.sha1(s.encode('utf-8')).hexdigest() == signature:
- response = make_response(echostr)
- return response
- # return '37创客:错误签名'
- else:
- msg = parse_message(request.get_data())
- # msg = parse_message(request.args)
- # msg = request.args
- if msg.type == 'text':
- reply = create_reply('你发送了条文字信息:' + msg.content, message=msg)
- if msg.type == 'image':
- reply = create_reply('你发送了条图片信息', msg)
- if msg.content == '37ck':
- reply = create_reply('你发送了条文字信息:', '37创客')
- return reply.render()
- # 主程序
- if __name__ == '__main__':
- app.run(host='0.0.0.0', port=84, debug=True)
复制代码 |
|