/
commands.go
84 lines (67 loc) · 2.06 KB
/
commands.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
package register_bot
import tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
func (b *Bot) RegisterTextCommand(command string, textHandlers ...func() string) {
b.mu.Lock()
defer b.mu.Unlock()
b.commandHandlers[command] = CommandHandlers{
TextHandlers: textHandlers,
}
}
// Регистрация команды для обработки изображения по пути файла
func (b *Bot) RegisterImagePathCommand(command string, imageHandlers ...func() string) {
b.mu.Lock()
defer b.mu.Unlock()
b.commandHandlers[command] = CommandHandlers{
ImagePath: imageHandlers,
}
}
// Регистрация команды для обработки изображения в виде байтов
func (b *Bot) RegisterImageBytesCommand(command string, imageHandlers ...func() []byte) {
b.mu.Lock()
defer b.mu.Unlock()
b.commandHandlers[command] = CommandHandlers{
ImageBytes: imageHandlers,
}
}
func (b *Bot) BasicAuth(command string) {
b.registerBasicAuth[command] = 1
}
func (b *Bot) RegisterRegisterCommand(command string) {
b.registerRegisterCommand[command] = 1
}
func (b *Bot) SetPrivateCommand(command string) {
b.mu.Lock()
defer b.mu.Unlock()
b.privateCommands[command] = struct{}{}
}
func (b *Bot) AllowUser(username string) {
b.mu.Lock()
defer b.mu.Unlock()
b.allowedUsernames[username] = struct{}{}
}
func (b *Bot) isPrivateCommand(command string) bool {
b.mu.RLock()
defer b.mu.RUnlock()
_, isPrivate := b.privateCommands[command]
return isPrivate
}
func (b *Bot) isAllowedUser(username string) bool {
b.mu.RLock()
defer b.mu.RUnlock()
_, allowed := b.allowedUsernames[username]
return allowed
}
func (b *Bot) RegisterUserInputCommand(command string, callback UserInputCallback) {
b.mu.Lock()
defer b.mu.Unlock()
b.userInputCallbacks[command] = callback
b.commandHandlers[command] = CommandHandlers{}
}
func (b *Bot) SendMessageByChatID(chatID int64, message string) error {
response := tgbotapi.NewMessage(chatID, message)
if b.parseMode != "" {
response.ParseMode = b.parseMode
}
_, err := b.bot.Send(response)
return err
}