Skip to content

Commit

Permalink
Add button template (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
swapagarwal committed Apr 24, 2016
1 parent 19ef5ef commit 20e8828
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Some advanced features:
├── modules/ # home for various features
├── modules/src/ # code goes here
├── modules/tests/ # tests go here
├── templates/ # for sending structured messages
├── CONTRIBUTING.md # contributing guidelines
└── jarvis.py # the main bot
```
Expand Down
52 changes: 52 additions & 0 deletions templates/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from copy import copy

button = {
'type': '',
'title': '',
'url': '',
'payload': ''
}

def web_url(title, url):
web_url_button = copy(button)
web_url_button['type'] = 'web_url'
web_url_button['title'] = title
web_url_button['url'] = url
return web_url_button

def postback(title, payload):
postback_button = copy(button)
postback_button['type'] = 'postback'
postback_button['title'] = title
postback_button['payload'] = payload
return postback_button

template = {
'template_type': 'button',
'value': {
'attachment': {
'type': 'template',
'payload': {
'template_type': 'button',
'text': '',
'buttons': []
}
}
}
}

class ButtonTemplate:
def __init__(self, text=''):
self.template = template['value']
self.text = text
def add_web_url(self, title='', url=''):
web_url_button = web_url(title, url)
self.template['attachment']['payload']['buttons'].append(web_url_button)
def add_postback(self, title='', payload=''):
postback_button = postback(title, payload)
self.template['attachment']['payload']['buttons'].append(postback_button)
def set_text(self, text=''):
self.text = text
def get_message(self):
self.template['attachment']['payload']['text'] = self.text
return self.template

0 comments on commit 20e8828

Please sign in to comment.