forked from enginebai/PyMessager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
38 lines (28 loc) · 1.04 KB
/
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
36
37
38
#!/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:
messagings = entry['messaging']
for message in messagings:
sender = message['sender']['id']
if message.get('message'):
text = message['message']['text']
print("{} says {}".format(sender, text))
return "Hi"
if __name__ == '__main__':
context = ('ssl/fullchain.pem', 'ssl/privkey.pem')
app.run(host='0.0.0.0', debug=True, ssl_context=context)