add new all command

This commit is contained in:
Владимир Фёдоров 2022-04-25 03:07:52 +07:00
parent 42f73f84fd
commit 3e0bfc433b
2 changed files with 29 additions and 9 deletions

View File

@ -10,7 +10,8 @@ import (
) )
type chatDTO struct { type chatDTO struct {
ChatID int64 `bson:"chat_id"` ChatID int64 `bson:"chat_id"`
Users []string `bson:"users"`
} }
var cfg *config.Config var cfg *config.Config
@ -41,6 +42,10 @@ func AddChat(chatID int64) error {
} }
func AddUserInChat(chatID int64, username string) error { func AddUserInChat(chatID int64, username string) error {
if username == "" {
return nil
}
if err := AddChat(chatID); err != nil { if err := AddChat(chatID); err != nil {
return err return err
} }
@ -61,3 +66,20 @@ func AddUserInChat(chatID int64, username string) error {
) )
return err return err
} }
func GetUsers(chatID int64) ([]string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL))
if err != nil {
return nil, err
}
collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName)
chatDTO := &chatDTO{}
if err := collection.FindOne(ctx, bson.M{"chat_id": chatID}).Decode(chatDTO); err != nil {
return nil, err
}
return chatDTO.Users, err
}

14
main.go
View File

@ -13,10 +13,7 @@ import (
"strings" "strings"
) )
const ( const tag = "@all"
tag = "@all"
allUsers = "@vo13crabs @disconecto @v9gel @KeyM_v2 @k_ukolov @iamembarrassed @freel_Fedorov_Kirill @butinmv"
)
type Opts struct { type Opts struct {
Token string `short:"t" long:"token" description:"Telegram api token"` Token string `short:"t" long:"token" description:"Telegram api token"`
@ -82,21 +79,22 @@ func run() {
chatID := update.Message.Chat.ID chatID := update.Message.Chat.ID
username := update.Message.From.UserName username := update.Message.From.UserName
_ = db.AddUserInChat(chatID, username)
if strings.Contains(strings.ToLower(text), tag) { if strings.Contains(strings.ToLower(text), tag) {
_, _ = bot.Send(tgbot.NewMessage(chatID, allUsers)) users, _ := db.GetUsers(chatID)
_, _ = bot.Send(tgbot.NewMessage(chatID, "@"+strings.Join(users, " @")))
} }
command := commands.Command(strings.Replace(text, opts.Name, "", 1)) command := commands.Command(strings.Replace(text, opts.Name, "", 1))
switch command { switch command {
case commands.Start: case commands.Start:
_, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Здравствуйте, я Волтер (v1.1.0), ваш дворецкий (%d).", chatID))) _, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Здравствуйте, я Волтер (v1.2.0), ваш дворецкий (%d).", chatID)))
_ = db.AddChat(chatID) _ = db.AddChat(chatID)
case commands.Help: case commands.Help:
_, _ = bot.Send(tgbot.NewMessage(chatID, "Вот что я умею:\n\n1) Позвать всех")) _, _ = bot.Send(tgbot.NewMessage(chatID, "Вот что я умею:\n\n1) Позвать всех"))
case commands.Ping: case commands.Ping:
_, _ = bot.Send(tgbot.NewMessage(chatID, "pong")) _, _ = bot.Send(tgbot.NewMessage(chatID, "pong"))
} }
_ = db.AddUserInChat(chatID, username)
} }
} }