-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
54 lines (46 loc) · 1.63 KB
/
app.py
File metadata and controls
54 lines (46 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from flask import Flask, request, abort
from wechatpy import parse_message, create_reply
from wechatpy.utils import check_signature
from wechatpy.exceptions import InvalidSignatureException
from dotenv import load_dotenv
import os
from deepseek_client import DeepSeekClient
# 加载环境变量
load_dotenv()
# 初始化DeepSeek客户端
deepseek_client = DeepSeekClient()
# 获取环境变量
TOKEN = os.getenv('WECHAT_TOKEN')
AES_KEY = os.getenv('WECHAT_AES_KEY')
APP_ID = os.getenv('WECHAT_APP_ID')
app = Flask(__name__)
@app.route('/wechat', methods=['GET', 'POST'])
def wechat():
# 微信服务器验证请求
if request.method == 'GET':
signature = request.args.get('signature', '')
timestamp = request.args.get('timestamp', '')
nonce = request.args.get('nonce', '')
echostr = request.args.get('echostr', '')
try:
check_signature(TOKEN, signature, timestamp, nonce)
return echostr
except InvalidSignatureException:
abort(403)
# 处理微信消息
if request.method == 'POST':
try:
msg = parse_message(request.data)
if msg.type == 'text':
# 调用DeepSeek API处理用户消息
response = deepseek_client.chat(msg.content)
reply = create_reply(response, msg)
return reply.render()
else:
reply = create_reply('目前只支持文本消息', msg)
return reply.render()
except Exception as e:
print(f'Error: {str(e)}')
abort(500)
if __name__ == '__main__':
app.run(debug=True)