-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmongo_data_function.py
54 lines (45 loc) · 1.81 KB
/
mongo_data_function.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
from pymongo import MongoClient
import os
from dotenv import load_dotenv
load_dotenv()
MONGO_URI = os.getenv("MONGO_URI")
# Connect to MongoDB
client = MongoClient(MONGO_URI)
db = client["telegram_bot_db"]
users_collection = db["users"]
bot_token = os.getenv("bot_token")
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Handle the /start command."""
chat_id = update.effective_chat.id
first_name = update.effective_chat.first_name
username =update.effective_chat.username
# Check if user already exists in the database
user = users_collection.find_one({"chat_id": chat_id})
if user:
message = f"Welcome back, {first_name}!"
else:
# Save new user to the database
users_collection.insert_one({
"chat_id": chat_id,
"first_name": first_name,
"username": username,
"joined": update.effective_message.date
})
message = f"Hello, {first_name}! You are now registered."
await context.bot.send_message(chat_id=chat_id, text=message)
async def show_users(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Handle a command to show all registered users."""
all_users = users_collection.find()
message = "👥 Registered Users:\n"
for user in all_users:
message += f"- {user['first_name']} (ID: {user['chat_id']})\n"
await context.bot.send_message(chat_id=update.effective_chat.id, text=message)
if __name__ == "__main__":
app = ApplicationBuilder().token(bot_token).build()
# Add command handlers
app.add_handler(CommandHandler("start", start))
app.add_handler(CommandHandler("users", show_users))
print("Bot is running...")
app.run_polling()