-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
67 lines (48 loc) · 1.96 KB
/
Copy pathserver.py
File metadata and controls
67 lines (48 loc) · 1.96 KB
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
# coding: utf-8
from flask import Flask, request, send_from_directory, render_template
from config import CONFIG
import messenger
app = Flask(__name__)
@app.route('/webhook', methods=['GET'])
def validate():
if request.args.get('hub.mode', '') == 'subscribe' and \
request.args.get('hub.verify_token', '') == CONFIG['VERIFY_TOKEN']:
print("Validating webhook")
return request.args.get('hub.challenge', '')
else:
return 'Failed validation. Make sure the validation tokens match.'
@app.route('/webhook', methods=['POST'])
def webhook():
payload = request.get_data(as_text=True)
for event in messenger.messaging_events(payload):
if 'optin' in event:
messenger.received_authentication(event)
elif 'message' in event:
messenger.received_message(event)
elif 'delivery' in event:
messenger.received_delivery_confirmation(event)
elif 'postback' in event:
messenger.received_postback(event)
elif 'read' in event:
messenger.received_message_read(event)
elif 'account_linking' in event:
messenger.received_account_link(event)
else:
print("Webhook received unknown messagingEvent:", event)
return "ok"
@app.route('/authorize', methods=['GET'])
def authorize():
account_linking_token = request.args.get('account_linking_token', '')
redirect_uri = request.args.get('redirect_uri', '')
auth_code = '1234567890'
redirect_uri_success = redirect_uri + "&authorization_code=" + auth_code
return render_template('authorize.html', data={
'account_linking_token': account_linking_token,
'redirect_uri': redirect_uri,
'redirect_uri_success': redirect_uri_success
})
@app.route('/assets/<path:path>')
def assets(path):
return send_from_directory('assets', path)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=False, threaded=True)