Skip to content

Commit

Permalink
Add Quick Replies support
Browse files Browse the repository at this point in the history
  • Loading branch information
swapagarwal committed Nov 1, 2016
1 parent b5deccd commit 5128bf1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
21 changes: 11 additions & 10 deletions jarvis.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,23 @@ def webhook():
messaging_events = data['entry'][0]['messaging']
for event in messaging_events:
sender = event['sender']['id']
message = None
if 'message' in event and 'text' in event['message']:
text = event['message']['text']
payload = {
'recipient': {
'id': sender
},
'message': modules.search(text)
}
r = requests.post('https://graph.facebook.com/v2.6/me/messages', params={'access_token': ACCESS_TOKEN}, json=payload)
if 'quick_reply' in event['message'] and 'payload' in event['message']['quick_reply']:
quick_reply_payload = event['message']['quick_reply']['payload']
message = modules.search(quick_reply_payload, postback=True)
else:
text = event['message']['text']
message = modules.search(text)
if 'postback' in event and 'payload' in event['postback']:
postback = event['postback']['payload']
postback_payload = event['postback']['payload']
message = modules.search(postback_payload, postback=True)
if message is not None:
payload = {
'recipient': {
'id': sender
},
'message': modules.search(postback, postback=True)
'message': message
}
r = requests.post('https://graph.facebook.com/v2.6/me/messages', params={'access_token': ACCESS_TOKEN}, json=payload)
return '' # 200 OK
Expand Down
1 change: 1 addition & 0 deletions modules/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import config
import json
import os
import requests
import sys
Expand Down
18 changes: 18 additions & 0 deletions templates/quick_replies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from copy import deepcopy as copy

QUICK_REPLIES_LIMIT = 10
TITLE_CHARACTER_LIMIT = 20
PAYLOAD_CHARACTER_LIMIT = 1000

def add_quick_reply(message, title='', payload=''):
message_with_quick_reply = copy(message)
if 'quick_replies' not in message_with_quick_reply:
message_with_quick_reply['quick_replies'] = []
if len(message_with_quick_reply['quick_replies']) < QUICK_REPLIES_LIMIT:
quick_reply = {}
# TODO: location + image_url
quick_reply['content_type'] = 'text'
quick_reply['title'] = title[:TITLE_CHARACTER_LIMIT]
quick_reply['payload'] = payload[:PAYLOAD_CHARACTER_LIMIT]
message_with_quick_reply['quick_replies'].append(quick_reply)
return message_with_quick_reply

0 comments on commit 5128bf1

Please sign in to comment.