Skip to content
This repository was archived by the owner on Jun 10, 2025. It is now read-only.

Commit 772d431

Browse files
committed
Merge pull request #10 from halfcrazy/add-wechatpy
Add wechatpy
2 parents 971e3fe + 1ccaa55 commit 772d431

File tree

9 files changed

+55
-7
lines changed

9 files changed

+55
-7
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ services:
88

99
env:
1010
- TOX_ENV=py27
11-
- TOX_ENV=pypy
1211

1312
install:
1413
- pip install flake8

requirements.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
backports-abc==0.4
22
backports.ssl-match-hostname==3.5.0.1
33
certifi==2016.2.28
4+
cffi==1.6.0
45
click==6.6
6+
cryptography==1.3.2
7+
enum34==1.1.4
58
flake8==2.5.4
9+
idna==2.1
10+
ipaddress==1.0.16
611
mccabe==0.4.0
12+
optionaldict==0.1.1
713
pep8==1.7.0
814
py==1.4.31
15+
pyasn1==0.1.9
16+
pycparser==2.14
917
pyflakes==1.0.0
1018
PyJWT==1.4.0
1119
pymongo==3.2.2
1220
pytest==2.9.1
1321
pytest-tornado==0.4.5
22+
python-dateutil==2.5.3
23+
requests==2.10.0
1424
singledispatch==3.4.0.3
1525
six==1.10.0
1626
tornado==4.3
27+
wechatpy==1.2.9
28+
xmltodict==0.10.1

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
'Programming Language :: Python :: 2',
3131
'Programming Language :: Python :: 2.7',
3232
'Programming Language :: Python :: Implementation :: CPython',
33-
'Programming Language :: Python :: Implementation :: PyPy',
3433
],
3534

3635
packages=find_packages('.', exclude=('tests*')),

spirit/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def __init__(self, config):
1313
cookie_secret=config.cookie_secret,
1414
template_path=config.template_path,
1515
static_path=config.static_path,
16-
xsrf_cookies=True,
16+
xsrf_cookies=config.xsrf_cookies,
1717
)
1818

1919
self.db = getattr(

spirit/config/_base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def load_config(mode):
1717
class Config(object):
1818
def __init__(self):
1919
self.DEBUG = True
20+
self.xsrf_cookies = True
2021
self.cookie_secret = base64.b64encode(
2122
uuid.uuid4().bytes + uuid.uuid4().bytes)
2223
self.template_path = os.path.abspath('../templates/')

spirit/config/development.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44

55
class DevelopmentConfig(Config):
6-
pass
6+
def __init__(self):
7+
Config.__init__(self)
8+
self.xsrf_cookies = False
79

810

911
config = DevelopmentConfig()

spirit/controllers/wechat.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# -*- coding: utf-8 -*-
2+
# import tornado.web
3+
from wechatpy import create_reply, parse_message
4+
from wechatpy.utils import check_signature
5+
from wechatpy.exceptions import InvalidSignatureException
6+
7+
from ._base import BaseHandler
8+
9+
10+
class InterfaceHandler(BaseHandler):
11+
def get(self):
12+
signature = self.get_argument('signature')
13+
timestamp = self.get_argument('timestamp')
14+
nonce = self.get_argument('nonce')
15+
echostr = self.get_argument('echostr')
16+
17+
token = 'spirit'
18+
19+
try:
20+
check_signature(token, signature, timestamp, nonce)
21+
except InvalidSignatureException:
22+
# 处理异常情况或忽略
23+
pass
24+
self.write(echostr)
25+
26+
def post(self):
27+
data = self.request.body
28+
msg = parse_message(data)
29+
30+
if msg.type == 'text':
31+
reply = create_reply(msg.content, msg)
32+
else:
33+
reply = create_reply('Sorry, can not handle this for now', msg)
34+
self.write(reply.render())

spirit/routers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# -*- coding: utf-8 -*-
2-
from .controllers import site
2+
from .controllers import site, wechat
33

44

55
routers = [
6-
(r"/", site.IndexHandler)
6+
(r"/", site.IndexHandler),
7+
(r"/wx_interface", wechat.InterfaceHandler)
78
]

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# and then run "tox" from this directory.
55

66
[tox]
7-
envlist = py27, pypy
7+
envlist = py27
88

99
[base]
1010
deps =

0 commit comments

Comments
 (0)