Перейти к содержимому
Все статьи
Telegram Bots

Напоминания по расписанию в aiogram 3 с APScheduler

Паттерн, который я использую в продакшен-ботах для надёжной отправки напоминаний об оплате — без блокировки event loop.

Temurbek Murotov 1 мин чтения

Эта статья пока доступна только на английском языке.

Reminder bots are one of the most requested Telegram projects: payment reminders, appointment reminders, subscription renewals. Here's the approach I used in the Kindergarten Payment Bot.

The setup

from apscheduler.schedulers.asyncio import AsyncIOScheduler
from aiogram import Bot, Dispatcher

bot = Bot(token=settings.BOT_TOKEN)
dp = Dispatcher()
scheduler = AsyncIOScheduler(timezone="Asia/Tashkent")

AsyncIOScheduler runs jobs inside the same event loop as aiogram, so jobs can await bot methods directly.

The job

async def send_payment_reminders():
    debtors = await repo.get_parents_with_debt()
    for parent in debtors:
        try:
            await bot.send_message(
                parent.telegram_id,
                f"Hello! The fee for {parent.child_name} is due. "
                f"Amount: {parent.debt:,} UZS",
            )
        except TelegramForbiddenError:
            await repo.mark_blocked(parent.id)
        await asyncio.sleep(0.05)  # stay under Telegram limits

Scheduling

scheduler.add_job(send_payment_reminders, "cron", day=1, hour=10)
scheduler.add_job(send_payment_reminders, "cron", day=10, hour=10)

async def main():
    scheduler.start()
    await dp.start_polling(bot)

Lessons learned

  1. Handle blocked users. People block bots — catch TelegramForbiddenError and stop messaging them.
  2. Respect rate limits. Telegram allows ~30 messages per second; a small sleep keeps you safe.
  3. Use the right timezone. Always set it explicitly on the scheduler.
  4. Make jobs idempotent. Store when a reminder was sent so a restart never sends duplicates.
  • #aiogram
  • #telegram
  • #python
  • #asyncio
Поделиться:

Похожие статьи

Есть идея проекта?

Давайте превратим её в надёжный продукт. Первая консультация и оценка — бесплатно.