From 3e0bfc433b126b26f8a337e5aa049a57ff9de099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D0=BC=D0=B8=D1=80=20=D0=A4?= =?UTF-8?q?=D0=B5=D0=B4=D0=BE=D1=80=D0=BE=D0=B2?= Date: Mon, 25 Apr 2022 03:07:52 +0700 Subject: [PATCH] add new all command --- db/db.go | 24 +++++++++++++++++++++++- main.go | 14 ++++++-------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/db/db.go b/db/db.go index 66c2ac5..f351a70 100644 --- a/db/db.go +++ b/db/db.go @@ -10,7 +10,8 @@ import ( ) type chatDTO struct { - ChatID int64 `bson:"chat_id"` + ChatID int64 `bson:"chat_id"` + Users []string `bson:"users"` } var cfg *config.Config @@ -41,6 +42,10 @@ func AddChat(chatID int64) error { } func AddUserInChat(chatID int64, username string) error { + if username == "" { + return nil + } + if err := AddChat(chatID); err != nil { return err } @@ -61,3 +66,20 @@ func AddUserInChat(chatID int64, username string) error { ) 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 +} diff --git a/main.go b/main.go index 469b960..6215709 100644 --- a/main.go +++ b/main.go @@ -13,10 +13,7 @@ import ( "strings" ) -const ( - tag = "@all" - allUsers = "@vo13crabs @disconecto @v9gel @KeyM_v2 @k_ukolov @iamembarrassed @freel_Fedorov_Kirill @butinmv" -) +const tag = "@all" type Opts struct { Token string `short:"t" long:"token" description:"Telegram api token"` @@ -82,21 +79,22 @@ func run() { chatID := update.Message.Chat.ID username := update.Message.From.UserName + _ = db.AddUserInChat(chatID, username) + 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)) switch command { 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) case commands.Help: _, _ = bot.Send(tgbot.NewMessage(chatID, "Вот что я умею:\n\n1) Позвать всех")) case commands.Ping: _, _ = bot.Send(tgbot.NewMessage(chatID, "pong")) } - - _ = db.AddUserInChat(chatID, username) } }