-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
29 lines (25 loc) · 997 Bytes
/
Copy pathdatabase.py
File metadata and controls
29 lines (25 loc) · 997 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os
import pymongo
from dotenv import load_dotenv
from datetime import datetime
# Load environment variables
load_dotenv()
# Connect to MongoDB
client = pymongo.MongoClient(os.getenv("MONGO_URI"))
db = client["telegram_bot"]
users_collection = db["users"]
chat_collection = db["chats"]
files_collection = db["files"]
# Function to add a new user
def add_user(chat_id, first_name, username, phone_number=None):
user = {"chat_id": chat_id, "first_name": first_name, "username": username, "phone": phone_number}
users_collection.update_one({"chat_id": chat_id}, {"$set": user}, upsert=True)
# Function to store chat messages with timestamp
def save_chat(chat_id, user_message, bot_response):
timestamp = datetime.now() # Capture current timestamp
chat_collection.insert_one({
"chat_id": chat_id,
"user": user_message,
"bot": bot_response,
"timestamp": timestamp # Add timestamp to the stored document
})