-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback_handler.go
192 lines (145 loc) · 3.88 KB
/
callback_handler.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
package main
import (
"fmt"
"log"
"strconv"
"strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func callbackHandling(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, update.CallbackQuery.Data)
callbackText := update.CallbackQuery.Data
var score, qID, cID string
if strings.Contains(callbackText, "score") {
spl := strings.Split(callbackText, "-")
s := strings.Split(spl[0], "=")
score = s[1]
q := strings.Split(spl[1], "=")
qID = q[1]
c := strings.Split(spl[2], "=")
cID = c[1]
}
switch update.CallbackQuery.Data {
case "backToMainMenu":
msg.Text = menu
msg.ReplyMarkup = mainMenuKeyboard
case "quiz":
user := users{telegramID: update.CallbackQuery.From.ID}
go resetTest(user.telegramID)
countChan := make(chan int)
go func() {
num := numberOfQuestions()
countChan <- num
}()
count := <-countChan
questionChan := make(chan string)
cIDChan := make(chan int)
errorChan := make(chan error)
go func() {
question, cID, err := questionWalker(1)
questionChan <- question
cIDChan <- cID
errorChan <- err
}()
question := fmt.Sprintf("سوال شماره %d از مجموع %d سوال:\n", 1, count)
question += <-questionChan
cID := <-cIDChan
err := <-errorChan
if err != nil {
log.Fatal(err)
}
msg.Text = question
msg.ReplyMarkup = scoreButtons(1, cID)
case "score=" + score + "-qid=" + qID + "-cid=" + cID:
user := users{}
user.telegramID = update.CallbackQuery.From.ID
score, _ := strconv.Atoi(score)
qID, _ := strconv.Atoi(qID)
cID, _ := strconv.Atoi(cID)
go setScore(user.telegramID, score, qID, cID)
countChan := make(chan int)
go func() {
num := numberOfQuestions()
countChan <- num
}()
count := <-countChan
questionChan := make(chan string)
cIDChan := make(chan int)
errorChan := make(chan error)
go func() {
question, cID, err := questionWalker(qID + 1)
questionChan <- question
cIDChan <- cID
errorChan <- err
}()
question := fmt.Sprintf("سوال شماره %d از مجموع %d سوال:\n", qID+1, count)
question += <-questionChan
categotyID := <-cIDChan
err := <-errorChan
if err != nil {
go setTestCompleted(user.telegramID)
ltChan := make(chan []string)
go func() {
lt := showLifetraps(user.telegramID)
ltChan <- lt
}()
lifetraps := <-ltChan
if len(lifetraps) == 0 {
msg.Text = noLifetrap
msg.ReplyMarkup = backToMainMenuKeyboard
} else {
var lifetrapsInText string
for i, lifetrap := range lifetraps {
lifetrapsInText += fmt.Sprintf("تله شماره %d:\n%s\n", i+1, lifetrap)
}
msg.Text = testEnded + lifetrapsInText
msg.ReplyMarkup = backToMainMenuKeyboard
}
} else {
msg.Text = question
msg.ReplyMarkup = scoreButtons(qID+1, categotyID)
}
case "myLifeTraps":
user := users{telegramID: update.CallbackQuery.From.ID}
testedChan := make(chan bool)
go func() {
t := isTestCompleted(user.telegramID)
testedChan <- t
}()
user.tested = <-testedChan
if user.tested {
ltChan := make(chan []string)
go func() {
lt := showLifetraps(user.telegramID)
ltChan <- lt
}()
lifetraps := <-ltChan
if len(lifetraps) == 0 {
msg.Text = noLifetrap
msg.ReplyMarkup = backToMainMenuKeyboard
} else {
var lifetrapsInText string
for i, lifetrap := range lifetraps {
lifetrapsInText += fmt.Sprintf("تله شماره %d:\n%s\n", i+1, lifetrap)
}
msg.Text = showingLifetraps + lifetrapsInText
msg.ReplyMarkup = backToMainMenuKeyboard
}
} else {
msg.Text = testNotCompleted
msg.ReplyMarkup = backToMainMenuKeyboard
}
case "guide":
msg.Text = guide
msg.ReplyMarkup = backToMainMenuKeyboard
}
errorCh := make(chan error)
go func() {
_, err := bot.Send(msg)
errorCh <- err
}()
err := <-errorCh
if err != nil {
log.Panic(err)
}
}