-
Notifications
You must be signed in to change notification settings - Fork 1
/
updatechain.go
376 lines (286 loc) · 7.54 KB
/
updatechain.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package tg
import (
"encoding/json"
"path"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
// Update is an update response, from Telegram GetUpdates.
type Update tgbotapi.Update
// UpdateType is a type of update chain
type UpdateType int
// UpdateChain contains chain of updates
type UpdateChain struct {
updateType UpdateType
updates []Update
}
type callbackData struct {
S string `json:"s"`
I string `json:"i"`
}
const (
// UpdateTypeNone - type `none` for update chain.
// No type has not been set yet or chain has been cleaned up.
UpdateTypeNone UpdateType = iota
// UpdateTypeUnknown - unknown update type
UpdateTypeUnknown
// UpdateTypeMessage - type message
UpdateTypeMessage
// UpdateTypeCallback - type callback
UpdateTypeCallback
)
func (u UpdateType) String() string {
return [...]string{"none", "unknown", "message", "callback"}[u]
}
// Get gets all updates from chain
func (uc *UpdateChain) Get() []Update {
return uc.updates
}
// MessageTextGet gets messages text or captions for every update from chain.
// Chain must have message type
func (uc *UpdateChain) MessageTextGet() []string {
var text []string
if uc.updateType != UpdateTypeMessage {
return text
}
for _, u := range uc.updates {
if u.Message != nil {
if len(u.Message.Text) > 0 {
text = append(text, u.Message.Text)
} else if len(u.Message.Caption) > 0 {
text = append(text, u.Message.Caption)
}
}
}
return text
}
// MessagesIDsGet gets update ids from updates chain
func (uc *UpdateChain) MessagesIDsGet() []int {
var ids []int
switch uc.updateType {
case UpdateTypeMessage:
for _, u := range uc.updates {
ids = append(ids, u.Message.MessageID)
}
case UpdateTypeCallback:
for _, u := range uc.updates {
ids = append(ids, u.CallbackQuery.Message.MessageID)
}
}
return ids
}
// MessagesIDGet gets update id from first update element from chain
func (uc *UpdateChain) MessagesIDGet() int {
if len(uc.updates) == 0 {
return 0
}
u := uc.updates[0]
switch uc.updateType {
case UpdateTypeMessage:
return u.Message.MessageID
case UpdateTypeCallback:
return u.CallbackQuery.Message.MessageID
}
return 0
}
// CallbackQueryIDGet gets callback ID from first update element from chain.
// Chain must have callback type
func (uc *UpdateChain) CallbackQueryIDGet() string {
if uc.updateType != UpdateTypeCallback {
return ""
}
if len(uc.updates) == 0 {
return ""
}
return uc.updates[0].CallbackQuery.ID
}
// FilesGet gets files from update chain.
// At the time only Photo, Document and Voice types are supported
func (uc *UpdateChain) FilesGet(t Telegram) ([]File, error) {
var files []File
if uc.updateType != UpdateTypeMessage {
return []File{}, ErrUpdateWrongType
}
for _, u := range uc.updates {
if elt := u.Message.Photo; len(elt) > 0 {
// Get last element in array (largest by size)
f, err := fileGet(t, elt[len(elt)-1].FileID, "")
if err != nil {
return []File{}, nil
}
files = append(files, f)
}
if elt := u.Message.Voice; elt != nil {
f, err := fileGet(t, (*elt).FileID, "")
if err != nil {
return []File{}, nil
}
files = append(files, f)
}
if elt := u.Message.Document; elt != nil {
f, err := fileGet(t, elt.FileID, elt.FileName)
if err != nil {
return []File{}, nil
}
files = append(files, f)
}
if elt := u.Message.Video; elt != nil {
f, err := fileGet(t, elt.FileID, elt.FileName)
if err != nil {
return []File{}, nil
}
files = append(files, f)
}
if elt := u.Message.Audio; elt != nil {
f, err := fileGet(t, elt.FileID, elt.FileName)
if err != nil {
return []File{}, nil
}
files = append(files, f)
}
if elt := u.Message.Sticker; elt != nil {
f, err := fileGet(t, elt.FileID, elt.Emoji)
if err != nil {
return []File{}, nil
}
files = append(files, f)
}
}
return files, nil
}
// TypeGet gets chain type
func (uc *UpdateChain) TypeGet() UpdateType {
return uc.updateType
}
// add adds new updates into update chain
func (uc *UpdateChain) add(updates []Update) {
for _, u := range updates {
t := updateTypeEltGet(u)
if t == UpdateTypeUnknown {
continue
}
// If chain has no type yet
if uc.updateType == UpdateTypeNone {
uc.updateType = t
}
// Skip new elements with different type
if uc.updateType != t {
continue
}
// Add new element into chain
uc.updates = append(uc.updates, u)
}
}
func (uc *UpdateChain) callbackSessionStateGet() (SessionState, string, error) {
var d callbackData
data := uc.callbackDataGet()
if len(data) == 0 {
return sessionBreak, "", nil
}
if err := json.Unmarshal([]byte(data), &d); err != nil {
return sessionBreak, "", err
}
return SessionState{d.S}, d.I, nil
}
// callbackDataGet gets callback data from first update element from chain.
// Chain must have callback type
func (uc *UpdateChain) callbackDataGet() string {
if uc.updateType != UpdateTypeCallback {
return ""
}
if len(uc.updates) == 0 {
return ""
}
return uc.updates[0].CallbackQuery.Data
}
// commandCheck checks first update element in chain has command signs.
// If so command and its args will be returned.
// Chain must have message type
func (uc *UpdateChain) commandCheck() (string, string) {
if uc.updateType != UpdateTypeMessage {
return "", ""
}
if len(uc.updates) == 0 {
return "", ""
}
update := uc.updates[0]
return update.Message.Command(), update.Message.CommandArguments()
}
// updateTypeEltGet gets type for specified update element
func updateTypeEltGet(update Update) UpdateType {
if update.Message != nil {
return UpdateTypeMessage
}
if update.CallbackQuery != nil {
return UpdateTypeCallback
}
return UpdateTypeUnknown
}
// updateIDsGet gets chat and user ID from specified update element
func updateIDsGet(update Update) (int64, int64) {
switch updateTypeEltGet(update) {
case UpdateTypeMessage:
return update.Message.Chat.ID, update.Message.From.ID
case UpdateTypeCallback:
return update.CallbackQuery.Message.Chat.ID, update.CallbackQuery.From.ID
}
return 0, 0
}
// updateUserNameGet gets user name from specified update element
func updateUserNameGet(update Update) string {
switch updateTypeEltGet(update) {
case UpdateTypeMessage:
return update.Message.From.UserName
case UpdateTypeCallback:
return update.CallbackQuery.From.UserName
}
return ""
}
// updateFirstNameGet gets user name from specified update element
func updateFirstNameGet(update Update) string {
switch updateTypeEltGet(update) {
case UpdateTypeMessage:
return update.Message.From.FirstName
case UpdateTypeCallback:
return update.CallbackQuery.From.FirstName
}
return ""
}
// updateLastNameGet gets user name from specified update element
func updateLastNameGet(update Update) string {
switch updateTypeEltGet(update) {
case UpdateTypeMessage:
return update.Message.From.LastName
case UpdateTypeCallback:
return update.CallbackQuery.From.LastName
}
return ""
}
func callbackDataGen(state SessionState, identifier string) (string, error) {
d := callbackData{
S: state.state,
I: identifier,
}
b, err := json.Marshal(&d)
if err != nil {
return "", err
}
return string(b), nil
}
// fileGet gets file by specified file ID from Telegram
// If `fileName` is empty base part of file path will be used.
func fileGet(t Telegram, fileID, fileName string) (File, error) {
f, err := t.bot.GetFile(tgbotapi.FileConfig{
FileID: fileID,
})
if err != nil {
return File{}, err
}
if fileName == "" {
fileName = path.Base(f.FilePath)
}
return File{
FileSize: f.FileSize,
FileName: fileName,
f: f,
}, nil
}