37创客科创中心

 找回密码
 立即注册
查看: 1000|回复: 0

微信flaskwx

[复制链接]

194

主题

324

帖子

2401

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2401
发表于 2024-2-5 09:17:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
flaskwx.py

  1. # -*- coding: utf-8 -*-
  2. import time
  3. from flask import Flask, request, abort
  4. import hashlib
  5. import xmltodict
  6. # from wechatpy import parse_message, create_reply
  7. WECHAT_TOKEN = '37ck20200808'
  8. app = Flask(__name__)
  9. # 格式处理
  10. def make(xmldict, t_text):
  11.     res_dict = {
  12.         'xml': {
  13.             'ToUserName': xmldict.get('FromUserName'),
  14.             'FromUserName': xmldict.get('ToUserName'),
  15.             'CreateTime': int(time.time()),
  16.             'MsgType': 'text',
  17.             'Content': t_text
  18.         }
  19.     }
  20.     return res_dict
  21. # 微信入口
  22. @app.route('/wx', methods=['GET', 'POST'])
  23. def wx2024():
  24.     signature = request.args.get("signature")
  25.     timestamp = request.args.get("timestamp")
  26.     nonce = request.args.get("nonce")
  27.     echostr = request.args.get("echostr")
  28.     if not all([signature, timestamp, nonce]):
  29.         abort(400)
  30.     li = [WECHAT_TOKEN, timestamp, nonce]
  31.     li.sort()
  32.     t_str = ''.join(li)
  33.     sign = hashlib.sha1(t_str.encode('utf-8')).hexdigest()
  34.     if signature != sign:
  35.         abort(403)
  36.     else:
  37.         if request.method == 'GET':#GET获取数据和验证
  38.             echostr = request.args.get('echostr')
  39.             if not echostr:
  40.                 abort(400)
  41.             return echostr
  42.         elif request.method == 'POST':#数据提交交互部分,人机应答入口
  43.             xml_s = request.data
  44.             if not xml_s:
  45.                 abort(400)
  46.             # 获取微信公众平台信息
  47.             xml_dict = xmltodict.parse(xml_s)
  48.             xml_dict = xml_dict.get('xml')
  49.             msg_type = xml_dict.get('MsgType')
  50.             Content = xml_dict.get('Content')
  51.             # 各个成员功能识别入口
  52.             if msg_type == 'text':
  53.                 if Content == '37创客':
  54.                     Content = '科技兴国从我做起'
  55.                 elif Content == '全梓豪':
  56.                     Content = '全梓豪已经通过测试'
  57.                 elif Content == '欧阳尚宏':
  58.                     Content = '欧阳尚宏已经通过测试'
  59.             else:
  60.                 Content = '其他奥利给混进来了,请注意安全!'
  61.             res_dict = make(xml_dict, Content)
  62.             res_xml_str = xmltodict.unparse(res_dict)
  63.             return res_xml_str
  64.             # return 'success'
  65. if __name__ == '__main__':
  66.     app.run(host='0.0.0.0', port=85, debug=True)
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|37创客科创中心

GMT+8, 2025-12-11 17:18 , Processed in 0.153041 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表