-
Notifications
You must be signed in to change notification settings - Fork 2
/
ResponseChat.py
162 lines (126 loc) · 5.01 KB
/
ResponseChat.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import json
from time import time
class ResponseChat:
def __init__(self, loggerFileName=None):
self.data = {"bot_state": str(time()), "data": []}
self.logger = Logger(loggerFileName) if loggerFileName else None
def set_bot_state(self, state=""):
self.data['bot_state'] = state
def send_text(self, msg=""):
self.logger.debug(locals()) if self.logger else None
self.data['data'].append({"type": "text", "body": {"data": msg}})
def send_document(self, link="", filename="", mimeType="", caption=""):
self.logger.debug(locals()) if self.logger else None
self.data['data'].append({"type": "document", "body": {"link": link, "filename": filename, "mime_type": mimeType, "caption": caption}})
def send_image(self, link="", mimeType="", caption=""):
self.data['data'].append({"type": "image", "body": {"link": link, "mime_type": mimeType, "caption": caption}})
def send_audio(self, link="", mimeType="", caption=""):
self.data['data'].append({"type": "audio", "body": {"link": link, "mime_type": mimeType, "caption": caption}})
def send_location(self, latitude="", longitude="", name="", address=""):
self.data['data'].append({"type": "location", "body": {"latitude": latitude, "longitude": longitude, "name": name, "address": address}})
def send_video(self, link="", mimeType="", caption=""):
self.data['data'].append({"type": "video", "body": {"link": link, "mime_type": mimeType, "caption": caption}})
def add_contact(self, contact_object):
self.data['data'].append({"type": "Contact", "Contact": contact_object})
def send_agent_transfer(self, skillName="", uui=""):
self.logger.debug(locals()) if self.logger else None
self.data['data'].append({"type": "CCTransfer", "skillName": skillName, "uui": uui})
def init_interactive(self, name):
return ListObject(name)
def add_interactive_object(self, list_object):
self.logger.debug(locals()) if self.logger else None
self.data['data'].append({"type": "interactive", "body": list_object.build_object()})
def add_buttons(self, list_object):
self.logger.debug(locals()) if self.logger else None
self.data['data'].append({"type": "interactive", "body": list_object.build_object()})
def get_data(self):
return json.dumps(self.data)
def get_response(self):
return self.data
def put_test_data(self, data):
self.data = data
def get_xml(self):
# Code for generating XML representation of data
pass
def print_data(self):
return json.dumps(self.data)
def send(self):
self.logger.info("whatsapp post data:::" + json.dumps(self.data, indent=4)) if self.logger else None
print(self.get_data())
class Menu:
def __init__(self, type, title, link):
self.type = type
self.title = title
self.link = link
self.buttons = []
def add_choice(self, id, title):
self.buttons.append({"id": id, "title": title})
def return_menu_payload(self):
payload = {
"type": self.type,
"title": self.title,
"link": self.link,
"choices": self.buttons
}
return payload
def return_button_payload_image(self, image):
payload = {
"type": self.type,
self.type: {
"type": "button",
"header": {
"type": "image",
"image": {
"link": image
}
},
"body": {
"text": self.title
},
"action": {
"buttons": self.buttons
}
}
}
self.buttons = []
return payload
class ButtonObject:
def __init__(self, title):
self.body = title
self.buttons = []
def add_button(self, id, title):
self.buttons.append({"id": id, "title": title})
def build_object(self):
payload = {
"type": "button",
"title": self.body,
"choices": self.buttons
}
return payload
class ListObject:
def __init__(self, title):
self.body = title
self.sections = []
def section(self, title):
section = ListObjectSection(title)
self.sections.append(section)
return section
def build_object(self):
payload = {
"type": "list",
"title": self.body,
"sections": [section.build_object() for section in self.sections]
}
return payload
class ListObjectSection:
def __init__(self, title):
self.title = title
self.choices = []
def add_choice(self, id, title, description=""):
self.choices.append({"id": id, "title": title, "description": description})
def build_object(self):
payload = {
"title": self.title,
"choices": self.choices
}
return payload