From 0d1dd4362d098acaaa119cef208f630a1f842ff2 Mon Sep 17 00:00:00 2001 From: Sergey Elpashev Date: Thu, 16 Jan 2025 16:51:26 +0300 Subject: [PATCH] Feat: fixed notifications --- main.py | 7 +------ nwxraybot/notifiers/subscription.py | 31 ++++++++++++++++++----------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/main.py b/main.py index 779875b..c445e37 100644 --- a/main.py +++ b/main.py @@ -8,10 +8,6 @@ from nwxraybot import NwXrayBot, Settings from nwxraybot.handlers import * from nwxraybot.models import User - -async def main(bot: NwXrayBot, skip_updates: bool = True) -> None: - await asyncio.create_task(bot.start()) - if __name__ == "__main__": config = Settings() # Load config from .env logging.basicConfig(level=logging.DEBUG if config.debug else logging.INFO) @@ -30,5 +26,4 @@ if __name__ == "__main__": # Start bot bot = NwXrayBot(config.bot_token.get_secret_value()) bot.include_routers(HelloHandler(), MenuHandler(), AdminHandler()) - - asyncio.run(main(bot)) + loop.run_until_complete(bot.start(skip_updates=True)) diff --git a/nwxraybot/notifiers/subscription.py b/nwxraybot/notifiers/subscription.py index 1bdc326..01d3950 100644 --- a/nwxraybot/notifiers/subscription.py +++ b/nwxraybot/notifiers/subscription.py @@ -1,6 +1,5 @@ import asyncio import datetime -import logging from aiogram import Bot from apscheduler.schedulers.asyncio import AsyncIOScheduler @@ -8,21 +7,29 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler from nwxraybot.models import User -# TODO: This shit is not working -async def send_subscription_notification(bot: Bot, telegram_id: int) -> None: - logging.debug(f"Sending subscription notification to {telegram_id}") - - async def check_subscription_status(bot: Bot): - logging.debug('Running notifier task') - await asyncio.sleep(1) - for user in User.select(): - if user.telegram_id != '': - await bot.send_message(user.telegram_id, 'Your subscription is active') + + # Check users subscription status + now_time = datetime.datetime.now() + for user in User.select().where((User.time > now_time) & ((User.time - now_time <= datetime.timedelta(days=7))) & (User.telegram_id != '')): + text = "" + delta = user.time - now_time + print(delta) + if delta.days <= 1: + text = "Скоро истекает срок действия вашей подписки. Не теряйте доступ к NwaifuVPN — продлите подписку прямо сейчас!" + elif delta.days <= 3: + text = "До окончания вашей подписки осталось менее 3-х дней. Продлите, чтобы избежать отключения." + else: + text = "Ваша подписка заканчивается через менее чем 7 дней. Продлите её, чтобы избежать отключения." + await bot.send_message(user.telegram_id, text) + + # Check non-paying users + for user in User.select().where((User.time < now_time) & (now_time - User.time < datetime.timedelta(days=1)) & (User.telegram_id != '')): + await bot.send_message(user.telegram_id, "Ваша подписка истекла. Чтобы восстановить доступ к NwaifuVPN, продлите подписку уже сегодня") def setup_subscription_notifier(bot: Bot) -> None: scheduler = AsyncIOScheduler(event_loop=asyncio.get_event_loop()) scheduler.add_job(check_subscription_status, - 'interval', seconds=10, args=[bot]) + 'cron', hour=10, minute=0, args=[bot]) scheduler.start()