-
Notifications
You must be signed in to change notification settings - Fork 122
/
api.py
35 lines (25 loc) · 955 Bytes
/
api.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
from flask import Flask, request
__author__ = 'enginebai'
API_ROOT = 'api/'
FB_WEBHOOK = 'fb_webhook'
app = Flask(__name__)
@app.route(API_ROOT + FB_WEBHOOK, methods=["GET"])
def fb_webhook():
verification_code = 'I_AM_VERIFICIATION_CODE'
verify_token = request.args.get('hub.verify_token')
if verification_code == verify_token:
return request.args.get('hub.challenge')
@app.route(API_ROOT + FB_WEBHOOK, methods=['POST'])
def fb_receive_message():
message_entries = json.loads(request.data.decode('utf8'))['entry']
for entry in message_entries:
for message in entry['messaging']:
if message.get('message'):
print("{sender[id]} says {message[text]}".format(**message))
return "Hi"
if __name__ == '__main__':
context = ('ssl/fullchain.pem', 'ssl/privkey.pem')
app.run(host='0.0.0.0', debug=True, ssl_context=context)