-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.go
456 lines (392 loc) · 10.4 KB
/
bot.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// -*- Go -*-
/* ------------------------------------------------ */
/* Golang source */
/* Author: Alexei Panov <[email protected]> */
/* ------------------------------------------------ */
package main
import (
"fmt"
"io"
"math/rand"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
"github.com/go-pg/pg"
log "github.com/sirupsen/logrus"
"gopkg.in/telegram-bot-api.v4"
)
// PhotoCache is a struct for store thread-safe caches
type PhotoCache struct {
cache map[int]string
mutex sync.RWMutex
}
var (
bot *tgbotapi.BotAPI
photoCache PhotoCache
filesCache FilesCacheMemory
// ErrorUserNotFound generic error for user is not found
ErrorUserNotFound = fmt.Errorf("user not found")
)
func botServe() (err error) {
var (
updates <-chan tgbotapi.Update
)
defer wg.Done()
photoCache.cache = make(map[int]string)
filesCache.cache = make(map[string]string)
if bot, err = tgbotapi.NewBotAPI(options.APIKey); err != nil {
return
}
bot.Debug = options.Debug
log.Debug("Telegram bot initialized sucessful")
go updatePhotoCache()
go filesCache.Update()
updateOptions := tgbotapi.NewUpdate(0)
updateOptions.Timeout = 60
if updates, err = bot.GetUpdatesChan(updateOptions); err != nil {
return
}
for update := range updates {
if update.Message == nil {
continue
}
go func() {
if err = saveMessage(update.Message); err != nil {
log.Errorf("Unable to save message: %s", err)
}
}()
// Insult
go insultMessage(update.Message)
// command handler
if update.Message.Command() != "" {
go commandsMainHandler(update.Message)
}
}
return
}
func saveMessage(msg *tgbotapi.Message) (err error) {
// Files
if msg.Audio != nil {
go getFile(msg.Audio.FileID)
}
if msg.Document != nil {
go getFile(msg.Document.FileID)
}
if msg.Photo != nil {
for _, f := range *msg.Photo {
go getFile(f.FileID)
}
}
if msg.Sticker != nil {
go getFile(msg.Sticker.FileID)
}
if msg.Video != nil {
go getFile(msg.Video.FileID)
}
if msg.Voice != nil {
go getFile(msg.Voice.FileID)
}
if msg.From != nil {
go getUserPhoto(msg.From)
}
return dbSaveMessage(msg)
}
func getFile(fileID string) {
var (
err error
filename string
f tgbotapi.File
)
fc := tgbotapi.FileConfig{}
fc.FileID = fileID
if f, err = bot.GetFile(fc); err != nil {
log.Errorf("Unable to get file FileID [%s]: %s", fileID, err)
return
}
if filename, err = getFileName(fileID); err != nil {
log.Errorf("Unable to get file name for file ID %s: %s", fileID, err)
return
}
var stat os.FileInfo
if stat, err = os.Stat(filename); err == nil {
if stat.Size() == int64(f.FileSize) {
log.Debugf("File %s found. Skip it.", filename)
return
}
}
// check directory
path := filepath.Dir(filename)
if err = os.MkdirAll(path, 0755); err != nil {
log.Errorf("Unable to make directories for FileID [%s]: %s", fileID, err)
return
}
if err = downloadImage(f.Link(options.APIKey), filename); err != nil {
log.Errorf("Unable to download file for FileID [%s]: %s", fileID, err)
return
}
if err = dbSaveFileToCahce(fileID, f.FilePath); err != nil {
log.Errorf("Unabel to save file to cache ID %s file name %s: %s", fileID, filename, err)
return
}
log.Debugf("File downloaded for FileID [%s] in %s", fileID, filename)
}
func getShortFileName(fileID string) (filename string) {
var (
fcache FileCache
f tgbotapi.File
err error
)
if fn := filesCache.Get(fileID); fn != "" {
return fn
}
if fcache, err = getFileFromCache(fileID); err != nil && err != pg.ErrNoRows {
log.Errorf("Unable to get file from cache file ID %s: %s", fileID, err)
} else if err == nil {
log.Debugf("File with ID %s found in cache: %s", fileID, fcache.FileName)
return fcache.FileName
}
config := tgbotapi.FileConfig{FileID: fileID}
if f, err = bot.GetFile(config); err != nil {
return
}
filename = f.FilePath
if err = dbSaveFileToCahce(fileID, filename); err != nil {
log.Errorf("Unable to save file cache for ID=%s and file name %s: %s", fileID, filename, err)
}
return
}
func getFileName(fileID string) (filename string, err error) {
if filename = getShortFileName(fileID); filename == "" {
err = fmt.Errorf("unable to get file name for file ID %s", fileID)
return
}
filename = filepath.Join(options.StaticDirPath, filename)
return
}
func downloadImage(url string, filename string) (err error) {
resp, err := http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
file, err := os.Create(filename)
if err != nil {
return
}
defer file.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
return
}
return
}
func getUserPhotoFilename(user *tgbotapi.User) (filename string, err error) {
photoCache.mutex.RLock()
if fn, ok := photoCache.cache[user.ID]; ok {
photoCache.mutex.RUnlock()
if fn == "" {
return
}
return filepath.Base(fn), nil
}
photoCache.mutex.RUnlock()
if err = getUserPhoto(user); err != nil {
return
}
photoCache.mutex.RLock()
filename = photoCache.cache[user.ID]
photoCache.mutex.RUnlock()
if filename == "" {
return
}
filename = filepath.Base(filename)
return
}
func getUserPhoto(user *tgbotapi.User) (err error) {
var (
photos tgbotapi.UserProfilePhotos
link string
)
config := tgbotapi.NewUserProfilePhotos(user.ID)
if photos, err = bot.GetUserProfilePhotos(config); err != nil {
err = fmt.Errorf("Unable to get user profile photos for user with ID %d: %s", user.ID, err)
return
}
if photos.TotalCount == 0 {
return
}
if link, err = bot.GetFileDirectURL(photos.Photos[0][0].FileID); err != nil {
err = fmt.Errorf("unable to get file direct URL for file ID %s", photos.Photos[0][0].FileID)
return
}
fullFileName := filepath.Join(options.StaticDirPath, fmt.Sprintf("%d.jpg", user.ID))
if err = downloadImage(link, fullFileName); err != nil {
err = fmt.Errorf("Unable to download file %s to %s: %s", link, fullFileName, err)
return
}
photoCache.mutex.Lock()
photoCache.cache[user.ID] = fullFileName
photoCache.mutex.Unlock()
return
}
func updatePhotoCache() {
log.Debugf("Start update photo cache...")
var (
users []tgbotapi.User
err error
)
if users, err = getUsers(); err != nil {
log.Errorf("Unable to update photo cache: %s", err)
return
}
for _, user := range users {
go func(user tgbotapi.User) {
if err = getUserPhoto(&user); err != nil {
log.Errorf("Unable to get user photo: %s", err)
return
}
}(user)
}
log.Debugf("Finish update photo cache.")
}
func sendMessage(chatID int64, text string, replyID int) {
var (
omsg tgbotapi.Message
err error
)
blockSize := 4096
if len(text) > blockSize {
log.Debugf("Message to big, size %d, must be cut", len(text))
sendMessage(chatID, "* Сообщение слишком большое. Текст будет обрезан! *", replyID)
suffix := ""
if strings.HasPrefix(text, "```") {
blockSize = 4088
suffix = "```"
}
text = text[:blockSize]
text += suffix
}
msg := tgbotapi.NewMessage(chatID, text)
msg.ParseMode = "Markdown"
if replyID != 0 {
msg.ReplyToMessageID = replyID
}
if omsg, err = bot.Send(msg); err != nil {
// oops, try to send as plain text
log.Warnf("oops, unable to send markdown message [%s]: %s. Try to send as plain text.", text, err)
msg.ParseMode = ""
if omsg, err = bot.Send(msg); err != nil {
log.Errorf("Unable to send message to %d with text [%s] and reply [%d]: %s", chatID, text, replyID, err)
return
}
}
if err = saveMessage(&omsg); err != nil {
log.Errorf("Unable to save outgoing message: %s", err)
}
}
func isUserAdmin(chat *tgbotapi.Chat, user *tgbotapi.User) bool {
if chat == nil {
return false
}
var err error
config := tgbotapi.ChatConfig{
ChatID: chat.ID,
}
var admins []tgbotapi.ChatMember
if admins, err = bot.GetChatAdministrators(config); err != nil {
log.Errorf("Unable to get chat administrators: %s", err)
return false
}
for _, admin := range admins {
if admin.User.ID == user.ID {
return true
}
}
return false
}
func sendMessageToAdmins(msg *tgbotapi.Message) {
var err error
config := tgbotapi.ChatConfig{
ChatID: msg.Chat.ID,
}
var admins []tgbotapi.ChatMember
if admins, err = bot.GetChatAdministrators(config); err != nil {
log.Errorf("Unable to get chat administrators: %s", err)
return
}
link := fmt.Sprintf("Новая жалоба на спам: https://t.me/%s/%d", msg.Chat.UserName, msg.ReplyToMessage.MessageID)
for _, admin := range admins {
go sendMessage(int64(admin.User.ID), link, 0)
}
}
func isMeAdmin(chat *tgbotapi.Chat) bool {
var (
me tgbotapi.User
err error
)
if me, err = bot.GetMe(); err != nil {
log.Errorf("Unable to get me: %s", err)
return false
}
return isUserAdmin(chat, &me)
}
func sendMessageToAllChats(text string) {
if text == "" {
log.Warn("Unable to send empty message to all chats!")
return
}
var (
chats []tgbotapi.Chat
err error
)
if chats, err = getChats(); err != nil {
log.Errorf("Unable to get all chats in sending message [%s] to all chats: %s", text, err)
return
}
for _, chat := range chats {
if !chat.IsGroup() && !chat.IsSuperGroup() && !chat.IsChannel() { // skip channels, private and other chats
continue
}
go sendMessage(chat.ID, text, 0)
}
}
func insultMessage(msg *tgbotapi.Message) {
var (
targets []string
words []string
err error
)
if targets, err = dbInsultGetWordsOrTargets(false); err != nil {
log.Errorf("Unable to get insult targets: %s", err)
return
}
if words, err = dbInsultGetWordsOrTargets(true); err != nil {
log.Errorf("Unable to get insult words: %s", err)
return
}
for _, target := range targets {
if strings.Contains(strings.ToLower(msg.Text), strings.ToLower(target)) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
if r.Intn(4) == 0 {
log.Debugf("Found %s in message. Skip it randomly.", target)
break
}
// break if target word in URL
re := regexp.MustCompile(`(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?`)
for _, url := range re.FindAllString(msg.Text, -1) {
if strings.Contains(strings.ToLower(url), strings.ToLower(target)) {
log.Debugf("Target word \"%s\" in URL [%s]", target, url)
return
}
}
random := r.Int63n(int64(len(words)))
sendMessage(msg.Chat.ID, fmt.Sprintf("%s - %s", target, words[random]), msg.MessageID)
log.Debugf("Found %s in message. Answer %s - %s.", target, target, words[random])
break
}
}
}