/
rnj
/
SuaiTestBot
Обзор
Документация
Войти
/
rnj
/
SuaiTestBot
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
main.py
63 строки
2 KB
Dmitriy Pozdeev
init commit
03 июн 2026, 23:58
03 июн 2026, 23:58
110bad2
Код
Авторство
О чём код?
import asyncio import os from datetime import datetime, timedelta from dotenv import load_dotenv from bot.configuration import tg_bot import bot.handlers # noqa: F401 from bot.handlers.schedule import send_schedule_today from database import crud from database.init_database import init_database load_dotenv() def _seconds_until_broadcast(time_str: str) -> float: hour, minute = map(int, time_str.split(":")) now = datetime.now() target = now.replace(hour=hour, minute=minute, second=0, microsecond=0) if target <= now: target += timedelta(days=1) return (target - now).total_seconds() async def broadcast_loop(): time_str = os.getenv("BROADCAST_TIME", "08:00") while True: await asyncio.sleep(_seconds_until_broadcast(time_str)) for user in crud.get_subscribed_users(): if not user.group_id: continue try: await send_schedule_today(user.telegram_id, user.telegram_id) except Exception: pass await asyncio.sleep(60) async def main(): init_database() asyncio.create_task(broadcast_loop()) try: me = await asyncio.wait_for(tg_bot.get_me(), timeout=20) print(f"Бот запущен: @{me.username}") except asyncio.TimeoutError: print( "Не удалось подключиться к Telegram API (таймаут).\n" "Проверьте интернет, VPN или TELEGRAM_PROXY в .env" ) await tg_bot.close_session() return except Exception as exc: print(f"Ошибка подключения к Telegram API: {exc}") await tg_bot.close_session() return await tg_bot.infinity_polling(request_timeout=90) if __name__ == "__main__": try: asyncio.run(main()) except KeyboardInterrupt: pass