forked from jheuel/pollrBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbackquery.go
467 lines (419 loc) · 12.2 KB
/
callbackquery.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package main
import (
"fmt"
"strconv"
"strings"
"time"
tg "github.com/semog/go-bot-api/v4"
"k8s.io/klog"
)
func handleCallbackQuery(bot *tg.BotAPI, update tg.Update, st Store) error {
if update.CallbackQuery.Data == qryDummy {
return sendToastMessage(bot, update, "")
}
if update.CallbackQuery.Data[0] == qryEditPayload {
return handlePollEditQuery(bot, update, st)
}
if update.CallbackQuery.Data == qryCreatePoll {
return sendNewQuestionMessage(bot, update, st)
}
if strings.Contains(update.CallbackQuery.Data, qryPollDone) {
return handlePollDoneQuery(bot, update, st)
}
pollID, optionid, err := parseQueryPayload(update)
if err != nil {
return fmt.Errorf("could not parse query payload: %v", err)
}
if update.CallbackQuery.InlineMessageID != "" {
if err := st.AddInlineMsgToPoll(pollID, update.CallbackQuery.InlineMessageID); err != nil {
return fmt.Errorf("could not add inline message to poll: %v", err)
}
}
userID, err := getUpdateUserID(update)
if err != nil {
return err
}
p, err := st.GetPoll(pollID)
if err != nil {
sendToastMessage(bot, update, locErrUpdatingPollMessage)
return fmt.Errorf("could not get poll: %v", err)
}
if p.isInactive() {
sendToastMessage(bot, update, locPollIsInactive)
return fmt.Errorf("poll %d is inactive", pollID)
}
newAnswer := answer{
UserID: userID,
PollID: pollID,
OptionID: optionid,
}
unvoted, err := st.SaveAnswer(p, newAnswer)
if err != nil {
return fmt.Errorf("could not save answers: %v", err)
}
// polls were changed
p, err = st.GetPoll(pollID)
if err != nil {
sendToastMessage(bot, update, locErrUpdatingPollMessage)
return fmt.Errorf("could not get poll: %v", err)
}
choice, err := findChoice(p, newAnswer.OptionID)
if err != nil {
sendToastMessage(bot, update, locErrUpdatingPollMessage)
return err
}
pollsToUpdate.enqueue(p.ID)
var popupText string
if unvoted {
popupText = locSelectionRemoved
} else {
popupText = fmt.Sprintf(locYouSelected, choice.Text)
}
return sendToastMessage(bot, update, popupText)
}
func findChoice(p *poll, optionID int) (option, error) {
for _, o := range p.Options {
if o.ID == optionID {
return o, nil
}
}
return option{}, fmt.Errorf("could not find option")
}
func updatePollMessages(bot *tg.BotAPI, pollID int, st Store) error {
p, err := st.GetPoll(pollID)
if err != nil {
return fmt.Errorf("could not get poll: %v", err)
}
//msgs, err := st.GetAllPollMsg(pollID)
//if err != nil {
//return fmt.Errorf("could not get all pollmsgs: %v", err)
//}
listing := buildPollListing(p, st)
var ed tg.EditMessageTextConfig
ed.Text = listing
ed.ParseMode = tg.ModeHTML
if !p.isInactive() {
ed.ReplyMarkup = buildPollMarkup(p)
}
//for _, msg := range msgs {
//ed.ChatID = msg.ChatID
//ed.MessageID = msg.MessageID
//_, err = bot.Send(ed)
//if err != nil {
//klog.Infof("Could not edit message: %v \nThe message was: %s\n", err, ed.Text)
//klog.Infof("Could not edit message: %v\n", err)
//splits := strings.Split(ed.Text, "\n")
//ed.Text = ""
//for _, l := range splits {
//if !strings.HasPrefix(l, "\u251C") && !strings.HasPrefix(l, "\u2514") {
//ed.Text += l + "\n"
//}
//}
//klog.Infof("try again:\n %s", ed.Text)
//_, err = bot.Send(ed)
//if err != nil {
//klog.Infof("could not update message: %v\n", err)
//continue
//}
//}
//}
// reset
ed.ChatID = 0
ed.MessageID = 0
msgs, err := st.GetAllPollInlineMsg(p.ID)
if err != nil {
return fmt.Errorf("could not get all poll inline messages: %v", err)
}
for _, msg := range msgs {
ed.Text = listing
ed.InlineMessageID = msg.InlineMessageID
_, err := bot.Send(ed)
if err != nil {
if strings.Contains(err.Error(), "MESSAGE_ID_INVALID") {
klog.Infof("Remove inline message %s\n", msg.InlineMessageID)
st.RemoveInlineMsg(msg.InlineMessageID)
continue
}
// if strings.Contains(err.Error(), "chat not found") {
// klog.Infof("Remove inline message %s\n", msg.InlineMessageID)
// st.RemoveInlineMsg(msg.InlineMessageID)
// }
if strings.Contains(err.Error(), "message is not modified") {
continue
}
time.Sleep(20 * time.Millisecond)
klog.Infof("\n\n\nCould not edit inline message: %v \nThe message was: %s\n", err, ed.Text)
klog.Infof("Could not edit inline message: %v\n", err)
splits := strings.Split(ed.Text, "\n")
ed.Text = ""
for _, l := range splits {
if !strings.HasPrefix(l, "\u251C") && !strings.HasPrefix(l, "\u2514") {
ed.Text += l + "\n"
}
}
klog.Infof("try again:\n %s", ed.Text)
_, err = bot.Send(ed)
if err != nil {
klog.Infof("could not update inline message: %v\n", err)
continue
}
}
time.Sleep(20 * time.Millisecond)
}
return nil
}
func deletePollMessages(bot *tg.BotAPI, pollID int, st Store) error {
klog.Infof("TODO: delete existing shared poll messages.")
return nil
}
func handlePollDoneQuery(bot *tg.BotAPI, update tg.Update, st Store) error {
splits := strings.Split(update.CallbackQuery.Data, ":")
if len(splits) < 2 {
return fmt.Errorf("query did not contain the pollID")
}
pollID, err := strconv.Atoi(splits[1])
if err != nil {
return fmt.Errorf("could not convert string payload to int: %v", err)
}
userID, err := getUpdateUserID(update)
if err != nil {
return err
}
p, err := st.GetUserPoll(pollID, userID)
if err != nil {
return fmt.Errorf("could not get poll: %v", err)
}
_, err = sendEditMessage(bot, int64(userID), p)
if err != nil {
return fmt.Errorf("could not edit finished poll: %v", err)
}
err = st.SaveState(userID, p.ID, pollDone)
if err != nil {
return fmt.Errorf("could not change state to poll done: %v", err)
}
return nil
}
func handlePollEditQuery(bot *tg.BotAPI, update tg.Update, st Store) error {
splits := strings.Split(update.CallbackQuery.Data, ":")
if len(splits) < 3 {
klog.Infoln(splits)
sendToastMessage(bot, update, locErrUpdatingPollMessage)
return fmt.Errorf("query wrongly formatted")
}
pollID, err := strconv.Atoi(splits[1])
if err != nil {
sendToastMessage(bot, update, locErrUpdatingPollMessage)
return fmt.Errorf("could not convert string payload to int: %v", err)
}
var p *poll
noNewer := false
noOlder := false
toggleInactive := false
toggleMultipleChoice := false
toggleShowVotePct := false
userID, err := getUpdateUserID(update)
if err != nil {
return err
}
switch splits[2] {
case qryNextPoll:
p, err = st.GetPollNewer(pollID, userID)
if err != nil {
klog.Infof("could not get older poll: %v\n", err)
noNewer = true
p, err = st.GetUserPoll(pollID, userID)
if err != nil {
sendToastMessage(bot, update, locErrUpdatingPollMessage)
return fmt.Errorf("could not get poll: %v", err)
}
}
case qryPrevPoll:
p, err = st.GetPollOlder(pollID, userID)
if err != nil {
klog.Infof("could not get older poll: %v\n", err)
noOlder = true
p, err = st.GetUserPoll(pollID, userID)
if err != nil {
sendToastMessage(bot, update, locErrUpdatingPollMessage)
return fmt.Errorf("could not get poll: %v", err)
}
}
case qryToggleActive:
p, err = st.GetUserPoll(pollID, userID)
if err != nil {
sendToastMessage(bot, update, locErrUpdatingPollMessage)
return fmt.Errorf("could not get poll: %v", err)
}
toggleInactive = true
case qryToggleMultipleChoice:
p, err = st.GetUserPoll(pollID, userID)
if err != nil {
sendToastMessage(bot, update, locErrUpdatingPollMessage)
return fmt.Errorf("could not get poll: %v", err)
}
toggleMultipleChoice = true
case qryToggleShowVotePct:
p, err = st.GetUserPoll(pollID, userID)
if err != nil {
sendToastMessage(bot, update, locErrUpdatingPollMessage)
return fmt.Errorf("could not get poll: %v", err)
}
toggleShowVotePct = true
case qryEditOptions:
p, err = st.GetUserPoll(pollID, userID)
if err != nil {
sendToastMessage(bot, update, locErrUpdatingPollMessage)
return fmt.Errorf("could not get poll: %v", err)
}
state := waitingForOption
err = st.SaveState(userID, pollID, state)
if err != nil {
return err
}
_, err = sendInterMessage(bot, update, p)
if err != nil {
return fmt.Errorf("could not send inter message: %v", err)
}
return nil
case qryEditQuestion:
state := editQuestion
err = st.SaveState(userID, pollID, state)
if err != nil {
sendToastMessage(bot, update, locErrUpdatingPollMessage)
return err
}
msg := tg.NewMessage(update.CallbackQuery.Message.Chat.ID, locEditQuestion)
_, err = bot.Send(&msg)
if err != nil {
return fmt.Errorf("could not send message: %v", err)
}
return nil
case qryResetPoll:
err = st.ResetPoll(userID, pollID)
if err != nil {
sendToastMessage(bot, update, locErrUpdatingPollMessage)
return fmt.Errorf("could not reset poll: %v", err)
}
pollsToUpdate.enqueue(pollID)
sendToastMessage(bot, update, locResetPollMessage)
return nil
case qryDeletePoll:
p, err = st.GetUserPoll(pollID, userID)
if err != nil {
sendToastMessage(bot, update, locErrUpdatingPollMessage)
return fmt.Errorf("could not get poll: %v", err)
}
// The poll must be closed first
if inactive != p.Inactive {
sendToastMessage(bot, update, locClosePollBeforeDeleteMessage)
return nil
}
err = st.DeletePoll(userID, pollID)
if err != nil {
sendToastMessage(bot, update, locErrDeletingPollMessage)
return fmt.Errorf("could not delete poll: %v", err)
}
sendToastMessage(bot, update, fmt.Sprintf(locPollDeletedMessage, p.Question))
pollsToDelete.enqueue(pollID)
// Move to the next poll.
p, err = st.GetPollOlder(pollID, userID)
if err != nil {
klog.Infof("could not get older poll: %v\n", err)
noOlder = true
// Move to the previous poll.
p, err = st.GetPollNewer(pollID, userID)
if err != nil {
klog.Infof("could not get older poll: %v\n", err)
err = st.SaveState(userID, -1, ohHi)
if err != nil {
return err
}
_, err = sendMainMenuMessage(bot, update)
if err != nil {
return fmt.Errorf("could not send main menu message: %v", err)
}
return nil
}
}
default:
return fmt.Errorf("query wrongly formatted")
}
if err != nil {
return err
}
if toggleInactive {
if p.Inactive == open {
p.Inactive = inactive
} else {
p.Inactive = open
}
_, err = st.SavePoll(p)
if err != nil {
klog.Infoln("could not save toggled inactive state.")
sendToastMessage(bot, update, locErrUpdatingPollMessage)
}
}
if toggleMultipleChoice {
if p.Type == standard {
p.Type = multipleChoice
} else if p.Type == multipleChoice {
p.Type = rankedVoting
} else {
p.Type = standard
}
_, err = st.SavePoll(p)
if err != nil {
klog.Infoln("could not save toggled multiple choice state.")
sendToastMessage(bot, update, locErrUpdatingPollMessage)
}
}
if toggleShowVotePct {
if p.DisplayPercent == displayVotePercent {
p.DisplayPercent = hideDisplayVotePercent
} else {
p.DisplayPercent = displayVotePercent
}
_, err = st.SavePoll(p)
if err != nil {
klog.Infoln("could not save toggled show vote percent state.")
sendToastMessage(bot, update, locErrUpdatingPollMessage)
}
}
messageTxt := getSelectedPollHeader(p)
messageTxt += getFormattedPreviewPoll(p)
var ed tg.EditMessageTextConfig
ed.Text = messageTxt
ed.ParseMode = tg.ModeHTML
ed.ReplyMarkup = buildEditMarkup(p, noOlder, noNewer)
ed.ChatID = update.CallbackQuery.Message.Chat.ID
ed.MessageID = update.CallbackQuery.Message.MessageID
_, err = bot.Send(ed)
if err != nil {
klog.Infof("could not update message: %v\n", err)
}
pollsToUpdate.enqueue(p.ID)
return nil
}
func sendToastMessage(bot *tg.BotAPI, update tg.Update, msg string) error {
callbackConfig := tg.NewCallback(update.CallbackQuery.ID, msg)
_, err := bot.AnswerCallbackQuery(callbackConfig)
if err != nil {
klog.Infof("could not send toast message: %v\n", err)
}
return nil
}
func parseQueryPayload(update tg.Update) (pollID int, optionid int, err error) {
dataSplit := strings.Split(update.CallbackQuery.Data, ":")
if len(dataSplit) != 2 {
return pollID, optionid, fmt.Errorf("could not parse response")
}
pollID, err = strconv.Atoi(dataSplit[0])
if err != nil {
return pollID, optionid, fmt.Errorf("could not convert CallbackQuery data pollID to int: %v", err)
}
optionid, err = strconv.Atoi(dataSplit[1])
if err != nil {
return pollID, optionid, fmt.Errorf("could not convert CallbackQuery data OptionID to int: %v", err)
}
return pollID, optionid, nil
}