🔍 Overview

send_telegram_message_task is a Celery task that sends Telegram messages via pyTelegramBotAPI (telebot).

It supports multiple bots (base/offers/tracking-domain) and retries automatically on rate limits (HTTP 429).


📝 Task Definition

@celery.task(bind=True, max_retries=10, default_retry_delay=15)
def send_telegram_message_task(
    self: Task,
    message: str,
    telegram_user_id: int,
    bot_type: str = BotType.BASE,
) -> None:
    bot = BOTS_MAP.get(bot_type, base_bot)
    ...
    bot.send_message(chat_id=telegram_user_id, text=message, ...)

📑 Attributes

NameTypeDescription
messagestrThe text to send.
telegram_user_idintTelegram chat/user ID.
bot_typestrWhich bot to use (base_bot, offers_bot, tracking_domain_bot).

🔄 Error Handling & Retries

  • HTTP 429 Rate Limit

    • reads retry_after from Telegram API response
    • retries after that delay
  • Other errors

    • retries with default_retry_delay

Celery stops retrying after max_retries attempts.


🚀 Usage Example

from apps.telegram.tasks import send_telegram_message_task
from apps.telegram.constants import BotType
 
send_telegram_message_task.delay(
    message="Hello user!",
    telegram_user_id=123456789,
    bot_type=BotType.BASE,
)