-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_users.py
45 lines (36 loc) · 1.3 KB
/
list_users.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
import asyncio
import logging
import os
from dotenv import find_dotenv, load_dotenv
from telegram import Bot
from telegram.error import TelegramError
from users import UserStorage
# Load environment variables from .env file
dotenv_path = find_dotenv()
if not dotenv_path:
raise FileNotFoundError(
"The .env file is missing. Please create a .env file in the project directory with the following content:\n\nTELEGRAM_BOT_TOKEN=your-telegram-bot-token"
)
load_dotenv(dotenv_path)
# Configuration
TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
async def list_users():
bot = Bot(token=TOKEN)
print("Subscribed Users:")
user_storage = UserStorage()
users = user_storage.all()
user_count = 0
for chat_id, user in users:
try:
chat_info = await bot.get_chat(chat_id)
print(
f"Chat ID: {chat_info.id}, Username: @{chat_info.username}, First Name: {chat_info.first_name}, "
f"Last Name: {chat_info.last_name}, "
f"Street Name: {user.street_name}, Building: {user.building}"
)
user_count += 1
except TelegramError as e:
logging.error(f"Failed to get info for chat_id {chat_id}: {e}")
print(f"Total Users: {user_count}")
if __name__ == "__main__":
asyncio.run(list_users())