-
Notifications
You must be signed in to change notification settings - Fork 33
/
facebook_template_lib.py
73 lines (60 loc) · 2.61 KB
/
facebook_template_lib.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#==============================================================================
# title : facebook_template_lib.py
# description : This library is used to prepare custom payload for Facebook Messenger generic template with elements like image, title, subtitle, URL button and postback button. It will automatically create carousel if more than 1 generic templates are passed.
# author : Pragnakalp Techlabs
# email : letstalk@pragnakalp.com
# website : https://www.pragnakalp.com
#==============================================================================
class FacebookTemplate():
'''Create generic facebook messenger tempalte. '''
def __init__(self):
self.payload = {
"facebook": {
"attachment": {
"type": "template",
"payload": {
"template_type":"generic",
"elements": [
]
}
}
}
}
def add_element(self, element_obj):
self.payload['facebook']['attachment']['payload']['elements'].append(element_obj)
def get_payload(self):
return self.payload
class TemplateElement():
''' Add title, subtitle, image, buttons and action elements in generic template response.'''
def __init__(self, title, subtitle):
self.element = {
"title": title,
"subtitle": subtitle
}
def add_image_url(self, url):
self.element['image_url'] = url
def add_default_action(self, url, type, webview_height_ratio):
self.element["default_action"]={
"url":url,
"type": type,
"webview_height_ratio": webview_height_ratio
}
def add_button(self, button_obj):
self.element['buttons']=[button_obj]
def get_element(self):
return self.element
class TemplateElementButton():
'''Types of buttons that can be added in response.'''
def __init__(self, button_type, title):
self.button = {
"type": button_type,
"title": title
}
def add_web_url(self, url):
assert self.button['type']=='web_url', "Error: button type must be 'web_url'"
self.button['url'] = url
def add_payload(self, payload):
assert self.button['type']=='postback', "Error:button type must be 'postback'"
self.button['payload'] = payload
def get_button(self):
return self.button