-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.go
394 lines (326 loc) · 13.8 KB
/
bot.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package controller
import (
"strconv"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/st-matskevich/audio-guide-bot/api/provider/bot"
"github.com/st-matskevich/audio-guide-bot/api/provider/translation"
"github.com/st-matskevich/audio-guide-bot/api/repository"
)
const BUY_TICKET_QUERY = "buy_ticket"
const TICKET_CURRENCY_KEY = "TICKET_CURRENCY"
const TICKET_PRICE_KEY = "TICKET_PRICE"
type BotController struct {
WebAppURL string
BotProvider bot.BotProvider
TranslationProvider translation.TranslationProvider
TicketRepository repository.TicketRepository
ConfigRepository repository.ConfigRepository
}
func (controller *BotController) GetRoutes() []Route {
return []Route{
{
Method: "POST",
Path: "/bot",
Handler: controller.HandleBotUpdate,
},
}
}
func (controller *BotController) HandleBotUpdate(c *fiber.Ctx) error {
update := bot.Update{}
if err := c.BodyParser(&update); err != nil {
HandlerPrintf(c, LOG_ERROR, "Failed to parse input", "error", err)
return HandlerSendFailure(c, fiber.StatusBadRequest, "Failed to parse input")
}
if update.CallbackQuery != nil {
HandlerPrintf(c, LOG_INFO, "Received callback query")
return controller.HandleBotCallback(c, &update)
}
if update.PreCheckoutQuery != nil {
HandlerPrintf(c, LOG_INFO, "Received pre-checkout query")
return controller.HandleBotPreCheckout(c, &update)
}
if update.Message != nil {
HandlerPrintf(c, LOG_INFO, "Received message")
return controller.HandleBotMessage(c, &update)
}
return HandlerSendSuccess(c, fiber.StatusOK, nil)
}
func (controller *BotController) HandleBotMessage(c *fiber.Ctx, update *bot.Update) error {
locale := update.Message.From.LanguageCode
if update.Message.SuccessfulPayment != nil {
HandlerPrintf(c, LOG_INFO, "Message type is successful payment")
ticketCode, err := uuid.Parse(update.Message.SuccessfulPayment.InvoicePayload)
if err != nil {
HandlerPrintf(c, LOG_ERROR, "Failed to parse bot payment payload", "error", err)
return HandlerSendError(c, fiber.StatusInternalServerError, "Failed to parse bot payment payload")
}
if err = controller.TicketRepository.CreateTicket(ticketCode.String()); err != nil {
HandlerPrintf(c, LOG_ERROR, "Failed to register ticket in DB", "error", err)
return HandlerSendError(c, fiber.StatusInternalServerError, "Failed to register ticket in DB")
}
message, options, err := controller.buildPurchaseMessage(locale, ticketCode.String())
if err != nil {
HandlerPrintf(c, LOG_ERROR, "Failed to prepare message", "error", err)
return HandlerSendError(c, fiber.StatusInternalServerError, "Failed to prepare message")
}
HandlerPrintf(c, LOG_INFO, "Created ticket, responding with payment confirmation", "ticket", ticketCode.String())
if err := controller.BotProvider.SendMessage(update.Message.Chat.Id, message, options); err != nil {
HandlerPrintf(c, LOG_ERROR, "Failed to send bot message", "error", err)
return HandlerSendError(c, fiber.StatusInternalServerError, "Failed to send bot message")
}
return HandlerSendSuccess(c, fiber.StatusOK, nil)
}
HandlerPrintf(c, LOG_INFO, "Responding with welcome message")
message, options, err := controller.buildWelcomeMessage(locale)
if err != nil {
HandlerPrintf(c, LOG_ERROR, "Failed to prepare message", "error", err)
return HandlerSendError(c, fiber.StatusInternalServerError, "Failed to prepare message")
}
if err := controller.BotProvider.SendMessage(update.Message.Chat.Id, message, options); err != nil {
HandlerPrintf(c, LOG_ERROR, "Failed to send bot message", "error", err)
return HandlerSendError(c, fiber.StatusInternalServerError, "Failed to send bot message")
}
return HandlerSendSuccess(c, fiber.StatusOK, nil)
}
func (controller *BotController) HandleBotCallback(c *fiber.Ctx, update *bot.Update) error {
locale := update.CallbackQuery.From.LanguageCode
if update.CallbackQuery.Data == "" {
HandlerPrintf(c, LOG_ERROR, "Bot update didn't include a callback data")
return HandlerSendFailure(c, fiber.StatusBadRequest, "Bot update didn't include a callback data")
}
if err := controller.BotProvider.AnswerCallbackQuery(update.CallbackQuery.Id); err != nil {
HandlerPrintf(c, LOG_ERROR, "Failed to answer callback query", "error", err)
return HandlerSendError(c, fiber.StatusInternalServerError, "Failed to answer callback query")
}
if update.CallbackQuery.Data == BUY_TICKET_QUERY {
HandlerPrintf(c, LOG_INFO, "Callback query is BUY_TICKET_QUERY")
if update.CallbackQuery.Message == nil {
HandlerPrintf(c, LOG_ERROR, "Bot update didn't include a callback message")
return HandlerSendFailure(c, fiber.StatusBadRequest, "Bot update didn't include a callback message")
}
price, err := controller.getTicketPrice(c)
if err != nil {
HandlerPrintf(c, LOG_ERROR, "Failed to get ticket price", "error", err)
return HandlerSendError(c, fiber.StatusInternalServerError, "Failed to get ticket price")
} else if price == nil {
HandlerPrintf(c, LOG_INFO, "Ticket price is not set, responding with disabled payments message")
message, options, err := controller.buildPaymentsDisabledMessage(locale)
if err != nil {
HandlerPrintf(c, LOG_ERROR, "Failed to prepare message", "error", err)
return HandlerSendError(c, fiber.StatusInternalServerError, "Failed to prepare message")
}
if err := controller.BotProvider.SendMessage(update.CallbackQuery.Message.Chat.Id, message, options); err != nil {
HandlerPrintf(c, LOG_ERROR, "Failed to send bot message", "error", err)
return HandlerSendError(c, fiber.StatusInternalServerError, "Failed to send bot message")
}
return HandlerSendSuccess(c, fiber.StatusOK, nil)
}
invoice, err := controller.buildInvoiceData(locale, *price)
if err != nil {
HandlerPrintf(c, LOG_ERROR, "Failed to prepare invoice", "error", err)
return HandlerSendError(c, fiber.StatusInternalServerError, "Failed to prepare invoice")
}
ticketCode := uuid.New()
HandlerPrintf(c, LOG_INFO, "Responding with invoice for ticket", "ticket", ticketCode.String())
if err := controller.BotProvider.SendInvoice(update.CallbackQuery.Message.Chat.Id, invoice.Title, invoice.Description, ticketCode.String(), invoice.Price, invoice.Options); err != nil {
HandlerPrintf(c, LOG_ERROR, "Failed to send bot invoice", "error", err)
return HandlerSendError(c, fiber.StatusInternalServerError, "Failed to send bot invoice")
}
return HandlerSendSuccess(c, fiber.StatusOK, nil)
}
return HandlerSendSuccess(c, fiber.StatusOK, nil)
}
func (controller *BotController) HandleBotPreCheckout(c *fiber.Ctx, update *bot.Update) error {
locale := update.PreCheckoutQuery.From.LanguageCode
acceptCheckout, errorMessage, err := controller.validatePreCheckoutQuery(c, update, locale)
if err != nil {
HandlerPrintf(c, LOG_ERROR, "Failed to validate pre-checkout query", "error", err)
return HandlerSendError(c, fiber.StatusInternalServerError, "Failed to validate pre-checkout query")
}
HandlerPrintf(c, LOG_INFO, "Accepting pre-checkout", "accept", acceptCheckout)
if err := controller.BotProvider.AnswerPreCheckoutQuery(update.PreCheckoutQuery.Id, acceptCheckout, bot.AnswerPreCheckoutQueryOptions{ErrorMessage: errorMessage}); err != nil {
HandlerPrintf(c, LOG_ERROR, "Failed to answer pre-checkout query", "error", err)
return HandlerSendError(c, fiber.StatusInternalServerError, "Failed to answer pre-checkout query")
}
return HandlerSendSuccess(c, fiber.StatusOK, nil)
}
func (controller *BotController) validatePreCheckoutQuery(c *fiber.Ctx, update *bot.Update, locale string) (bool, *string, error) {
price, err := controller.getTicketPrice(c)
if err != nil {
HandlerPrintf(c, LOG_ERROR, "Failed to get ticket price", "error", err)
return false, nil, err
}
if price == nil {
HandlerPrintf(c, LOG_ERROR, "Ticket price is not set")
message, err := controller.TranslationProvider.TranslateMessage("PAYMENT_FAIL_PRICE_NOT_SET", locale, translation.TemplateData{})
if err != nil {
return false, nil, err
}
return false, &message, nil
}
if update.PreCheckoutQuery.Currency != price.Currency {
HandlerPrintf(c, LOG_WARNING, "Pre-checkout currency is not correct")
message, err := controller.TranslationProvider.TranslateMessage("PAYMENT_FAIL_INVALID_CURRENCY", locale, translation.TemplateData{})
if err != nil {
return false, nil, err
}
return false, &message, nil
}
if update.PreCheckoutQuery.TotalAmount != price.Price {
HandlerPrintf(c, LOG_WARNING, "Pre-checkout price is not correct")
message, err := controller.TranslationProvider.TranslateMessage("PAYMENT_FAIL_INVALID_PRICE", locale, translation.TemplateData{})
if err != nil {
return false, nil, err
}
return false, &message, nil
}
ticketCode, err := uuid.Parse(update.PreCheckoutQuery.InvoicePayload)
if err != nil {
HandlerPrintf(c, LOG_WARNING, "Pre-checkout payload is not correct")
message, err := controller.TranslationProvider.TranslateMessage("PAYMENT_FAIL_INVALID_TICKET", locale, translation.TemplateData{})
if err != nil {
return false, nil, err
}
return false, &message, nil
}
ticket, err := controller.TicketRepository.GetTicket(ticketCode.String())
if err != nil {
HandlerPrintf(c, LOG_ERROR, "Failed to get invoice ticket", "error", err)
return false, nil, err
}
if ticket != nil {
HandlerPrintf(c, LOG_WARNING, "Invoice ticket is already sold", "ticket", ticketCode.String())
message, err := controller.TranslationProvider.TranslateMessage("PAYMENT_FAIL_TICKET_SOLD", locale, translation.TemplateData{})
if err != nil {
return false, nil, err
}
return false, &message, nil
}
return true, nil, nil
}
type TicketPrice struct {
Currency string
Price int64
}
func (controller *BotController) getTicketPrice(c *fiber.Ctx) (*TicketPrice, error) {
currency, err := controller.ConfigRepository.GetValue(TICKET_CURRENCY_KEY)
if err != nil {
return nil, err
}
if currency == nil {
HandlerPrintf(c, LOG_ERROR, "Ticket currency key not found")
return nil, nil
}
priceString, err := controller.ConfigRepository.GetValue(TICKET_PRICE_KEY)
if err != nil {
return nil, err
}
if priceString == nil {
HandlerPrintf(c, LOG_ERROR, "Ticket price key not found")
return nil, nil
}
price, err := strconv.ParseInt(*priceString, 10, 64)
if err != nil {
return nil, err
}
result := TicketPrice{
Currency: *currency,
Price: price,
}
return &result, nil
}
func (controller *BotController) buildWelcomeMessage(locale string) (string, bot.SendMessageOptions, error) {
message, err := controller.TranslationProvider.TranslateMessage("MESSAGE_WELCOME", locale, translation.TemplateData{})
if err != nil {
return "", bot.SendMessageOptions{}, err
}
startText, err := controller.TranslationProvider.TranslateMessage("BUTTON_START_TOUR", locale, translation.TemplateData{})
if err != nil {
return "", bot.SendMessageOptions{}, err
}
buyTicketText, err := controller.TranslationProvider.TranslateMessage("BUTTON_BUY_TICKET", locale, translation.TemplateData{})
if err != nil {
return "", bot.SendMessageOptions{}, err
}
appURL := controller.WebAppURL
callbackQuery := BUY_TICKET_QUERY
opts := bot.SendMessageOptions{
InlineKeyboard: &bot.InlineKeyboardMarkup{
Markup: [][]bot.InlineKeyboardButton{{
{Text: startText, WebAppURL: &appURL},
}, {
{Text: buyTicketText, CallbackData: &callbackQuery},
}},
},
}
return message, opts, nil
}
func (controller *BotController) buildPurchaseMessage(locale string, ticketCode string) (string, bot.SendMessageOptions, error) {
message, err := controller.TranslationProvider.TranslateMessage("MESSAGE_PURCHASED_TICKET", locale, translation.TemplateData{"TICKET_CODE": ticketCode})
if err != nil {
return "", bot.SendMessageOptions{}, err
}
startText, err := controller.TranslationProvider.TranslateMessage("BUTTON_START_TOUR", locale, translation.TemplateData{})
if err != nil {
return "", bot.SendMessageOptions{}, err
}
appURL := controller.WebAppURL + "?ticket=" + ticketCode
opts := bot.SendMessageOptions{
InlineKeyboard: &bot.InlineKeyboardMarkup{
Markup: [][]bot.InlineKeyboardButton{{
{Text: startText, WebAppURL: &appURL},
}},
},
}
return message, opts, nil
}
func (controller *BotController) buildPaymentsDisabledMessage(locale string) (string, bot.SendMessageOptions, error) {
message, err := controller.TranslationProvider.TranslateMessage("MESSAGE_PAYMENTS_NOT_AVAILABLE", locale, translation.TemplateData{})
if err != nil {
return "", bot.SendMessageOptions{}, err
}
return message, bot.SendMessageOptions{}, nil
}
type InvoiceData struct {
Title string
Description string
Price bot.InvoicePrice
Options bot.SendInvoiceOptions
}
func (controller *BotController) buildInvoiceData(locale string, price TicketPrice) (InvoiceData, error) {
title, err := controller.TranslationProvider.TranslateMessage("PAYMENT_TICKET_TITLE", locale, translation.TemplateData{})
if err != nil {
return InvoiceData{}, err
}
description, err := controller.TranslationProvider.TranslateMessage("PAYMENT_TICKET_DESCRIPTION", locale, translation.TemplateData{})
if err != nil {
return InvoiceData{}, err
}
priceLabel, err := controller.TranslationProvider.TranslateMessage("PAYMENT_TICKET_PRICE_PART_PRICE", locale, translation.TemplateData{})
if err != nil {
return InvoiceData{}, err
}
payText, err := controller.TranslationProvider.TranslateMessage("BUTTON_PAY", locale, translation.TemplateData{})
if err != nil {
return InvoiceData{}, err
}
pay := true
opts := bot.SendInvoiceOptions{
InlineKeyboard: &bot.InlineKeyboardMarkup{
Markup: [][]bot.InlineKeyboardButton{{
{Text: payText, Pay: &pay},
}},
},
}
data := InvoiceData{
Title: title,
Description: description,
Price: bot.InvoicePrice{
Currency: price.Currency,
Parts: []bot.PricePart{{Label: priceLabel, Amount: price.Price}},
},
Options: opts,
}
return data, nil
}