-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwhatsappbot.go
302 lines (244 loc) · 9.37 KB
/
whatsappbot.go
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package menubotlib
import (
"errors"
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
"database/sql"
_ "github.com/lib/pq"
)
const (
custOrderInitState = "Initialized"
whatsAppServer = "s.whatsapp.net"
sayMenu = "For a command list please type & send-: menu?\nPlease include the question mark."
reminderGreeting = "Please save your email address, by typing & sending-: update email: example@emailprovider.com"
coldGreeting = "Hello there, I don't believe we've met before."
smartyPantsGreeting = "Hey there smarty pants, I see you've been here before."
noCommandText = "Err:NC, Sorry I couldn't identify a command in your mesasge."
unhandledCommandException = "Err:CF, Something went wrong processing your request."
updateOrderCommand = "update order X:newAmount"
UpdateOrderCommExpl = "Where X is the item's price list number, item order not important."
fullOrderExample = `An order of:
1 Tshirt,
2 hats,
1 Mug.
Should look like-: update order 1:1, 2:2, 3:1`
deleteOrder = "To remove an item from your order, use-: update order X:0"
shopComands = "to save your order please type & send-:" + updateOrderCommand + "\n" + UpdateOrderCommExpl +
"\n\n" + fullOrderExample +
"\n\n" + deleteOrder +
"\n\n" + "currentorder? - Prints your current pending order." +
"\n" + "To checkout type & send-: checkoutnow?"
queryCommands = `menu? - Prints this menu.
shop? - Prints the shop price list.
userinfo? - Prints your user info.`
updateCommands = `update email: newEmail
update nickname: newNickname
update consent: newConsent`
mainMenu = "Main Menu, command list:" +
"\n\n" + queryCommands +
"\n\n" + updateCommands
prclstPreamble = "Welcome to the Shop," +
"\n\n" + shopComands
)
type Command interface {
Execute(db *sql.DB, convo *ConversationContext, isAutoInc bool) error
}
type CommandCollection []Command
type UpdateUserInfoCommand struct {
Name string
Text string
}
type UpdateOrderCommand struct {
Text string
}
type QuestionCommand struct {
Name string
Text string
}
func (cmd UpdateUserInfoCommand) Execute(db *sql.DB, convo *ConversationContext, isAutoInc bool) error {
var colName = strings.TrimSpace(strings.TrimPrefix(cmd.Name, "update"))
err := convo.UserInfo.UpdateSingularUserInfoField(db, colName, cmd.Text)
if err != nil {
return fmt.Errorf("unhandled error updating user info: %v", err)
}
return errors.New("successfully updated user info." + colName + " to " + cmd.Text)
}
func (cmd UpdateOrderCommand) Execute(db *sql.DB, convo *ConversationContext, isAutoInc bool) error {
updates, err := ParseUpdateOrderCommand(cmd.Text)
if err != nil {
return fmt.Errorf("error parsing update answers command: %v", err)
}
err = convo.CurrentOrder.UpdateOrInsertCurrentOrder(db, convo.UserInfo.CellNumber, OrderItems{MenuIndications: updates}, isAutoInc)
if err != nil {
return fmt.Errorf("unhandled error updating order: %v", err)
}
return errors.New("successfully updated current order")
}
func (cmd QuestionCommand) Execute(db *sql.DB, convo *ConversationContext, isAutoInc bool) error {
return fmt.Errorf("%s", cmd.Text)
}
func BeginCheckout(db *sql.DB, ui UserInfo, ctlgselections []CatalogueSelection, c CustomerOrder, checkoutUrls CheckoutInfo, isAutoInc bool) string {
// Create a new URL object for each URL
returnURL, _ := url.Parse(checkoutUrls.ReturnURL)
cancelURL, _ := url.Parse(checkoutUrls.CancelURL)
notifyURL, _ := url.Parse(checkoutUrls.NotifyURL)
// Initialize checkoutURLs with the new URLs
checkoutUrls.ReturnURL = returnURL.String()
checkoutUrls.CancelURL = cancelURL.String()
checkoutUrls.NotifyURL = notifyURL.String()
//Tally the order and then create a CheckoutCart struct
cartTotal, cartSummary, err := c.TallyOrder(db, ui.CellNumber, ctlgselections, isAutoInc)
if err != nil {
return err.Error()
}
cart := CheckoutCart{
ItemName: c.BuildItemName(checkoutUrls.ItemNamePrefix),
CartTotal: cartTotal,
OrderID: c.OrderID,
CustFirstName: ui.NickName.String,
CustLastName: ui.CellNumber,
CustEmail: ui.Email.String}
return cartSummary + "/n/n" + ProcessPayment(cart, checkoutUrls)
}
func parseQuestionCommand(match string, db *sql.DB, convo *ConversationContext, checkoutUrls CheckoutInfo, isAutoInc bool) Command {
switch match {
case "currentorder?":
return QuestionCommand{Text: convo.CurrentOrder.GetCurrentOrderAsAString(db, convo.UserInfo.CellNumber, isAutoInc)}
case "shop?":
return QuestionCommand{Text: prclstPreamble + "\n\n" + AssembleCatalogueSelections(convo.Pricelist.PrlstPreamble, convo.Pricelist.Catalogue)}
case "userinfo?":
return QuestionCommand{Text: convo.UserInfo.GetUserInfoAsAString()}
case "checkoutnow?":
return QuestionCommand{Text: BeginCheckout(db, convo.UserInfo, convo.Pricelist.Catalogue, convo.CurrentOrder, checkoutUrls, isAutoInc)}
default:
return QuestionCommand{Text: mainMenu}
}
}
func (cc CommandCollection) ProcessCommands(convo *ConversationContext, db *sql.DB, isAutoInc bool) string {
var errors []string
for _, command := range cc {
err := command.Execute(db, convo, isAutoInc)
if err != nil {
errors = append(errors, err.Error())
}
}
return strings.Join(errors, "\n")
}
func GetResponseToMsg(convo *ConversationContext, db *sql.DB, checkoutUrls CheckoutInfo, isAutoInc bool) string {
commandRes := unhandledCommandException
commands := GetCommandsFromLastMessage(convo.MessageBody, convo, db, checkoutUrls, isAutoInc)
if len(commands) != 0 {
// Process commands
commandRes_Temp := CommandCollection(commands).ProcessCommands(convo, db, isAutoInc)
if commandRes_Temp != "" && commandRes_Temp != " " && commandRes_Temp != "\n" {
commandRes = commandRes_Temp
}
} else {
commandRes = noCommandText
}
if !convo.UserExisted {
if commandRes != noCommandText {
commandRes = smartyPantsGreeting + "\n\n" + commandRes + "\n\n" + reminderGreeting + "\n\n" + sayMenu
} else {
commandRes = coldGreeting + "\n\n" + reminderGreeting + "\n\n" + sayMenu
}
} else if commandRes == noCommandText {
commandRes += "\n\n" + sayMenu
}
convo.UserExisted = true
return commandRes
}
// Precompile regular expressions
var (
regexQuestionMark = regexp.MustCompile(`(menu\?|fr\.prlist\?|userinfo\?|currentorder\?|checkoutnow\?)`)
regexUpdateField = regexp.MustCompile(`(update email|update nickname|update social|update consent):\s*(\S*)`)
regexUpdateAnswers = regexp.MustCompile(`(update order):?\s*(.*)`)
)
func GetCommandsFromLastMessage(messageBody string, convo *ConversationContext, db *sql.DB, checkoutUrls CheckoutInfo, isAutoInc bool) []Command {
var commands []Command
messageBody = strings.ToLower(messageBody)
// Use precompiled regular expressions
if matches := regexQuestionMark.FindAllStringSubmatch(messageBody, -1); matches != nil {
for _, match := range matches {
commands = append(commands, parseQuestionCommand(match[1], db, convo, checkoutUrls, isAutoInc))
}
}
if matches := regexUpdateField.FindAllStringSubmatch(messageBody, -1); matches != nil {
for _, match := range matches {
commands = append(commands, UpdateUserInfoCommand{Name: match[1], Text: match[2]})
}
}
if matches := regexUpdateAnswers.FindAllStringSubmatch(messageBody, -1); matches != nil {
for _, match := range matches {
commands = append(commands, UpdateOrderCommand{Text: match[2]})
}
}
return commands
}
func ParseUpdateOrderCommand(commandText string) ([]MenuIndication, error) {
// Remove "update order" prefix
commandText = strings.TrimPrefix(commandText, "update order")
commandText = strings.TrimPrefix(commandText, ":")
commandText = strings.TrimSpace(commandText)
commandText = strings.Replace(commandText, " ", "", 1)
// Regular expression to match "ItemMenuNum: ItemAmount" pairs
re := regexp.MustCompile(`\b\d+:\s*(?:\d+x\d+(?:,\s*)?)+`)
// Find all matches in the commandText
matches := re.FindAllString(commandText, -1)
// Remove matched parts from the commandText
for k, match := range matches {
trimmedMatch := strings.TrimSpace(match)
trimmedMatch = strings.TrimSuffix(trimmedMatch, ",")
matches[k] = trimmedMatch
commandText = strings.Replace(commandText, match, "", 1)
}
// Trim any remaining whitespace or commas
commandText = strings.Trim(commandText, ",")
// Initialize slice to store OrderItems
var orderItems []MenuIndication
// Process each match
for _, match := range matches {
orderItem, err := parseOrderItem(match)
if err != nil {
return nil, err
}
orderItems = append(orderItems, orderItem)
}
// Process remaining commandText for simple "ItemMenuNum: ItemAmount" pairs
if commandText != "" {
remainingItems := strings.Split(commandText, ",")
for _, item := range remainingItems {
item = strings.TrimSpace(item)
if item == "" {
continue
}
orderItem, err := parseOrderItem(item)
if err != nil {
return nil, err
}
orderItems = append(orderItems, orderItem)
}
}
return orderItems, nil
}
func parseOrderItem(item string) (MenuIndication, error) {
parts := strings.SplitN(item, ":", 2)
if len(parts) != 2 {
return MenuIndication{}, fmt.Errorf("failed to parse item: %s", item)
}
// Parse ItemMenuNum
itemMenuNum, err := strconv.Atoi(strings.TrimSpace(parts[0]))
if err != nil {
return MenuIndication{}, fmt.Errorf("failed to parse ItemMenuNum: %v", err)
}
// Trim and clean up ItemAmount
itemAmount := strings.TrimSpace(parts[1])
itemAmount = strings.TrimSuffix(itemAmount, ",")
return MenuIndication{
ItemMenuNum: itemMenuNum,
ItemAmount: itemAmount,
}, nil
}