-
Notifications
You must be signed in to change notification settings - Fork 2
/
dummy.py
46 lines (37 loc) · 1.27 KB
/
dummy.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
import requests
import json
import os
import logging
from flask import Flask, request, render_template
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ['FLASK_SECRET_KEY']
ACCESS_TOKEN = os.environ['FB_ACCESS_TOKEN']
@app.route('/MIT-Hodor', methods=['POST','GET'])
def main():
challenge = request.args.get("hub.challenge")
if challenge:
app.logger.info('Recieved webhook verification (hub.challenge)')
return challenge
req = request.get_json(silent=True)
uid = (req['entry'][0]['messaging'][0]['sender']['id'])
try:
message_recieved = (req['entry'][0]['messaging'][0]['message']['text'])
except Exception as e:
app.logger.error('Exception {} faced when recieved {} from uid:{}'.format(str(e), req, uid))
## handle_exception(uid, message_recieved)
send_message(uid, 'Coming back, soon')
return 'OK'
def send_message(uid, message):
app.logger.info('sending {} to uid:{}'.format(message, uid))
data = {
"messaging_type": "RESPONSE",
"recipient": {
"id": uid
},
"message": {
"text": message
}
}
return requests.post('https://graph.facebook.com/v2.6/me/messages?access_token={}'.format(ACCESS_TOKEN), json=data)
if __name__ == '__main__':
app.run(debug=True, port=8999)