-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialog.go
202 lines (171 loc) · 4.69 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
package main
import (
"fmt"
"log"
"strings"
"github.com/go-telegram-bot-api/telegram-bot-api"
)
func handleDialog(bot *tgbotapi.BotAPI, update tgbotapi.Update, st Store) error {
state := ohHi
pollid := -1
var err error
if strings.Contains(update.Message.Text, locAboutCommand) {
msg := tgbotapi.NewMessage(int64(update.Message.From.ID), locAboutMessage)
_, err = bot.Send(&msg)
if err != nil {
return fmt.Errorf("could not send message: %v", err)
}
return err
}
if strings.Contains(update.Message.Text, locDonateCommand) {
fmt.Println("found donate command")
msg := tgbotapi.NewMessage(int64(update.Message.From.ID), locDonateMessage)
msg.ParseMode = tgbotapi.ModeHTML
_, err = bot.Send(&msg)
if err != nil {
return fmt.Errorf("could not send message: %v", err)
}
return err
}
state, pollid, err = st.GetState(update.Message.From.ID)
if err != nil {
// could not retrieve state -> state is zero
state = ohHi
log.Printf("could not get state from database: %v\n", err)
}
if strings.Contains(update.Message.Text, locEditCommand) {
polls, err := st.GetPollsByUser(update.Message.From.ID)
if err != nil || len(polls) == 0 {
log.Printf("could not get polls of user with userid %d: %v", update.Message.From.ID, err)
msg := tgbotapi.NewMessage(int64(update.Message.From.ID), locNoMessageToEdit)
_, err = bot.Send(&msg)
if err != nil {
return fmt.Errorf("could not send message: %v", err)
}
return fmt.Errorf("could not find message to edit: %v", err)
}
var p *poll
for _, p = range polls {
if p.ID == pollid {
break
}
}
_, err = sendEditMessage(bot, update, 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(update.Message.From.ID, pollid, state)
if err != nil {
return fmt.Errorf("could not save state: %v", 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: update.Message.From.ID,
}
pollid, err = st.SavePoll(p)
if err != nil {
return fmt.Errorf("could not save poll: %v", err)
}
msg := tgbotapi.NewMessage(int64(update.Message.From.ID), locGotQuestion)
_, err = bot.Send(&msg)
if err != nil {
return fmt.Errorf("could not send message: %v", err)
}
state = waitingForOption
err = st.SaveState(update.Message.From.ID, pollid, state)
if err != nil {
return fmt.Errorf("could not save state: %v", err)
}
return nil
}
if state == editQuestion {
p, err := st.GetPoll(pollid)
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 := tgbotapi.NewMessage(
int64(update.Message.From.ID),
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(update.Message.From.ID, pollid, state)
if err != nil {
return fmt.Errorf("could not save state: %v", err)
}
//return nil
}
if state == editPoll {
p, err := st.GetPoll(pollid)
if err != nil {
return fmt.Errorf("could not get poll: %v", err)
}
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"
msg := tgbotapi.NewMessage(
update.Message.Chat.ID,
body)
msg.ParseMode = tgbotapi.ModeHTML
msg.ReplyMarkup = buildEditMarkup(p, false, false)
_, err = bot.Send(msg)
if err != nil {
return fmt.Errorf("could not send message: %v", err)
}
}
if state == waitingForOption {
opts := []option{
option{
PollID: pollid,
Text: update.Message.Text,
}}
err = st.SaveOptions(opts)
if err != nil {
return fmt.Errorf("could not save option: %v", err)
}
p, err := st.GetPoll(pollid)
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
}
if state == pollDone {
p, err := st.GetPoll(pollid)
if err != nil {
return fmt.Errorf("could not get poll: %v", err)
}
_, err = postPoll(bot, p, int64(update.Message.From.ID))
if err != nil {
return fmt.Errorf("could not post poll: %v", err)
}
return nil
}
return nil
}