forked from jheuel/pollrBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialog.go
225 lines (196 loc) · 5.03 KB
/
dialog.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
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
tg "github.com/semog/go-bot-api/v4"
"k8s.io/klog"
)
func handleDialog(bot *tg.BotAPI, update tg.Update, st Store) error {
state := ohHi
pollID := -1
userID, err := getUpdateUserID(update)
if err != nil {
return err
}
if strings.Contains(update.Message.Text, locAboutCommand) {
msg := tg.NewMessage(int64(userID), locAboutMessage)
_, err = bot.Send(&msg)
if err != nil {
return fmt.Errorf("could not send message: %v", err)
}
return err
}
state, pollID, err = st.GetState(userID)
if err != nil {
// could not retrieve state -> state is zero
state = ohHi
klog.Infof("could not get state from database: %v\n", err)
}
if strings.Contains(update.Message.Text, locEditCommand) {
polls, err := st.GetPollsByUser(userID)
if err != nil || len(polls) == 0 {
klog.Infof("could not get polls of user with userID %d: %v", userID, err)
state = ohHi
err = st.SaveState(userID, pollID, state)
if err != nil {
return err
}
msg := tg.NewMessage(int64(update.Message.From.ID), locNoMessageToEdit)
_, err = bot.Send(&msg)
if err != nil {
return fmt.Errorf("could not send message: %v", err)
}
} else {
var p *poll
for _, p = range polls {
if p.ID == pollID {
break
}
}
_, err = sendEditMessage(bot, int64(userID), p)
if err != nil {
return fmt.Errorf("could not send edit message: %v", err)
}
return nil
}
}
if strings.Contains(update.Message.Text, "/start") || pollID < 0 && state != waitingForQuestion {
state = ohHi
err = st.SaveState(userID, pollID, state)
if err != nil {
return err
}
}
if state == ohHi {
_, err = sendMainMenuMessage(bot, update)
if err != nil {
return fmt.Errorf("could not send main menu message: %v", err)
}
return nil
}
if state == waitingForQuestion {
p := &poll{
Question: update.Message.Text,
UserID: userID,
}
pollID, err = st.SavePoll(p)
if err != nil {
return fmt.Errorf("could not save poll: %v", err)
}
msg := tg.NewMessage(int64(userID), locGotQuestion)
_, err = bot.Send(&msg)
if err != nil {
return fmt.Errorf("could not send message: %v", err)
}
state = waitingForOption
err = st.SaveState(userID, pollID, state)
if err != nil {
return err
}
return nil
}
if state == editQuestion {
p, err := st.GetUserPoll(pollID, userID)
if err != nil {
return fmt.Errorf("could not get poll: %v", err)
}
p.Question = update.Message.Text
_, err = st.SavePoll(p)
if err != nil {
return fmt.Errorf("could not save poll: %v", err)
}
msg := tg.NewMessage(int64(userID), fmt.Sprintf(locGotEditQuestion, p.Question))
_, err = bot.Send(&msg)
if err != nil {
return fmt.Errorf("could not send message: %v", err)
}
state = editPoll
err = st.SaveState(userID, pollID, state)
if err != nil {
return err
}
pollsToUpdate.enqueue(pollID)
}
if state == pollDone {
state = editPoll
err = st.SaveState(userID, pollID, state)
if err != nil {
return err
}
}
if state == editPoll {
p, err := st.GetUserPoll(pollID, userID)
if err != nil {
return fmt.Errorf("could not get poll: %v", err)
}
_, err = sendEditMessage(bot, int64(update.Message.Chat.ID), p)
if err != nil {
return fmt.Errorf("could not send message: %v", err)
}
}
if state == waitingForOption {
p, err := st.GetUserPoll(pollID, userID)
if err != nil {
return fmt.Errorf("could not get poll: %v", err)
}
isEdit, err := regexp.MatchString(`^[0-9]+\.[A-Za-z0-9 ]+`, update.Message.Text)
if err != nil {
return fmt.Errorf("could not match regexp: %v", err)
}
isDelete, err := regexp.MatchString(`^[0-9]+\.$`, update.Message.Text)
if err != nil {
return fmt.Errorf("could not match regexp: %v", err)
}
optID := 0
optText := update.Message.Text
if isEdit || isDelete {
r, err := regexp.Compile(`^([0-9]+)\.[ \t]*(.*)`)
if err != nil {
return fmt.Errorf("could not compile regexp: %v", err)
}
optionMatches := r.FindStringSubmatch(update.Message.Text)
if optionMatches == nil {
return fmt.Errorf("could not match regexp: %v", err)
}
optNum, err := strconv.Atoi(optionMatches[1])
if err != nil {
return fmt.Errorf("could not convert string to number: %v", err)
}
if optNum < 1 || optNum > len(p.Options) {
return fmt.Errorf("option out of range for edit or delete: %v", optNum)
}
optID = p.Options[optNum-1].ID
if len(optionMatches) > 2 {
optText = optionMatches[2]
}
}
opts := []option{
{
ID: optID,
PollID: pollID,
Text: optText,
}}
if isDelete {
err = st.DeleteOptions(opts)
} else {
err = st.SaveOptions(opts)
}
if err != nil {
return fmt.Errorf("could not save option: %v", err)
}
pollsToUpdate.enqueue(pollID)
// Refresh the poll
p, err = st.GetUserPoll(pollID, userID)
if err != nil {
return fmt.Errorf("could not get poll: %v", err)
}
_, err = sendInterMessage(bot, update, p)
if err != nil {
return fmt.Errorf("could not send inter message: %v", err)
}
return nil
}
return nil
}