-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
77 lines (63 loc) · 1.83 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
package main
import (
"context"
"fmt"
"strings"
"time"
"github.com/go-redis/redis/v8"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func StartBot(bot *tgbotapi.BotAPI, redisClient *redis.Client) {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message != nil {
go handleMessage(bot, redisClient, update.Message)
}
}
}
func handleMessage(bot *tgbotapi.BotAPI, redisClient *redis.Client, msg *tgbotapi.Message) {
ctx := context.Background()
if msg.ReplyToMessage != nil && isMeowMessage(msg.Text) && mirrorShield(msg) {
key := fmt.Sprintf("%d:%d", msg.Chat.ID, msg.ReplyToMessage.From.ID)
count, _ := redisClient.Incr(ctx, key).Result()
name := msg.ReplyToMessage.From.FirstName
response := fmt.Sprintf("%s стал котенком уже %d %s!", name, count, getDeclension(int(count)))
sentMsg, _ := bot.Send(tgbotapi.NewMessage(msg.Chat.ID, response))
go func() {
time.Sleep(5 * time.Second)
deleteConfig := tgbotapi.DeleteMessageConfig{
ChatID: sentMsg.Chat.ID,
MessageID: sentMsg.MessageID,
}
bot.Request(deleteConfig)
}()
return
}
}
func isMeowMessage(text string) bool {
meowMessages := []string{"мяу", "мур", "meow", "мяуу", "мяу мяу", "purr", "мурр", "мур мур"}
for _, m := range meowMessages {
if strings.EqualFold(text, m) {
return true
}
}
return false
}
func mirrorShield(msg *tgbotapi.Message) bool {
if msg.ReplyToMessage.From.ID == msg.From.ID || msg.ReplyToMessage.From.IsBot {
return false
}
return true
}
func getDeclension(count int) string {
switch {
case count%10 == 1 && count%100 != 11:
return "раз"
case count%10 >= 2 && count%10 <= 4 && (count%100 < 10 || count%100 >= 20):
return "раза"
default:
return "раз"
}
}