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

54 lines
1.1 KiB
Go

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
}
usernames := lo.Map(users, func(item storage.User, _ int) string {
return item.UserID
})
if len(usernames) > 0 {
bot.messenger.SendMessage(
ctx,
&messenger.Message{
ChatID: msg.ChatID,
Text: strings.Join(usernames, " "),
},
)
}
return nil
}