forked from reparadocs/Flask-HelloWorldBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloWorldBot.py
More file actions
28 lines (25 loc) · 1.06 KB
/
HelloWorldBot.py
File metadata and controls
28 lines (25 loc) · 1.06 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
from flask import Flask, request
import requests
import json
import traceback
import random
app = Flask(__name__)
token = "<ACCESS_TOKEN_HERE>"
@app.route('/webhook', methods=['GET', 'POST'])
def webhook():
if request.method == 'POST':
try:
data = json.loads(request.data)
text = data['entry'][0]['messaging'][0]['message']['text'] # Incoming Message Text
sender = data['entry'][0]['messaging'][0]['sender']['id'] # Sender ID
payload = {'recipient': {'id': sender}, 'message': {'text': "Hello World"}} # We're going to send this back
r = requests.post('https://graph.facebook.com/v2.6/me/messages/?access_token=' + token, json=payload) # Lets send it
except Exception as e:
print traceback.format_exc() # something went wrong
elif request.method == 'GET': # For the initial verification
if request.args.get('hub.verify_token') == '<VERIFY_TOKEN_HERE>':
return request.args.get('hub.challenge')
return "Wrong Verify Token"
return "Hello World" #Not Really Necessary
if __name__ == '__main__':
app.run(debug=True)