-
Notifications
You must be signed in to change notification settings - Fork 23
/
handle.py
70 lines (60 loc) · 2.37 KB
/
handle.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
66
67
68
69
70
# -*- coding: utf-8 -*-
# filename: handle.py
import hashlib
import reply
import receive
import web
class Handle(object):
def GET(self):
try:
data = web.input()
if len(data) == 0:
return "hello, this is handle view"
# 首次绑定公众号时需要对签名进行验证
signature = data.signature
timestamp = data.timestamp
nonce = data.nonce
echostr = data.echostr
token = "xxxxxxxx" # 请按照公众平台官网\基本配置中信息填写
my_list = [token, timestamp, nonce]
my_list.sort()
sha1 = hashlib.sha1()
map(sha1.update, my_list)
hashcode = sha1.hexdigest()
print "handle/GET func: hashcode, signature: ", hashcode, signature # 打印后台日志
if hashcode == signature:
return echostr
else:
return ""
except Exception, Argument:
return Argument
def POST(self):
try:
webData = web.data()
print "Handle Post webdata is \n", webData # 打印后台日志
recMsg = receive.parse_xml(webData)
if isinstance(recMsg, receive.Msg):
toUser = recMsg.FromUserName
fromUser = recMsg.ToUserName
if recMsg.MsgType == 'text':
content = recMsg.Content
replyMsg = reply.TextMsg(toUser, fromUser, content)
return replyMsg.send()
if recMsg.MsgType == 'image':
mediaId = recMsg.MediaId
replyMsg = reply.ImageMsg(toUser, fromUser, mediaId)
return replyMsg.send()
else:
return reply.Msg().send()
if isinstance(recMsg, receive.EventMsg):
toUser = recMsg.FromUserName
fromUser = recMsg.ToUserName
if recMsg.Event == 'CLICK':
print 'It is a CLICK event'
content = u'功能正在开发中,敬请期待..'.encode('utf-8')
replyMsg = reply.TextMsg(toUser, fromUser, content)
return replyMsg.send()
print "暂且不处理"
return reply.Msg().send()
except Exception, Argment:
return Argment