butler/internal/services/bot/bot_all/bot.go

57 lines
1.2 KiB
Go
Raw Normal View History

2023-08-13 13:22:15 +00:00
package bot_all
import (
"context"
"strings"
"github.com/samber/lo"
"git.3crabs.ru/VLADIMIR/butler/internal/modules/messenger"
"git.3crabs.ru/VLADIMIR/butler/internal/modules/storage"
"git.3crabs.ru/VLADIMIR/butler/internal/services/bot"
)
type botAll struct {
messenger messenger.IMessenger
storage storage.IStorage
}
func NewBotAll(
messenger messenger.IMessenger,
storage storage.IStorage,
) bot.IBot {
return &botAll{
messenger: messenger,
storage: storage,
}
}
func (bot *botAll) Process(ctx context.Context, msg messenger.Message) error {
if err := bot.storage.UpsertUser(ctx, storage.User{ChatID: msg.ChatID, UserID: msg.UserID}); err != nil {
return err
}
if !strings.Contains(msg.Text, "@all") {
return nil
}
users, err := bot.storage.GetAllUsersByChatID(ctx, msg.ChatID)
if err != nil {
return err
}
2023-08-13 13:39:36 +00:00
usernames := lo.FilterMap(users, func(item storage.User, _ int) (string, bool) {
if item.UserID == msg.UserID {
return "", false
}
return item.UserID, true
2023-08-13 13:22:15 +00:00
})
if len(usernames) > 0 {
bot.messenger.SendMessage(
ctx,
&messenger.Message{
ChatID: msg.ChatID,
Text: strings.Join(usernames, " "),
},
)
}
return nil
}