-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_bot.py
More file actions
59 lines (48 loc) · 1.68 KB
/
create_bot.py
File metadata and controls
59 lines (48 loc) · 1.68 KB
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
55
56
57
58
59
from __future__ import annotations
import asyncio
import os
from dotenv import load_dotenv
from aiogram import Bot, Dispatcher
from loguru import logger
from bot.handlers import get_handlers_router
from bot.middlewares import register_middlewares
def configure_logger() -> None:
logger.add(
"logs/telegram_bot.log",
level="DEBUG",
format="{time:YYYY-MM-DDTHH:mm:ss.SSSZ!UTC} {level} {message}",
rotation="50 KB",
compression="zip",
)
def get_bot_token() -> str:
token = os.getenv('BOT_TOKEN')
if not token:
raise ValueError("BOT_TOKEN environment variable is not set!")
return token
async def log_bot_info(bot: Bot) -> None:
bot_info = await bot.get_me()
states = {True: "Enabled", False: "Disabled", None: "Unknown (This's not a bot)"}
logger.info(f"Name - {bot_info.full_name}")
logger.info(f"Username - @{bot_info.username}")
logger.info(f"ID - {bot_info.id}")
logger.info(f"Groups Mode - {states[bot_info.can_join_groups]}")
logger.info(f"Privacy Mode - {states[not bot_info.can_read_all_group_messages]}")
logger.info(f"Inline Mode - {states[bot_info.supports_inline_queries]}")
logger.info("Bot successfully started")
async def main() -> None:
load_dotenv()
configure_logger()
token = get_bot_token()
bot = Bot(token=token)
dp = Dispatcher()
await register_middlewares(dp)
await log_bot_info(bot)
router = get_handlers_router()
dp.include_router(router)
try:
await dp.start_polling(bot)
except Exception as e:
logger.error(f"Polling error: {e}")
await bot.close()
if __name__ == '__main__':
asyncio.run(main())