I am trying to get my telegram bot to send messages at a certain time but it doesn't work. The bot launches fine, everything works as intended except for the scheduled messages. I suspect the line await asyncio.create_task(scheduler())
never runs but I don't know why or how to fix it/get it to work. Any help is appreciated.
import asyncio
import logging
import aioschedule as schedule
from aiogram import Bot, Dispatcher
from aiogram.types import BotCommand
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from app.config_reader import load_config
from app.handlers.feedback import register_handlers_feedback
from app.handlers.food import register_handlers_food
from app.handlers.common import register_handlers_common
from app.handlers.database import register_handlers_db
from app.handlers.mailing import mail_one, mail_two, mail_three
logger = logging.getLogger(__name__)
async def set_commands(bot: Bot):
commands = [
BotCommand(command="/feedback", description="Take a poll"),
BotCommand(command="/db", description="Add your info to our database"),
BotCommand(command="/cancel", description="Cancel")
]
await bot.set_my_commands(commands)
async def main():
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
)
logger.error("Starting bot")
# Parsing the config file
config = load_config("config/bot.ini")
# Instantiating Bot and Dispatcher objects
bot = Bot(token=config.tg_bot.token)
dp = Dispatcher(bot, storage=MemoryStorage())
async def scheduler():
# this line is here temporarily for debugging/checking if it works
schedule.every(5).seconds.do(lambda: mail_one(bot))
schedule.every().day.at('10:00').do(lambda: mail_one(bot))
schedule.every().day.at('10:00').do(lambda: mail_two(bot))
schedule.every().day.at('10:00').do(lambda: mail_three(bot))
while True:
await schedule.run_pending()
await asyncio.sleep(1)
await set_commands(bot)
# Registering handlers
register_handlers_common(dp, config.tg_bot.admin_id)
register_handlers_feedback(dp)
register_handlers_db(dp)
# optional: await dp.skip_updates()
await dp.start_polling()
await asyncio.create_task(scheduler())
if __name__ == '__main__':
asyncio.run(main())```