├── README.md ├── Procfile ├── app.pyc ├── requirements.txt ├── readme.md └── app.py /README.md: -------------------------------------------------------------------------------- 1 | # facebook-bot-flask -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: gunicorn app:app --log-file=- -------------------------------------------------------------------------------- /app.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Vaibhavs10/facebook-bot-flask/master/app.pyc -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask==0.11.1 2 | Jinja2==2.8 3 | MarkupSafe==0.23 4 | Werkzeug==0.11.10 5 | click==6.6 6 | gunicorn==19.6.0 7 | itsdangerous==0.24 8 | requests==2.10.0 9 | wsgiref==0.1.2 10 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Facebook Messenger Bot 2 | This is a simple python template that uses Flask to build a webhook for Facebook's Messenger Bot API. 3 | 4 | Read more in my [tutorial that uses this repository](https://blog.hartleybrody.com/fb-messenger-bot/) -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import json 4 | import requests 5 | from flask import Flask, request 6 | 7 | app = Flask(__name__) 8 | 9 | 10 | @app.route('/', methods=['GET']) 11 | def verify(): 12 | # when the endpoint is registered as a webhook, it must echo back 13 | # the 'hub.challenge' value it receives in the query arguments 14 | if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"): 15 | if not request.args.get("hub.verify_token") == os.environ["VERIFY_TOKEN"]: 16 | return "Verification token mismatch", 403 17 | return request.args["hub.challenge"], 200 18 | 19 | return "Hello world", 200 20 | 21 | 22 | @app.route('/', methods=['POST']) 23 | def webhook(): 24 | 25 | # endpoint for processing incoming messaging events 26 | 27 | data = request.get_json() 28 | log(data) # you may not want to log every incoming message in production, but it's good for testing 29 | 30 | if data["object"] == "page": 31 | 32 | for entry in data["entry"]: 33 | for messaging_event in entry["messaging"]: 34 | 35 | if messaging_event.get("message"): # someone sent us a message 36 | 37 | sender_id = messaging_event["sender"]["id"] # the facebook ID of the person sending you the message 38 | recipient_id = messaging_event["recipient"]["id"] # the recipient's ID, which should be your page's facebook ID 39 | message_text = messaging_event["message"]["text"] # the message's text 40 | 41 | #send_message(sender_id, "We're still testing!") 42 | send_generic_message(sender_id) 43 | if messaging_event.get("delivery"): # delivery confirmation 44 | pass 45 | 46 | if messaging_event.get("optin"): # optin confirmation 47 | pass 48 | 49 | if messaging_event.get("postback"): # user clicked/tapped "postback" button in earlier message 50 | sender_id = messaging_event["sender"]["id"] # the facebook ID of the person sending you the message 51 | recipient_id = messaging_event["recipient"]["id"] # the recipient's ID, which should be your page's facebook ID 52 | payload = messaging_event["postback"]["payload"] # the message's text 53 | if payload == 'C': 54 | send_message(sender_id, "Working") 55 | else: 56 | send_message(sender_id, "We're glad you're helping us test") 57 | #send_message(sender_id, payload) 58 | 59 | return "ok", 200 60 | 61 | def send_generic_message(recipient_id): 62 | log("sending generic message to {recipient}: {text}".format(recipient=recipient_id, text="Generic Template")) 63 | 64 | params = { 65 | "access_token": os.environ["PAGE_ACCESS_TOKEN"] 66 | } 67 | headers = { 68 | "Content-Type": "application/json" 69 | } 70 | data = json.dumps({ 71 | "recipient": { 72 | "id": recipient_id 73 | }, 74 | "message": { 75 | "attachment": { 76 | "type": "template", 77 | "payload": { 78 | "template_type": "generic", 79 | "elements":[ 80 | { 81 | "title": "Hello!", 82 | "item_url": "http://vaibhavs10.in", 83 | "image_url": "http://loremflickr.com/100/100", 84 | "subtitle": "The solution to all your needs!", 85 | "buttons":[ 86 | { 87 | "type": "web_url", 88 | "url": "http://vaibhavs10.in", 89 | "title": "View Website" 90 | }, 91 | { 92 | "type": "postback", 93 | "title": "Start Chatting", 94 | "payload": "C" 95 | } 96 | ] 97 | } 98 | ] 99 | } 100 | } 101 | } 102 | }) 103 | r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data) 104 | if r.status_code != 200: 105 | log(r.status_code) 106 | log(r.text) 107 | 108 | 109 | def send_image_url(recipient_id, image_url): 110 | log("sending image file url to {recipient}: {text}".format(recipient=recipient_id, text=image_url)) 111 | 112 | params = { 113 | "access_token": os.environ["PAGE_ACCESS_TOKEN"] 114 | } 115 | headers = { 116 | "Content-Type": "application/json" 117 | } 118 | data = json.dumps({ 119 | "recipient": { 120 | "id": recipient_id 121 | }, 122 | "message": { 123 | "attachment": { 124 | "type": "image", 125 | "payload": { 126 | "url": image_url 127 | } 128 | } 129 | } 130 | }) 131 | r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data) 132 | if r.status_code != 200: 133 | log(r.status_code) 134 | log(r.text) 135 | 136 | 137 | def send_action(recipient_id, action): 138 | #Action can be "mark_seen", "typing_on", "typing_off" 139 | log("sending action to {recipient}: {text}".format(recipient=recipient_id, text=action)) 140 | 141 | params = { 142 | "access_token": os.environ["PAGE_ACCESS_TOKEN"] 143 | } 144 | headers = { 145 | "Content-Type": "application/json" 146 | } 147 | data = json.dumps({ 148 | "recipient": { 149 | "id": recipient_id 150 | }, 151 | "message": { 152 | "attachment": { 153 | "type": "file", 154 | "payload": { 155 | "url": file_url 156 | } 157 | } 158 | } 159 | }) 160 | r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data) 161 | if r.status_code != 200: 162 | log(r.status_code) 163 | log(r.text) 164 | 165 | 166 | def send_file_url(recipient_id, file_url): 167 | log("sending file url to {recipient}: {text}".format(recipient=recipient_id, text=file_url)) 168 | 169 | params = { 170 | "access_token": os.environ["PAGE_ACCESS_TOKEN"] 171 | } 172 | headers = { 173 | "Content-Type": "application/json" 174 | } 175 | data = json.dumps({ 176 | "recipient": { 177 | "id": recipient_id 178 | }, 179 | "message": { 180 | "attachment": { 181 | "type": "file", 182 | "payload": { 183 | "url": file_url 184 | } 185 | } 186 | } 187 | }) 188 | r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data) 189 | if r.status_code != 200: 190 | log(r.status_code) 191 | log(r.text) 192 | 193 | def quick_reply(recipient_id, message_text, text): 194 | log("sending quick message to {recipient}: {text}".format(recipient=recipient_id, text=message_text)) 195 | 196 | params = { 197 | "access_token": os.environ["PAGE_ACCESS_TOKEN"] 198 | } 199 | headers = { 200 | "Content-Type": "application/json" 201 | } 202 | data = json.dumps({ 203 | "recipient": { 204 | "id": recipient_id 205 | }, 206 | "message": { 207 | "text": message_text, 208 | "quick_replies":[ 209 | { 210 | "content_type": "text", 211 | "title": text, 212 | "payload": text 213 | } 214 | ] 215 | } 216 | }) 217 | r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data) 218 | if r.status_code != 200: 219 | log(r.status_code) 220 | log(r.text) 221 | 222 | def send_message(recipient_id, message_text): 223 | 224 | log("sending message to {recipient}: {text}".format(recipient=recipient_id, text=message_text)) 225 | 226 | params = { 227 | "access_token": os.environ["PAGE_ACCESS_TOKEN"] 228 | } 229 | headers = { 230 | "Content-Type": "application/json" 231 | } 232 | data = json.dumps({ 233 | "recipient": { 234 | "id": recipient_id 235 | }, 236 | "message": { 237 | "text": message_text 238 | } 239 | }) 240 | r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data) 241 | if r.status_code != 200: 242 | log(r.status_code) 243 | log(r.text) 244 | 245 | def send_message_button_web_url(recipient_id, message_text, web_url, title): 246 | 247 | log("sending button w/ web url to {recipient}: {text}".format(recipient=recipient_id, text=message_text)) 248 | 249 | params = { 250 | "access_token": os.environ["PAGE_ACCESS_TOKEN"] 251 | } 252 | headers = { 253 | "Content-Type": "application/json" 254 | } 255 | data = json.dumps({ 256 | "recipient": { 257 | "id": recipient_id 258 | }, 259 | "message": { 260 | "attachment": { 261 | "type": "template", 262 | "payload": { 263 | "template_type": "button", 264 | "text": message_text, 265 | "buttons": [ 266 | { 267 | "type": "web_url", 268 | "url": web_url, 269 | "title": title 270 | } 271 | ] 272 | } 273 | } 274 | } 275 | }) 276 | r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data) 277 | if r.status_code != 200: 278 | log(r.status_code) 279 | log(r.text) 280 | 281 | 282 | def log(message): # simple wrapper for logging to stdout on heroku 283 | print str(message) 284 | sys.stdout.flush() 285 | 286 | 287 | if __name__ == '__main__': 288 | app.run(debug=True) 289 | --------------------------------------------------------------------------------