-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
65 lines (56 loc) · 2.07 KB
/
run.py
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
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
from infra.quart_app import app
from infra.scheduler import init_scheduler
#from infra.mysql import init_pool, init_tables
from infra.wechat import init_wx_client
from utils.logger import logger
import os, asyncio, json
from quart import request
from wechatpy import parse_message, create_reply
from wechatpy.utils import check_signature
from dispatcher import msg_dispatcher
from service import wechat_service
from utils.deco import controller, api_controller
from config import APP_ID, APP_SECRET, APP_TOKEN, APP_AES_KEY
@app.before_serving
async def startup():
loop = asyncio.get_event_loop()
init_scheduler(loop)
# await init_pool(loop)
# await init_tables()
init_wx_client(APP_ID, APP_SECRET)
@app.route("/wechat", methods=["GET", "POST"])
@controller
async 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")
msg_signature = request.args.get("msg_signature", "")
request_body = await request.data
check_signature(APP_TOKEN, signature, timestamp, nonce)
if request.method == "GET":
echo_str = request.args.get("echostr", "")
return echo_str
# POST request
if encrypt_type == "raw":
# plaintext mode
reply = await msg_dispatcher(request_body)
if reply != None:
return reply.render()
else:
return ""
else:
# encryption mode
from wechatpy.crypto import WeChatCrypto
crypto = WeChatCrypto(APP_TOKEN, APP_AES_KEY, APP_ID)
msg = crypto.decrypt_message(request_body, msg_signature, timestamp, nonce)
reply = await msg_dispatcher(msg)
return crypto.encrypt_message(reply.render(), nonce, timestamp)
@app.route("/api/generateQrCode", methods=["POST"])
@api_controller
async def getSubcriptionQrCode():
request_body = await request.get_json()
return await wechat_service.generate_qr_code(request_body)
if __name__ == '__main__':
app.run('0.0.0.0', 80)