From cec81fd6925eb0ef979b050206a40e571d7203d1 Mon Sep 17 00:00:00 2001 From: injetlee Date: Wed, 13 Jun 2018 09:47:21 +0800 Subject: [PATCH] wechat01 --- wechat/connect.py | 42 ++++++++++++++++++++++++++++++++++++++++ wechat/requirements.txt | Bin 0 -> 498 bytes 2 files changed, 42 insertions(+) create mode 100644 wechat/connect.py create mode 100644 wechat/requirements.txt diff --git a/wechat/connect.py b/wechat/connect.py new file mode 100644 index 0000000..423cd11 --- /dev/null +++ b/wechat/connect.py @@ -0,0 +1,42 @@ +import falcon +from wechatpy.utils import check_signature +from wechatpy.exceptions import InvalidSignatureException +from wechatpy import parse_message +from wechatpy.replies import TextReply, ImageReply + + +class Connect(object): + + def on_get(self, req, resp): + query_string = req.query_string + query_list = query_string.split('&') + b = {} + for i in query_list: + b[i.split('=')[0]] = i.split('=')[1] + + try: + check_signature(token='lengxiao', signature=b['signature'], timestamp=b['timestamp'], nonce=b['nonce']) + resp.body = (b['echostr']) + except InvalidSignatureException: + pass + resp.status = falcon.HTTP_200 + + def on_post(self, req, resp): + xml = req.stream.read() + msg = parse_message(xml) + if msg.type == 'text': + reply = TextReply(content=msg.content, message=msg) + xml = reply.render() + resp.body = (xml) + resp.status = falcon.HTTP_200 + elif msg.type == 'image': + reply = ImageReply(media_id=msg.media_id, message=msg) + xml = reply.render() + resp.body = (xml) + resp.status = falcon.HTTP_200 + + +app = falcon.API() +connect = Connect() +app.add_route('/connect', connect) + diff --git a/wechat/requirements.txt b/wechat/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..fdd31d56f9fea0c7445c14f971f8b18409e11b20 GIT binary patch literal 498 zcmZvZ!485j5Jcx};-_e+8ujAGC}Jc*K`2pwUVYm_^kVRo?x}H?@vdyo4$dQGT_D7C54Yl!IxxXVUvS%&yVJLXYCXDv9k(C5-%;W} X@J#tk1luTfc(6eQqyBS`#)_^F{Z~n0 literal 0 HcmV?d00001