-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_server.py
More file actions
54 lines (41 loc) · 1.48 KB
/
Copy pathrun_server.py
File metadata and controls
54 lines (41 loc) · 1.48 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
机器人服务器入口文件
Created by plough on 2018/11/16.
"""
import sys
from flask import Flask, request
import config
from app.utils.wxbot_helper import WxBotHelper
from app.wxbot import WxBot
app = Flask(__name__)
@app.route("/wxbot", methods=['GET', 'POST'])
def hello():
# token 验证,只进行一次。验证成功后,以后都不再发送此请求
if request.method == 'GET' and 'echostr' in request.args:
reply_echo_str = WxBotHelper.verify_url(request.args)
return reply_echo_str
# 正常接收消息
if request.method == 'POST':
encrypt_msg = answer_msg_and_encrypt(request.args, request.get_data(as_text=True))
return encrypt_msg
def init_logging():
import logging
logging.basicConfig(
filename='wxbot.log',
format='%(asctime)s %(levelname)s: %(message)s',
datefmt='%Y/%m/%d %H:%M:%S',
level=logging.DEBUG)
def answer_msg_and_encrypt(args, encrypt_xml_data):
s_msg_signature = args['msg_signature']
s_timestamp = args['timestamp']
s_nonce = args['nonce']
s_xml_content = WxBotHelper.decrypt_msg(s_msg_signature, s_timestamp, s_nonce, encrypt_xml_data)
s_res_xml = WxBot.make_response(s_xml_content)
if s_res_xml is None:
sys.exit(0)
return WxBotHelper.encrypt_msg(s_res_xml, s_nonce, s_timestamp)
if __name__ == "__main__":
init_logging()
app.run(host="0.0.0.0", port=5006, debug=config.DEBUG)