-
Notifications
You must be signed in to change notification settings - Fork 6
/
responder.py
202 lines (182 loc) · 6.26 KB
/
responder.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
################################################
# responder.py #
# Defines specific outbound customer messages. #
# #
################################################
from config import *
from order import *
import json
import requests
def sendWelcomeMessage(name, phone):
payload = json.dumps({
"recipient_type": "individual",
"to": phone,
"type": "text",
"text": {
"body": "Hello, " + name + "Welcome to SoftRock Café! I am ready to take your order."
}
})
response = requests.request("POST", WHATSAPP_API_SERVER + "/messages", headers=headers, data=payload)
def sendTextMessage(userId, text):
payload = json.dumps({
"recipient_type": "individual",
"to": userId,
"type": "text",
"text": {
"body": text
}
})
response = requests.request("POST", WHATSAPP_API_SERVER + "/messages", headers=headers, data=payload)
def sendPlateSelection(userId, text, footer, plates):
jsonresults = json.loads(plates)
payload = json.dumps({
"recipient_type": "individual",
"to": userId,
"type": "interactive",
"interactive": {
"type": "button",
"body": {
"text": text
},
"footer": {
"text": footer
},
"action": {
"buttons": [
{
"type": "reply",
"reply": {
"id": "plate-id-0",
"title": jsonresults["options"][0]["name"] + " - " + str(jsonresults["options"][0]["price"])
}
},
{
"type": "reply",
"reply": {
"id": "plate-id-1",
"title": jsonresults["options"][1]["name"] + " - " + str(jsonresults["options"][1]["price"])
}
},
{
"type": "reply",
"reply": {
"id": "plate-id-2",
"title": jsonresults["options"][2]["name"] + " - " + str(jsonresults["options"][2]["price"])
}
}
]
}
}
})
response = requests.request("POST", WHATSAPP_API_SERVER + "/messages", headers=headers, data=payload)
print(response.text)
def sendYesNo(userId, text):
payload = json.dumps({
"recipient_type": "individual",
"to": userId,
"type": "interactive",
"interactive": {
"type": "button",
"body": {
"text": text
},
"action": {
"buttons": [
{
"type": "reply",
"reply": {
"id": "checkout-ready-yes",
"title": "Yes"
}
},
{
"type": "reply",
"reply": {
"id": "checkout-ready-no",
"title": "No"
}
}
]
}
}
})
response = requests.request("POST", WHATSAPP_API_SERVER + "/messages", headers=headers, data=payload)
print(response.text)
def sendDeferredCheckout(userId, text):
payload = json.dumps({
"recipient_type": "individual",
"to": userId,
"type": "interactive",
"interactive": {
"type": "button",
"body": {
"text": text
},
"action": {
"buttons": [
{
"type": "reply",
"reply": {
"id": "deferred-id-accepted",
"title": "Yes"
}
},
{
"type": "reply",
"reply": {
"id": "deferred-id-rejected",
"title": "No"
}
}
]
}
}
})
response = requests.request("POST", WHATSAPP_API_SERVER + "/messages", headers=headers, data=payload)
print(response.text)
def sendPaymentSelection(userId):
payload = json.dumps({
"recipient_type": "individual",
"to": userId,
"type": "interactive",
"interactive": {
"type": "button",
"body": {
"text": "Select your payment method"
},
"action": {
"buttons": [
{
"type": "reply",
"reply": {
"id": "payment-id-0",
"title": "I pay"
}
},
{
"type": "reply",
"reply": {
"id": "payment-id-1",
"title": "Ask a friend"
}
},
{
"type": "reply",
"reply": {
"id": "payment-id-2",
"title": "Split with friends"
}
}
]
}
}
})
response = requests.request("POST", WHATSAPP_API_SERVER + "/messages", headers=headers, data=payload)
print(response.text)
def showOrder(userId):
jsonOrder = json.loads(getOrderSelected(userId))
total = 0
for item in jsonOrder['selection']:
responder.sendTextMessage(userId, item["name"] + " - " + str(item["price"]))
total += item["price"]
responder.sendTextMessage(userId, "Total: " + str(total))