add AddUserInChat
This commit is contained in:
		
							parent
							
								
									6ccd5b478e
								
							
						
					
					
						commit
						3005afda25
					
				
							
								
								
									
										22
									
								
								db/db.go
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								db/db.go
									
									
									
									
									
								
							@ -39,3 +39,25 @@ func AddChat(chatID int64) error {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	return result.Err()
 | 
						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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										3
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								main.go
									
									
									
									
									
								
							@ -80,6 +80,9 @@ func run() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		text := update.Message.Text
 | 
							text := update.Message.Text
 | 
				
			||||||
		chatID := update.Message.Chat.ID
 | 
							chatID := update.Message.Chat.ID
 | 
				
			||||||
 | 
							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))
 | 
								_, _ = bot.Send(tgbot.NewMessage(chatID, allUsers))
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user