1 Star 0 Fork 0

shanhe/PythonProject

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
wechat_service.py 1.48 KB
一键复制 编辑 原始数据 按行查看 历史
山河 提交于 2026-06-22 09:38 +08:00 . 实验四
# wechat_service.py
from flask import Flask, request, make_response
from wechatpy import parse_message, create_reply
from wechatpy.utils import check_signature
from test import get_weather_text # 你原始代码中的函数
app = Flask(__name__)
# 与微信测试号后台填写的 Token 保持一致
TOKEN = "222984b57a9b0a93" # 随意设置,但必须与测试号一致
@app.route('/wechat', methods=['GET', 'POST'])
def wechat():
signature = request.args.get('signature', '')
timestamp = request.args.get('timestamp', '')
nonce = request.args.get('nonce', '')
echostr = request.args.get('echostr', '')
if request.method == 'GET':
# 验证服务器地址的有效性(第一次配置时微信会发GET请求)
try:
check_signature(TOKEN, signature, timestamp, nonce)
return make_response(echostr)
except Exception as e:
return "验证失败", 403
# POST 处理用户发来的消息(明文模式)
msg = parse_message(request.data)
if msg.type == 'text':
city = msg.content.strip()
reply_text = get_weather_text(city) # 调用你写的天气函数
reply = create_reply(reply_text, msg)
else:
reply = create_reply('请直接发送城市名,例如“上海”', msg)
return make_response(reply.render())
if __name__ == '__main__':
# 监听所有接口,端口 80(需 root 权限)或 8080(但微信只支持 80/443)
app.run(host='0.0.0.0', port=80)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/shanhe13/PythonProject.git
git@gitee.com:shanhe13/PythonProject.git
shanhe13
PythonProject
PythonProject
master

搜索帮助