forked from cloverstd/flask-wechatpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
50 lines (36 loc) · 1.11 KB
/
demo.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
#!/usr/bin/env python
# encoding: utf-8
from flask import Flask, request, session
from flask_wechatpy import Wechat, wechat_required, oauth
from wechatpy.replies import TextReply
from wechatpy.replies import create_reply
app = Flask(__name__)
app.config['WECHAT_APPID'] = 'wx186958a84617e867'
app.config['WECHAT_SECRET'] = '12e75aabd90ab2e034941f61f0c8d0aa'
app.config['WECHAT_TOKEN'] = 'token'
app.config['DEBUG'] = True
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
wechat = Wechat(app)
@app.route('/')
@oauth(scope='snsapi_userinfo')
def index():
return "hello"
@app.route('/clear')
def clear():
if 'wechat_user_id' in session:
session.pop('wechat_user_id')
return "ok"
@app.route('/wechat', methods=['GET', 'POST'])
@wechat_required
def wechat_handler():
msg = request.wechat_msg
if msg.type == 'text':
reply = create_reply(msg.content, message=msg)
else:
reply = TextReply(content='hello', message=msg)
return reply
@app.route('/access_token')
def access_token():
return "access token: {}".format(wechat.access_token)
if __name__ == '__main__':
app.run()