-
Notifications
You must be signed in to change notification settings - Fork 6
/
callbackquery.go
362 lines (314 loc) · 9.25 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
package main
import (
"fmt"
"log"
"strconv"
"strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
func handleCallbackQuery(bot *tgbotapi.BotAPI, update tgbotapi.Update, st Store) error {
if update.CallbackQuery.Data == "dummy" {
callbackConfig := tgbotapi.NewCallback(
update.CallbackQuery.ID,
"")
_, err := bot.AnswerCallbackQuery(callbackConfig)
if err != nil {
return fmt.Errorf("could not send answer to callback query: %v", err)
}
return nil
}
if update.CallbackQuery.Data[0] == 'e' {
return handlePollEditQuery(bot, update, st)
}
if update.CallbackQuery.Data == createPollQuery {
return sendNewQuestionMessage(bot, update, st)
}
if strings.Contains(update.CallbackQuery.Data, pollDoneQuery) {
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)
}
}
p, err := st.GetPoll(pollid)
if err != nil {
return fmt.Errorf("could not get poll: %v", err)
}
if isInactive(p) {
callbackConfig := tgbotapi.NewCallback(
update.CallbackQuery.ID,
"This poll is inactive.")
_, err = bot.AnswerCallbackQuery(callbackConfig)
if err != nil {
return fmt.Errorf("could not send answer to callback query: %v", err)
}
return fmt.Errorf("poll %d is inactive", pollid)
}
newAnswer := answer{
UserID: update.CallbackQuery.From.ID,
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 {
return fmt.Errorf("could not get poll: %v", err)
}
var choice option
for _, o := range p.Options {
if o.ID == newAnswer.OptionID {
choice = o
}
}
pollsToUpdate.enqueue(p.ID)
popupText := fmt.Sprintf(`You voted for "%s"`, choice.Text)
if unvoted {
popupText = fmt.Sprintf("Seems like you deleted your vote.")
}
callbackConfig := tgbotapi.NewCallback(
update.CallbackQuery.ID,
popupText)
_, err = bot.AnswerCallbackQuery(callbackConfig)
if err != nil {
return fmt.Errorf("could not send answer to callback query: %v", err)
}
return nil
}
func updatePollMessages(bot *tgbotapi.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 tgbotapi.EditMessageTextConfig
ed.Text = listing
ed.ParseMode = tgbotapi.ModeHTML
if !isInactive(p) {
ed.ReplyMarkup = buildPollMarkup(p)
}
//for _, msg := range msgs {
//ed.ChatID = msg.ChatID
//ed.MessageID = msg.MessageID
//_, err = bot.Send(ed)
//if err != nil {
//log.Printf("Could not edit message: %v \nThe message was: %s\n", err, ed.Text)
//log.Printf("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"
//}
//}
//log.Printf("try again:\n %s", ed.Text)
//_, err = bot.Send(ed)
//if err != nil {
//log.Printf("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 {
log.Printf("\n\n\nCould not edit inline message: %v \nThe message was: %s\n", err, ed.Text)
log.Printf("Could not edit inline message: %v\n", err)
if strings.Contains(err.Error(), "MESSAGE_ID_INVALID") {
log.Printf("Remove inline message %s\n", msg.InlineMessageID)
st.RemoveInlineMsg(msg.InlineMessageID)
continue
}
// if strings.Contains(err.Error(), "chat not found") {
// log.Printf("Remove inline message %s\n", msg.InlineMessageID)
// st.RemoveInlineMsg(msg.InlineMessageID)
// }
if strings.Contains(err.Error(), "message is not modified") {
continue
}
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"
}
}
log.Printf("try again:\n %s", ed.Text)
_, err = bot.Send(ed)
if err != nil {
log.Printf("could not update inline message: %v\n", err)
continue
}
}
}
return nil
}
func handlePollDoneQuery(bot *tgbotapi.BotAPI, update tgbotapi.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)
}
p, err := st.GetPoll(pollid)
if err != nil {
return fmt.Errorf("could not get poll: %v", err)
}
_, err = postPoll(bot, p, int64(update.CallbackQuery.From.ID))
if err != nil {
return fmt.Errorf("could not post finished poll: %v", err)
}
err = st.SaveState(update.CallbackQuery.From.ID, p.ID, pollDone)
if err != nil {
return fmt.Errorf("could not change state to poll done: %v", err)
}
return nil
}
func handlePollEditQuery(bot *tgbotapi.BotAPI, update tgbotapi.Update, st Store) error {
splits := strings.Split(update.CallbackQuery.Data, ":")
if len(splits) < 3 {
log.Println(splits)
return fmt.Errorf("query wrongly formatted")
}
pollid, err := strconv.Atoi(splits[1])
if err != nil {
return fmt.Errorf("could not convert string payload to int: %v", err)
}
var p *poll
noNewer := false
noOlder := false
toggleInactive := false
toggleMultipleChoice := false
switch splits[2] {
case "+":
p, err = st.GetPollNewer(pollid, update.CallbackQuery.From.ID)
if err != nil {
log.Printf("could not get older poll: %v\n", err)
noNewer = true
}
case "-":
p, err = st.GetPollOlder(pollid, update.CallbackQuery.From.ID)
if err != nil {
log.Printf("could not get older poll: %v\n", err)
noOlder = true
}
case "c":
p, err = st.GetPoll(pollid)
if err != nil {
log.Printf("could not get poll: %v\n", err)
}
toggleInactive = true
case "m":
p, err = st.GetPoll(pollid)
if err != nil {
log.Printf("could not get poll: %v\n", err)
}
toggleMultipleChoice = true
case "o":
state := waitingForOption
err = st.SaveState(update.CallbackQuery.From.ID, pollid, state)
if err != nil {
return fmt.Errorf("could not save state: %v", err)
}
msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, locAddOption)
_, err = bot.Send(&msg)
if err != nil {
return fmt.Errorf("could not send message: %v", err)
}
return nil
case "q":
state := editQuestion
err = st.SaveState(update.CallbackQuery.From.ID, pollid, state)
if err != nil {
return fmt.Errorf("could not save state: %v", err)
}
msg := tgbotapi.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
default:
return fmt.Errorf("query wrongly formatted")
}
if err != nil {
p, err = st.GetPoll(pollid)
if err != nil {
return fmt.Errorf("could not get poll by poll id: %v", err)
}
}
// danger! malicious client could send pollid from another user in query
if p.UserID != update.CallbackQuery.From.ID {
return fmt.Errorf("user does not own poll: %v", err)
}
if toggleInactive {
p.Inactive = 1 - p.Inactive // states are 0 and 1
_, err = st.SavePoll(p)
if err != nil {
log.Println("Could not save toggled inactive state.")
}
}
if toggleMultipleChoice {
p.Type = 1 - p.Type // states are 0 and 1
_, err = st.SavePoll(p)
if err != nil {
log.Println("Could not save toggled multiple choice state.")
}
}
body := "This is the poll currently selected:\n<pre>\n"
body += p.Question + "\n"
for i, o := range p.Options {
body += fmt.Sprintf("%d. %s", i+1, o.Text) + "\n"
}
body += "</pre>\n\n"
var ed tgbotapi.EditMessageTextConfig
ed.Text = body
ed.ParseMode = tgbotapi.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 {
return fmt.Errorf("could not update message: %v", err)
}
pollsToUpdate.enqueue(p.ID)
return nil
}
func parseQueryPayload(update tgbotapi.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
}