-
Notifications
You must be signed in to change notification settings - Fork 1
/
telegram-bot-stop-command-handler.go
62 lines (50 loc) · 1.59 KB
/
telegram-bot-stop-command-handler.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
package v1
import (
"fmt"
"os"
"github.com/psyb0t/glogger"
"github.com/psyb0t/telegram-logger/internal/pkg/types"
)
const (
telegramBotByeMessageTpl = `Bye!
Chat ID %d has been removed from the system.`
)
// telegramBotStopCommandHandler handles the telegramBotStopCommand command
// of the Telegram bot. When this function is called, it removes all users
// with the provided chat ID from the system and sends notification to the user
// via Telegram.
func (a *app) telegramBotStopCommandHandler(chatID int64) error {
log := glogger.New(glogger.Caller{
Service: os.Getenv(serviceNameEnvVarName),
Package: packageName,
Receiver: "app",
Function: "telegramBotStopCommandHandler",
})
log.Debug("handling command")
user := types.User{TelegramChatID: chatID}
// define errMsg which is used to send a generic message to
// the sender of the command via telegram when an error occurs
// in the deferred function
errMsg := ""
defer func() {
if errMsg != "" {
if err := a.telegramBotSendMessage(user, errMsg); err != nil {
log.Err(err).Error("error when sending telegram error message")
}
}
}()
log.Data("chatID", chatID).Debug("deleting all users by Telegram chat ID")
err := a.db.GetUserRepositoryWriter().DeleteAllByTelegramChatID(chatID)
if err != nil {
errMsg = "an error occurred when trying to delete users"
log.Err(err).Error(errMsg)
return err
}
msg := fmt.Sprintf(telegramBotByeMessageTpl, chatID)
if err := a.telegramBotSendMessage(user, msg); err != nil {
errMsg = "could not send telegram bye message"
log.Err(err).Error(errMsg)
return err
}
return nil
}