Feat: started working on the sub notification
This commit is contained in:
6
main.py
6
main.py
@@ -8,6 +8,10 @@ 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)
|
||||
@@ -27,4 +31,4 @@ if __name__ == "__main__":
|
||||
bot = NwXrayBot(config.bot_token.get_secret_value())
|
||||
bot.include_routers(HelloHandler(), MenuHandler(), AdminHandler())
|
||||
|
||||
asyncio.run(bot.start(skip_updates=True))
|
||||
asyncio.run(main(bot))
|
||||
|
||||
@@ -2,6 +2,7 @@ from aiogram import Bot, Dispatcher, Router
|
||||
from aiogram.utils.callback_answer import CallbackAnswerMiddleware
|
||||
|
||||
from nwxraybot.middlewares import UserMiddleware
|
||||
from nwxraybot.notifiers import setup_subscription_notifier
|
||||
|
||||
from .meta import Handler
|
||||
|
||||
@@ -12,6 +13,7 @@ class NwXrayBot:
|
||||
self.dp = Dispatcher()
|
||||
self.dp.message.middleware(UserMiddleware())
|
||||
self.dp.message.middleware(CallbackAnswerMiddleware())
|
||||
setup_subscription_notifier(self.bot)
|
||||
|
||||
def include_routers(self, *routers: Handler):
|
||||
for router in routers:
|
||||
|
||||
1
nwxraybot/notifiers/__init__.py
Normal file
1
nwxraybot/notifiers/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from nwxraybot.notifiers.subscription import setup_subscription_notifier
|
||||
28
nwxraybot/notifiers/subscription.py
Normal file
28
nwxraybot/notifiers/subscription.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import asyncio
|
||||
import datetime
|
||||
import logging
|
||||
|
||||
from aiogram import Bot
|
||||
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')
|
||||
|
||||
|
||||
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])
|
||||
scheduler.start()
|
||||
Reference in New Issue
Block a user