-
Notifications
You must be signed in to change notification settings - Fork 3
/
handlers.go
118 lines (101 loc) · 2.81 KB
/
handlers.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
package main
import (
"fmt"
"log"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func ProcessUpdate(update tgbotapi.Update) {
// If we've gotten a message update.
if update.Message != nil {
msg := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{
ChatID: update.Message.Chat.ID,
},
DisableWebPagePreview: true,
}
if update.Message.Text == "/start" {
msg.Text = "Just ask question"
if _, err := bot.Send(msg); err != nil {
log.Println(err)
}
return
}
if update.Message.Text == "/queue" {
_, n := qu.Load(update.Message.From.ID)
switch n {
case -1:
if currentTask != nil && currentTask.UserID == update.Message.From.ID {
msg.Text = "It's your turn now!!!"
} else {
msg.Text = "Hey! You haven't asked question yet!"
}
case 0:
msg.Text = "Hold a second, you're next"
default:
msg.Text = fmt.Sprintf("Hold on! Your queue is %d", n)
}
if _, err := bot.Send(msg); err != nil {
log.Println(err)
}
return
}
if chars := []rune(update.Message.Text); string(chars[0]) == "/" {
msg.Text = "There is no such command"
if _, err := bot.Send(msg); err != nil {
log.Println(err)
}
return
}
// Do enqueue task
task := Task{
UserID: update.Message.From.ID,
Stop: make(chan bool),
}
if reply := update.Message.ReplyToMessage; reply != nil && reply.From.ID == bot.Self.ID {
task.WrapPrevContext(reply.Text, update.Message.Text)
} else {
task.WrapInRoles(update.Message.Text)
}
n, err := qu.Enqueue(&task)
log.Println(err)
if err != nil {
if err == ErrOnePerUser {
msg.Text = "You've already asked your question. You can edit the existing one until it's your turn"
}
if err == ErrQueueLimit {
msg.Text = fmt.Sprintf("Now queue is full %d/%d. Wait one slot to be free at least.\nCheck queue /stats", n, qu.Limit)
}
if _, err := bot.Send(msg); err != nil {
log.Println(err)
}
return
}
msg.Text = fmt.Sprintf("Your question registered! Your queue is %d/%d.\nYou can edit your message until it's your turn", n, qu.Limit)
sent, err := bot.Send(msg)
if err != nil {
log.Println(err)
}
task.AnnounceID = sent.MessageID
}
if update.EditedMessage != nil {
task := Task{
UserID: update.EditedMessage.From.ID,
MessageID: update.EditedMessage.MessageID,
Question: update.EditedMessage.Text,
}
qu.Enqueue(&task)
}
if update.CallbackQuery != nil {
if update.CallbackQuery.Data == "/stop" && currentTask != nil {
if !currentTask.Stopped {
callback := tgbotapi.NewCallback(update.CallbackQuery.ID, "Stopping")
bot.Request(callback)
currentTask.Stop <- true
currentTask.Stopped = true
} else {
callback := tgbotapi.NewCallback(update.CallbackQuery.ID, "Already stopped")
bot.Request(callback)
}
}
}
}