/
tg.go
192 lines (161 loc) · 4.75 KB
/
tg.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
package tg
import (
"context"
"log"
"log/slog"
"slices"
"strings"
"time"
"unicode"
"github.com/eko/gocache/lib/v4/cache"
gocache_store "github.com/eko/gocache/store/go_cache/v4"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
gocache "github.com/patrickmn/go-cache"
"github.com/vyneer/pacany-bot/config"
"github.com/vyneer/pacany-bot/db"
"github.com/vyneer/pacany-bot/tg/commands/implementation"
)
type Bot struct {
api *tgbotapi.BotAPI
adminCache *cache.Cache[[]tgbotapi.ChatMember]
tagDB *db.DB
}
func New(c *config.Config, tagDB *db.DB) (Bot, error) {
_ = tgbotapi.SetLogger(log.Default())
bot, err := tgbotapi.NewBotAPI(c.Token)
if err != nil {
return Bot{}, err
}
bot.Debug = c.Debug == 2
slog.Debug("authorized on bot", "account", bot.Self.UserName)
botCmdSlice := []tgbotapi.BotCommand{}
for _, v := range implementation.InteractableOrder {
if desc, show := v.GetDescription(); show {
botCmdSlice = append(botCmdSlice, tgbotapi.BotCommand{
Command: v.GetParentName() + v.GetName(),
Description: desc,
})
}
}
if _, err := bot.Request(tgbotapi.NewSetMyCommandsWithScope(
tgbotapi.NewBotCommandScopeAllChatAdministrators(),
botCmdSlice...,
)); err != nil {
return Bot{}, err
}
gocacheClient := gocache.New(10*time.Minute, 15*time.Minute)
gocacheStore := gocache_store.NewGoCache(gocacheClient)
cacheManager := cache.New[[]tgbotapi.ChatMember](gocacheStore)
return Bot{
api: bot,
tagDB: tagDB,
adminCache: cacheManager,
}, nil
}
func (b *Bot) Run() error {
u := tgbotapi.NewUpdate(-1)
u.Timeout = 60
u.AllowedUpdates = []string{"message", "chat_member"}
updates := b.api.GetUpdatesChan(u)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
for update := range updates {
switch {
case update.ChatMember != nil:
_, err := b.setAdmins(ctx, update.ChatMember.Chat.ChatConfig())
if err != nil {
slog.Warn("unable to update admin list", "err", err)
continue
}
case update.Message != nil && (update.Message.Chat.IsGroup() || update.Message.Chat.IsSuperGroup()):
chatID := update.Message.Chat.ID
text := update.Message.Text
username := update.Message.From.UserName
var commandResponse implementation.CommandResponse
if !update.Message.IsCommand() {
commandResponse = implementation.GetAutomaticCommand("tagscan").Run(ctx, implementation.CommandArgs{
DB: b.tagDB,
ChatID: chatID,
Args: []string{
username,
text,
},
})
} else {
admins, err := b.getAdmins(ctx, update.Message.Chat.ChatConfig())
if err != nil {
slog.Warn("unable to get admin list", "err", err)
continue
}
if slices.ContainsFunc[[]tgbotapi.ChatMember](admins, func(cm tgbotapi.ChatMember) bool {
return cm.User.ID == update.Message.From.ID
}) {
commandResponse = b.command(ctx, chatID, update.Message.Command(), update.Message.CommandArguments())
}
}
response := tgbotapi.NewMessage(update.Message.Chat.ID, "")
response.Text = commandResponse.Text
if commandResponse.Capitalize {
response.Text = b.capitalize(response.Text)
}
if commandResponse.Reply {
response.ReplyToMessageID = update.Message.MessageID
}
if len(response.Text) != 0 {
if _, err := b.api.Send(response); err != nil {
log.Panic(err)
}
}
}
}
return nil
}
func (b *Bot) command(ctx context.Context, chatID int64, command string, args string) implementation.CommandResponse {
argsSplit := strings.Fields(args)
cmd := implementation.GetInteractableCommand(command)
if cmd == nil {
return implementation.CommandResponse{
Text: "",
Reply: false,
Capitalize: true,
}
}
slog.Debug("running command", "chatID", chatID, "command", command)
return cmd.Run(ctx, implementation.CommandArgs{
DB: b.tagDB,
ChatID: chatID,
Args: argsSplit,
})
}
func (b *Bot) getAdmins(ctx context.Context, c tgbotapi.ChatConfig) ([]tgbotapi.ChatMember, error) {
slog.Debug("getting admin list", "chatID", c.ChatID)
admins, err := b.adminCache.Get(ctx, c.ChatID)
if err == nil {
return admins, nil
}
admins, err = b.setAdmins(ctx, c)
if err != nil {
return nil, err
}
return admins, nil
}
func (b *Bot) setAdmins(ctx context.Context, c tgbotapi.ChatConfig) ([]tgbotapi.ChatMember, error) {
slog.Debug("setting admin list", "chatID", c.ChatID)
admins, err := b.api.GetChatAdministrators(tgbotapi.ChatAdministratorsConfig{
ChatConfig: c,
})
if err != nil {
return nil, err
}
if err := b.adminCache.Set(ctx, c.ChatID, admins); err != nil {
return nil, err
}
return admins, nil
}
func (b *Bot) capitalize(s string) string {
if len(s) == 0 {
return s
}
r := []rune(s)
return string(append([]rune{unicode.ToUpper(r[0])}, r[1:]...))
}