-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
63 lines (55 loc) · 2.9 KB
/
main.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import time
import os
from flask import Flask, request, abort, render_template
from flask import make_response
from wechatpy import parse_message, create_reply
from wechatpy.utils import check_signature
from wechatpy.exceptions import InvalidSignatureException
from wechatpy.exceptions import InvalidAppIdException
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
app = Flask(__name__)
@app.route('/', methods = ['GET', 'POST'] )
def wechat_auth():
if request.method == 'GET':
token = 'zzl.foo'
query = request.args
signature = query.get('signature', '')
timestamp = query.get('timestamp', '')
nonce = query.get('nonce', '')
echostr = query.get('echostr', '')
print(signature)
try:
check_signature(token, signature, timestamp, nonce)
except InvalidSignatureException:
abort(403)
return make_response(echostr)
else:
rec = request.stream.read()
xml_rec = ET.fromstring(rec)
msgtype = xml_rec.find('MsgType').text
tou = xml_rec.find('ToUserName').text
fromu = xml_rec.find('FromUserName').text
xml_rep_img = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>1</ArticleCount><Articles><item><Title><![CDATA[%s]]></Title><Description><![CDATA[%s]]></Description><PicUrl><![CDATA[%s]]></PicUrl></item></Articles><FuncFlag>1</FuncFlag></xml>"
xml_rep_mutiimg = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>6</ArticleCount><Articles><item><Title><![CDATA[%s]]></Title><PicUrl><![CDATA[%s]]></PicUrl></item><item><Title><![CDATA[我的冰箱]]></Title><Url><![CDATA[%s]]></Url></item><item><Title><![CDATA[定制早餐]]></Title><Url><![CDATA[%s]]></Url></item><item><Title><![CDATA[定制午餐]]></Title><Url><![CDATA[%s]]></Url></item><item><Title><![CDATA[定制晚餐]]></Title><Url><![CDATA[%s]]></Url></item><item><Title><![CDATA[结伴购物]]></Title><Url><![CDATA[%s]]></Url></item></Articles></xml>"
#用户一旦关注改公众账号,自动回复以下图文消息
if msgtype == "event":
msgcontent = xml_rec.find('Event').text
if msgcontent == "subscribe":
msgcontent = 'Welcome!'
else:
msgcontent = 'error'
msg_title = u"美食助手,您的私人定制"
msg_imag_url = "http://gourmetmaster.sinaapp.com/static/main_meitu_3.jpg"
response = make_response(xml_rep_img % (fromu,tou,str(int(time.time())),msg_title,msgcontent,msg_imag_url))
response.content_type='application/xml'
return response
if __name__ == '__main__':
server_ip = '0.0.0.0'
server_port = 80
app.run(host=server_ip, port=server_port, debug=True)