-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
379 lines (293 loc) · 9.24 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
package main
import (
"fmt"
"log"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/aikchun/gotelegrambot"
"github.com/aikchun/gototo"
"github.com/aikchun/totobro-bot/internal/services/nextdrawservice"
"github.com/aikchun/totobro-bot/internal/services/subscriptionservice"
"github.com/uptrace/bun"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
type Bot struct {
username string
token string
db *bun.DB
handlers map[string]func(*gotelegrambot.Update, []string)
subscriptionService *subscriptionservice.SubscriptionService
nextDrawService *nextdrawservice.NextDrawService
}
func newBot(n string, t string, db *bun.DB) Bot {
ss := subscriptionservice.NewSubscriptionService(db)
nds := nextdrawservice.NewNextDrawService(db)
b := Bot{
username: n,
token: t,
db: db,
subscriptionService: &ss,
nextDrawService: &nds,
handlers: make(map[string]func(*gotelegrambot.Update, []string)),
}
b.handlers["/nextdraw"] = b.nextDraw
b.handlers["/results"] = b.results
b.handlers["/subscribe"] = b.subscribe
b.handlers["/start"] = b.start
b.handlers["/help"] = b.help
b.handlers["/fetchNextDraw"] = b.fetchNextDraw
b.handlers["/unsubscribe"] = b.unsubscribe
b.handlers["/setalert"] = b.setAlert
b.handlers["setprize"] = b.setPrize
return b
}
func (b Bot) handle(u *gotelegrambot.Update) {
s := u.Message.Text
trimmed := strings.Trim(s, " ")
tokens := strings.Split(trimmed, " ")
funcName := tokens[0]
args := tokens[1:]
callbackQuery := u.CallbackQuery
if callbackQuery != nil && callbackQuery.Message != nil {
queryParams, err := url.Parse(callbackQuery.Data)
if err != nil {
log.Printf("could not parse callback data")
}
command := queryParams.Query().Get("a")
if handler, ok := b.handlers[command]; ok {
handler(u, args)
}
a := gotelegrambot.AnswerCallbackQueryPayload{
CallbackQueryID: callbackQuery.ID,
Text: "Updated!",
}
gotelegrambot.AnswerCallbackQuery(b.token, a)
return
}
for key, handler := range b.handlers {
if key == funcName || funcName == fmt.Sprintf("%s@%s", key, b.username) {
handler(u, args)
}
}
}
func (b Bot) nextDraw(u *gotelegrambot.Update, args []string) {
n := gototo.GetNextDraw()
p := gotelegrambot.SendMessagePayload{
ChatID: u.Message.Chat.ID,
Text: nextDrawMessage(n),
}
if _, err := gotelegrambot.SendMessage(b.token, p); err != nil {
log.Fatal(err)
}
}
func nextDrawMessage(n gototo.NextDraw) string {
isToday := isNextDrawToday(n)
if isToday {
return fmt.Sprintf("The draw is today!\n\nDate: %s\nPrize: %s", n.GetDate(), n.GetPrize())
}
return fmt.Sprintf("Next Toto Draw\n\nDate: %s\nPrize: %s", n.GetDate(), n.GetPrize())
}
func (b Bot) results(u *gotelegrambot.Update, args []string) {
d := gototo.GetLatestDraw()
var ws []string
for _, s := range d.GetWinningNumbers() {
ws = append(ws, strconv.Itoa(s))
}
text := `The latest Toto results
Date: %s
Winning Numbers: %s
Additional Number: %d`
t := fmt.Sprintf(text, d.GetDate(), strings.Join(ws, " "), d.GetAdditionalNumber())
p := gotelegrambot.SendMessagePayload{
ChatID: u.Message.Chat.ID,
Text: t,
}
if _, err := gotelegrambot.SendMessage(b.token, p); err != nil {
log.Fatal(err)
}
}
func (b Bot) start(u *gotelegrambot.Update, args []string) {
p := gotelegrambot.SendMessagePayload{
ChatID: u.Message.Chat.ID,
Text: fmt.Sprintf("%s\n%s", b.startMessage(), b.helpMessage()),
}
if _, err := gotelegrambot.SendMessage(b.token, p); err != nil {
log.Fatal(err)
}
}
func (b Bot) startMessage() string {
return "Hello I am your bro for Toto."
}
func (b Bot) helpMessage() string {
return `You can start with:
/nextdraw to view the upcoming draw.
/results for the latest draw results.
/setalert alert only when next draw prize is above the amount set
/subscribe to get toto draw alerts.
/unsubscribe to stop getting toto draw alerts.`
}
func (b Bot) help(u *gotelegrambot.Update, args []string) {
p := gotelegrambot.SendMessagePayload{
ChatID: u.Message.Chat.ID,
Text: b.helpMessage(),
}
if _, err := gotelegrambot.SendMessage(b.token, p); err != nil {
log.Fatal(err)
}
}
func (b Bot) subscribe(u *gotelegrambot.Update, args []string) {
chatID := u.Message.Chat.ID
p := gotelegrambot.SendMessagePayload{
ChatID: chatID,
Text: "You are subscribed to next draw alerts for Toto!",
}
if _, err := b.subscriptionService.Subscribe(chatID, 0); err != nil {
log.Println(err)
p.Text = "subscribe unsuccessful"
}
if _, err := gotelegrambot.SendMessage(b.token, p); err != nil {
log.Fatal(err)
}
}
func (b Bot) unsubscribe(u *gotelegrambot.Update, args []string) {
chatID := u.Message.Chat.ID
p := gotelegrambot.SendMessagePayload{
ChatID: chatID,
Text: "You have unsubscribed from new Toto draw alerts!",
}
if err := b.subscriptionService.SoftDelete(chatID); err != nil {
log.Println(err)
p.Text = "unsubscribe unsuccessful"
}
if _, err := gotelegrambot.SendMessage(b.token, p); err != nil {
log.Fatal(err)
}
}
func (b Bot) fetchNextDraw(u *gotelegrambot.Update, args []string) {
for _, p := range b.createNextDrawBroadcastPayloads(u, gototo.GetNextDraw()) {
if _, err := gotelegrambot.SendMessage(b.token, p); err != nil {
log.Fatalln(err)
}
}
}
func (b Bot) setAlert(u *gotelegrambot.Update, args []string) {
p := gotelegrambot.SendMessagePayload{
ChatID: u.Message.Chat.ID,
Text: "Alert you only when the next draw prize money is more or equal than...:",
ReplyMarkup: CreatePrizeMoneyAlertLimitInlineKeyboardMarkup(),
}
if _, err := gotelegrambot.SendMessage(b.token, p); err != nil {
log.Fatalln(err)
}
}
func (b Bot) setPrize(u *gotelegrambot.Update, args []string) {
queryParams, _ := url.Parse(u.CallbackQuery.Data)
amt := queryParams.Query().Get("amt")
v, err := strconv.Atoi(amt)
if err != nil {
a := gotelegrambot.AnswerCallbackQueryPayload{
CallbackQueryID: u.CallbackQuery.ID,
ShowAlert: true,
Text: "Unable to parse value",
}
gotelegrambot.AnswerCallbackQuery(b.token, a)
return
}
err = b.subscriptionService.SetPrize(u.CallbackQuery.Message.Chat.ID, uint32(v))
if err != nil {
a := gotelegrambot.AnswerCallbackQueryPayload{
CallbackQueryID: u.CallbackQuery.ID,
ShowAlert: true,
Text: "Unable to save value",
}
gotelegrambot.AnswerCallbackQuery(b.token, a)
return
}
m := message.NewPrinter(language.English)
payload := gotelegrambot.EditMessageTextPayload{
ChatID: strconv.FormatInt(u.CallbackQuery.Message.Chat.ID, 10),
MessageID: u.CallbackQuery.Message.MessageID,
Text: m.Sprintf("Minimum prize money for alerts is now: $%d", v),
}
gotelegrambot.EditMessageText(b.token, payload)
}
func CreatePrizeMoneyAlertLimitInlineKeyboardMarkup() *gotelegrambot.InlineKeyboardMarkup {
amounts := []uint{1000000, 2000000, 4000000, 8000000}
buttons := make([][]gotelegrambot.InlineKeyboardButton, len(amounts))
m := message.NewPrinter(language.English)
for i := 0; i < len(amounts); i++ {
amt := amounts[i]
button := gotelegrambot.InlineKeyboardButton{
Text: fmt.Sprintf("$%s", m.Sprintf("%d", amt)),
CallbackData: fmt.Sprintf("?a=setprize&amt=%d", amt),
}
buttons[i] = []gotelegrambot.InlineKeyboardButton{button}
}
return &gotelegrambot.InlineKeyboardMarkup{InlineKeyboard: buttons}
}
func (b Bot) createNextDrawBroadcastPayloads(u *gotelegrambot.Update, n gototo.NextDraw) []gotelegrambot.SendMessagePayload {
if !isValidFetchNextDrawUpdate(u) {
log.Fatalln("Not valid payload")
}
subs, err := b.subscriptionService.GetAll()
if err != nil {
log.Fatalln(err)
}
_, err = b.nextDrawService.Save(n)
isToday := isNextDrawToday(n)
if err != nil && !isToday {
log.Fatalln("failed to save next draw and next draw is not today")
}
prizeMoney, err := parsePrize(n.GetPrize())
if err != nil {
log.Fatalln("unable to parse next draw's prize")
}
var ps []gotelegrambot.SendMessagePayload
for _, s := range subs {
if s.Threshold <= prizeMoney {
p := gotelegrambot.SendMessagePayload{
ChatID: s.ChatID,
Text: nextDrawMessage(n),
}
ps = append(ps, p)
}
}
return ps
}
func isValidFetchNextDrawUpdate(u *gotelegrambot.Update) bool {
id, err := strconv.ParseInt(os.Getenv("FETCH_NEXT_DRAW_TASK_MOCK_ID"), 10, 64)
if err != nil {
log.Fatal(err)
}
// we use one super large value for id for all the objects
// to make it incredibly unlikely for anyone but us to make this call
return u.UpdateID == id &&
u.Message.MessageID == id &&
u.Message.From.ID == id &&
u.Message.Chat.ID == id &&
u.Message.From.FirstName == os.Getenv("FETCH_NEXT_DRAW_TASK_USER_FIRST_NAME")
}
func isNextDrawToday(n gototo.NextDraw) bool {
now := time.Now().Add(time.Hour * 8)
nowString := now.Format("Mon, 02 Jan 2006")
return nowString == parseNextDrawDateString(n.GetDate())
}
func parseNextDrawDateString(dateString string) string {
s := strings.Split(dateString, " ")
var ds []string
for i := 0; i < 4; i++ {
ds = append(ds, s[i])
}
return strings.Join(ds, " ")
}
func parsePrize(prize string) (uint32, error) {
p := strings.Split(prize, " ")
first := p[0]
str := strings.Replace(strings.Trim(first, "$"), ",", "", -1)
val, err := strconv.Atoi(str)
return uint32(val), err
}