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

58 lines
1.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package bot_all
import (
"context"
"fmt"
"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") && !strings.Contains(msg.Text, "@все") && strings.ToLower(msg.Text) != "ау" {
return nil
}
users, err := bot.storage.GetAllUsersByChatID(ctx, msg.ChatID)
if err != nil {
return err
}
usernames := lo.FilterMap(users, func(item *storage.User, _ int) (string, bool) {
if item.UserID == msg.UserID {
return "", false
}
return fmt.Sprintf("@%s", item.UserID), true
})
if len(usernames) > 0 {
bot.messenger.SendMessage(
ctx,
&messenger.Message{
ChatID: msg.ChatID,
Text: strings.Join(usernames, " "),
},
)
}
return nil
}