-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenerate.py
111 lines (96 loc) · 3.79 KB
/
generate.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from .exception import CharacterCountExceeded
class Generate:
@staticmethod
def button(button_type, title="None", url=None, payload=None, webview_height="full"):
"""
Creates a button and returns the dictionary containing the button data
.. see also:https://developers.facebook.com/docs/messenger-platform/send-api-reference/buttons
For more info https://developers.facebook.com/docs/messenger-platform/send-api-reference/buttons
:arg button_type: Type of button.
:type button_type: str
:arg title: Title of the button. Max 20 characters.
:type title: str
:arg url: This URL is opened in a mobile browser when the button is tapped.
:type url: str
:arg payload: This data will be sent back to your webhook. 1000 character limit.
:type payload: str
:arg webview_height: Height of the Webview. Valid values: compact,tall,full.
:type webview_height: str
:returns: A dictionary containing mapping between proper keys and arguments.
:rtype: dict
"""
if len(payload) > 1000:
raise CharacterCountExceeded("Max number of characters allowed are 1000."
"The number of characters in the 'payload' are %s. " % len(payload))
button = {
"type": type,
"title": title,
}
if button_type == "web_url":
button["url"] = url
button["webview_height_ratio"] = webview_height
elif button_type == "postback":
button["payload"] = payload
elif button_type == "phone_number":
button["payload"] = payload
elif button_type == "element_share":
button.pop("title")
elif button_type == "account_link":
button["url"] = url
button.pop("title")
elif button_type == "account_unlink":
button.pop("title")
return button
@staticmethod
def element(title, subtitle=None, image_url=None, buttons=None):
"""
Generates payload for element
:param title: Bubble title.
:type title: str
:param subtitle: Bubble subtitle.
:type subtitle: str
:param image_url: Bubble image.
:type image_url: str
:param buttons: Set of buttons that appear as call-to-actions.
:type buttons: list
:return: A dictionary containing mapping between proper keys and arguments.
:rtype: dict
"""
element = {
"title": title,
"subtitle": subtitle,
"image_url": image_url,
}
if subtitle is None:
element.pop("subtitle")
if image_url is None:
element.pop("image_url")
if buttons is not None:
element["buttons"] = buttons
print(buttons)
return element
@staticmethod
def quick_reply(content_type, title=None, payload=None, image_url=None):
"""
This method creates a dict for a quick_reply
:param content_type: text or location
:type content_type: str
:param title: Caption of button
:type title: str
:param payload: Custom data that will be sent back to you via webhook
:type payload: str
:param image_url: URL of image for text quick replies
:type image_url: str
:return: dict containing quick_reply
:rtype: dict
"""
quick_reply = {
"content_type": content_type,
}
if content_type == "text":
if (title or payload) is None:
raise ValueError
quick_reply["title"] = title
quick_reply["payload"] = payload
quick_reply["image_url"] = image_url
return quick_reply