-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwechat.py
56 lines (45 loc) · 1.63 KB
/
wechat.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
#!/usr/bin/env python
#-*- coding=utf-8 -*-
import hashlib
import time
import urllib, urllib2
from lxml import etree
import web
from fanfou import sendtext
import hanzi
import photo
render = web.template.render('templates/')
class wechatmsg:
def GET(self):
token = "fanfou"
params = web.input()
args = [token, params['timestamp'], params['nonce']]
args.sort()
if hashlib.sha1("".join(args)).hexdigest() == params['signature']:
if params.has_key('echostr'):
return params['echostr']
def POST(self):
str_xml = web.data()
xml = etree.fromstring(str_xml)
msgType = xml.find("MsgType").text
fromUser= xml.find("FromUserName").text
toUser = xml.find("ToUserName").text
if msgType=='event':
event = xml.find("Event").text
if event == 'subscribe' :
return render.weixin(
fromUser, toUser, int(time.time()), hanzi.hello)
elif msgType=='text' :
content = xml.find("Content").text
sendtext(content)
return render.weixin(fromUser,toUser,int(time.time()),hanzi.txtok)
elif msgType=='image':
pic_url = xml.find("PicUrl").text
msg_id = xml.find("MsgId").text
info = photo.save(pic_url, msg_id)
if info == 1:
return render.weixin(fromUser,toUser,int(time.time()),hanzi.picok)
else:
return render.weixin(fromUser,toUser,int(time.time()),info)
else:
return render.weixin(fromUser,toUser,int(time.time()),hanzi.cnfse)