Feat: fixed notifications
This commit is contained in:
7
main.py
7
main.py
@@ -8,10 +8,6 @@ from nwxraybot import NwXrayBot, Settings
|
|||||||
from nwxraybot.handlers import *
|
from nwxraybot.handlers import *
|
||||||
from nwxraybot.models import User
|
from nwxraybot.models import User
|
||||||
|
|
||||||
|
|
||||||
async def main(bot: NwXrayBot, skip_updates: bool = True) -> None:
|
|
||||||
await asyncio.create_task(bot.start())
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
config = Settings() # Load config from .env
|
config = Settings() # Load config from .env
|
||||||
logging.basicConfig(level=logging.DEBUG if config.debug else logging.INFO)
|
logging.basicConfig(level=logging.DEBUG if config.debug else logging.INFO)
|
||||||
@@ -30,5 +26,4 @@ if __name__ == "__main__":
|
|||||||
# Start bot
|
# Start bot
|
||||||
bot = NwXrayBot(config.bot_token.get_secret_value())
|
bot = NwXrayBot(config.bot_token.get_secret_value())
|
||||||
bot.include_routers(HelloHandler(), MenuHandler(), AdminHandler())
|
bot.include_routers(HelloHandler(), MenuHandler(), AdminHandler())
|
||||||
|
loop.run_until_complete(bot.start(skip_updates=True))
|
||||||
asyncio.run(main(bot))
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import datetime
|
import datetime
|
||||||
import logging
|
|
||||||
|
|
||||||
from aiogram import Bot
|
from aiogram import Bot
|
||||||
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||||
@@ -8,21 +7,29 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
|||||||
from nwxraybot.models import User
|
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):
|
async def check_subscription_status(bot: Bot):
|
||||||
logging.debug('Running notifier task')
|
|
||||||
await asyncio.sleep(1)
|
# Check users subscription status
|
||||||
for user in User.select():
|
now_time = datetime.datetime.now()
|
||||||
if user.telegram_id != '':
|
for user in User.select().where((User.time > now_time) & ((User.time - now_time <= datetime.timedelta(days=7))) & (User.telegram_id != '')):
|
||||||
await bot.send_message(user.telegram_id, 'Your subscription is active')
|
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:
|
def setup_subscription_notifier(bot: Bot) -> None:
|
||||||
scheduler = AsyncIOScheduler(event_loop=asyncio.get_event_loop())
|
scheduler = AsyncIOScheduler(event_loop=asyncio.get_event_loop())
|
||||||
scheduler.add_job(check_subscription_status,
|
scheduler.add_job(check_subscription_status,
|
||||||
'interval', seconds=10, args=[bot])
|
'cron', hour=10, minute=0, args=[bot])
|
||||||
scheduler.start()
|
scheduler.start()
|
||||||
|
|||||||
Reference in New Issue
Block a user