From 3005afda256884b733740432d97cbf1d51593ea1 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 02:54:43 +0700 Subject: [PATCH] add AddUserInChat --- db/db.go | 22 ++++++++++++++++++++++ main.go | 3 +++ 2 files changed, 25 insertions(+) diff --git a/db/db.go b/db/db.go index d2d5800..3879cd7 100644 --- a/db/db.go +++ b/db/db.go @@ -39,3 +39,25 @@ func AddChat(chatID int64) error { } return result.Err() } + +func AddUserInChat(chatID int64, username string) error { + if err := AddChat(chatID); err != nil { + return err + } + + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + + client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL)) + if err != nil { + return err + } + + collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName) + _, err = collection.UpdateOne( + ctx, + bson.E{Key: "chat_id", Value: chatID}, + bson.M{"addToSet": bson.M{"users": username}}, + ) + return err +} diff --git a/main.go b/main.go index ffa225a..5e1bbc8 100644 --- a/main.go +++ b/main.go @@ -80,6 +80,9 @@ func run() { text := update.Message.Text 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))