@@ 23-58 (lines=36) @@ | ||
20 | return render_template('index.html', host=host) |
|
21 | ||
22 | ||
23 | @app.route('/wechat', methods=['GET', 'POST']) |
|
24 | def wechat(): |
|
25 | signature = request.args.get('msg_signature', '') |
|
26 | timestamp = request.args.get('timestamp', '') |
|
27 | nonce = request.args.get('nonce', '') |
|
28 | ||
29 | crypto = WeChatCrypto(TOKEN, EncodingAESKey, CorpId) |
|
30 | if request.method == 'GET': |
|
31 | echo_str = request.args.get('echostr', '') |
|
32 | try: |
|
33 | echo_str = crypto.check_signature( |
|
34 | signature, |
|
35 | timestamp, |
|
36 | nonce, |
|
37 | echo_str |
|
38 | ) |
|
39 | except InvalidSignatureException: |
|
40 | abort(403) |
|
41 | return echo_str |
|
42 | else: |
|
43 | try: |
|
44 | msg = crypto.decrypt_message( |
|
45 | request.data, |
|
46 | signature, |
|
47 | timestamp, |
|
48 | nonce |
|
49 | ) |
|
50 | except (InvalidSignatureException, InvalidCorpIdException): |
|
51 | abort(403) |
|
52 | msg = parse_message(msg) |
|
53 | if msg.type == 'text': |
|
54 | reply = create_reply(msg.content, msg).render() |
|
55 | else: |
|
56 | reply = create_reply('Can not handle this for now', msg).render() |
|
57 | res = crypto.encrypt_message(reply, nonce, timestamp) |
|
58 | return res |
|
59 | ||
60 | ||
61 | if __name__ == '__main__': |
@@ 16-51 (lines=36) @@ | ||
13 | app = Flask(__name__) |
|
14 | ||
15 | ||
16 | @app.route('/wechat', methods=['GET', 'POST']) |
|
17 | def wechat(): |
|
18 | signature = request.args.get('msg_signature', '') |
|
19 | timestamp = request.args.get('timestamp', '') |
|
20 | nonce = request.args.get('nonce', '') |
|
21 | ||
22 | crypto = WeChatCrypto(TOKEN, EncodingAESKey, CorpId) |
|
23 | if request.method == 'GET': |
|
24 | echo_str = request.args.get('echostr', '') |
|
25 | try: |
|
26 | echo_str = crypto.check_signature( |
|
27 | signature, |
|
28 | timestamp, |
|
29 | nonce, |
|
30 | echo_str |
|
31 | ) |
|
32 | except InvalidSignatureException: |
|
33 | abort(403) |
|
34 | return echo_str |
|
35 | else: |
|
36 | try: |
|
37 | msg = crypto.decrypt_message( |
|
38 | request.data, |
|
39 | signature, |
|
40 | timestamp, |
|
41 | nonce |
|
42 | ) |
|
43 | except (InvalidSignatureException, InvalidCorpIdException): |
|
44 | abort(403) |
|
45 | msg = parse_message(msg) |
|
46 | if msg.type == 'text': |
|
47 | reply = create_reply(msg.content, msg).render() |
|
48 | else: |
|
49 | reply = create_reply('Can not handle this for now', msg).render() |
|
50 | res = crypto.encrypt_message(reply, nonce, timestamp) |
|
51 | return res |
|
52 | ||
53 | ||
54 | if __name__ == '__main__': |