├── HelloWorldBot.py ├── Procfile ├── README.md └── requirements.txt /HelloWorldBot.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, request 2 | import requests 3 | import json 4 | import traceback 5 | import random 6 | app = Flask(__name__) 7 | 8 | token = "" 9 | 10 | @app.route('/webhook', methods=['GET', 'POST']) 11 | def webhook(): 12 | if request.method == 'POST': 13 | try: 14 | data = json.loads(request.data) 15 | text = data['entry'][0]['messaging'][0]['message']['text'] # Incoming Message Text 16 | sender = data['entry'][0]['messaging'][0]['sender']['id'] # Sender ID 17 | payload = {'recipient': {'id': sender}, 'message': {'text': "Hello World"}} # We're going to send this back 18 | r = requests.post('https://graph.facebook.com/v2.6/me/messages/?access_token=' + token, json=payload) # Lets send it 19 | except Exception as e: 20 | print traceback.format_exc() # something went wrong 21 | elif request.method == 'GET': # For the initial verification 22 | if request.args.get('hub.verify_token') == '': 23 | return request.args.get('hub.challenge') 24 | return "Wrong Verify Token" 25 | return "Hello World" #Not Really Necessary 26 | 27 | if __name__ == '__main__': 28 | app.run(debug=True) 29 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: gunicorn HelloWorldBot:app 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Flask-HelloWorldBot 2 | A Basic Template for A FB Messenger Bot created in Flask 3 | 4 | Steps to get up and running: 5 | 6 | 1) Create a new FB App https://developers.facebook.com/quickstarts/?platform=web 7 | 8 | 2) Create a new FB Page https://www.facebook.com/pages/create 9 | 10 | 3) Go to your app and to the messengers tab on the sidebar 11 | 12 | 4) Generate a token for your page 13 | 14 | 5) In HelloWorldBot.py replace '\' with that token 15 | 16 | 6) In HelloWorldBot.py replace '\' with any string you like 17 | 18 | 7) Deploy your app, you need a https url, so deploying to Heroku is easiest, should work out of the box 19 | 20 | 8) Click 'Setup Webhooks' back in the Facebook Developers page (still in the Messenger tab), and the Callback URL should be YOUR_URL + '/webhook' 21 | 22 | REMEMBER: It needs to be https 23 | 24 | Your Verify Token is the string you chose in step 6 25 | 26 | At least check 'messages' in Subscription Fields, others are optional 27 | 28 | 9) Verify and Save 29 | 30 | 10) If you did it correctly, your page should respond to every message from YOU to the page with "Hello World" 31 | Note that this will not work for anyone else until you submit your app for review 32 | 33 | Using this as a reference can help: https://developers.facebook.com/docs/messenger-platform/quickstart 34 | 35 | No experience with Heroku? Its super easy: https://devcenter.heroku.com/articles/getting-started-with-python#introduction 36 | 37 | This seems like another tutorial that goes more in-depth (although in Node.js but should be fairly easy to translate, maybe?) - https://github.com/jw84/messenger-bot-tutorial 38 | 39 | I don't have much more experience with bots than this, but I would be happy to try and answer any questions you have! You can contact me at reparadocs (at) gmail (dot) com 40 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask==0.10.1 2 | gunicorn==19.4.5 3 | itsdangerous==0.24 4 | Jinja2==2.8 5 | MarkupSafe==0.23 6 | requests==2.9.1 7 | Werkzeug==0.11.5 8 | wheel==0.24.0 9 | --------------------------------------------------------------------------------