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

58 lines
1.3 KiB
Go
Raw Permalink Normal View History

2023-08-13 13:22:15 +00:00
package bot_all
import (
"context"
2023-08-13 17:17:42 +00:00
"fmt"
2023-08-13 13:22:15 +00:00
"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,
}
}
2023-08-13 14:13:17 +00:00
func (bot *botAll) Process(ctx context.Context, msg *messenger.Message) error {
2023-08-13 17:17:42 +00:00
if err := bot.storage.UpsertUser(ctx, &storage.User{ChatID: msg.ChatID, UserID: msg.UserID}); err != nil {
2023-08-13 13:22:15 +00:00
return err
}
2023-08-13 17:17:42 +00:00
if !strings.Contains(msg.Text, "@all") && !strings.Contains(msg.Text, "@все") && strings.ToLower(msg.Text) != "ау" {
2023-08-13 13:22:15 +00:00
return nil
}
users, err := bot.storage.GetAllUsersByChatID(ctx, msg.ChatID)
if err != nil {
return err
}
2023-08-13 17:17:42 +00:00
usernames := lo.FilterMap(users, func(item *storage.User, _ int) (string, bool) {
2023-08-13 13:39:36 +00:00
if item.UserID == msg.UserID {
return "", false
}
2023-08-13 17:17:42 +00:00
return fmt.Sprintf("@%s", 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
}