代码拉取完成,页面将自动刷新
# -*- coding: utf-8 -*-
import os
from flask import Flask, request, abort
from wechatpy import parse_message, create_reply
from wechatpy.exceptions import (
InvalidSignatureException,
)
from wechatpy.utils import check_signature
# 这里设置你自定义的token, 比如: abcd,token在公众号平台 --> 设置与开发 --> 基本配置.
TOKEN = os.getenv("WECHAT_TOKEN", "abcd")
AES_KEY = os.getenv("WECHAT_AES_KEY", "")
APPID = os.getenv("WECHAT_APPID", "")
app = Flask(__name__)
@app.route("/wx", methods=["GET", "POST"])
def wechat():
signature = request.args.get("signature", "")
timestamp = request.args.get("timestamp", "")
nonce = request.args.get("nonce", "")
encrypt_type = request.args.get("encrypt_type", "raw")
try:
check_signature(TOKEN, signature, timestamp, nonce)
except InvalidSignatureException:
abort(403)
if request.method == "GET":
echo_str = request.args.get("echostr", "")
return echo_str
# POST request
if encrypt_type == "raw":
# plaintext mode
msg = parse_message(request.data)
if msg.type == "text":
reply = create_reply(msg.content, msg)
else:
reply = create_reply("Sorry, can not handle this for now", msg)
return reply.render()
if __name__ == "__main__":
app.run("0.0.0.0", 8081, debug=True)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。