-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.py
62 lines (54 loc) · 2.2 KB
/
app.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
51
52
53
54
55
56
57
58
59
60
61
62
from flask import Flask, request, Response
import requests, json, random, os
app = Flask(__name__)
# env_variables
# token to verify that this bot is legit
verify_token = os.getenv('VERIFY_TOKEN', None)
# token to send messages through facebook messenger
access_token = os.getenv('ACCESS_TOKEN', None)
@app.route('/webhook', methods=['GET'])
def webhook_verify():
if request.args.get('hub.verify_token') == verify_token:
return request.args.get('hub.challenge')
return "Wrong verify token"
@app.route('/webhook', methods=['POST'])
def webhook_action():
data = json.loads(request.data.decode('utf-8'))
for entry in data['entry']:
user_message = entry['messaging'][0]['message']['text']
user_id = entry['messaging'][0]['sender']['id']
response = {
'recipient': {'id': user_id},
'message': {}
}
response['message']['text'] = handle_message(user_id, user_message)
r = requests.post(
'https://graph.facebook.com/v2.6/me/messages/?access_token=' + access_token, json=response)
return Response(response="EVENT RECEIVED",status=200)
@app.route('/webhook_dev', methods=['POST'])
def webhook_dev():
# custom route for local development
data = json.loads(request.data.decode('utf-8'))
user_message = data['entry'][0]['messaging'][0]['message']['text']
user_id = data['entry'][0]['messaging'][0]['sender']['id']
response = {
'recipient': {'id': user_id},
'message': {'text': handle_message(user_id, user_message)}
}
return Response(
response=json.dumps(response),
status=200,
mimetype='application/json'
)
def handle_message(user_id, user_message):
# DO SOMETHING with the user_message ... ¯\_(ツ)_/¯
return "Hello "+user_id+" ! You just sent me : " + user_message
@app.route('/privacy', methods=['GET'])
def privacy():
# needed route if you need to make your bot public
return "This facebook messenger bot's only purpose is to [...]. That's all. We don't use it in any other way."
@app.route('/', methods=['GET'])
def index():
return "Hello there, I'm a facebook messenger bot."
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')