Skip to content

Add retry logic with exponential backoff for Telegram network exceptions#67

Draft
Copilot wants to merge 4 commits into
masterfrom
copilot/fix-telegram-network-exception
Draft

Add retry logic with exponential backoff for Telegram network exceptions#67
Copilot wants to merge 4 commits into
masterfrom
copilot/fix-telegram-network-exception

Conversation

Copilot AI commented Jan 12, 2026

Copy link
Copy Markdown

The bot crashes on Telegram network exceptions without recovery. Added automatic retry with exponential backoff for all network operations.

Changes

Retry Decorator (services/utils.py)

  • Created async_retry() decorator: 3 attempts, 1s→2s→4s exponential backoff
  • Configurable exception types and timing parameters
  • Logs retry attempts via logging module

HTTP API Calls (services/api.py)

  • Applied retry to all 28 API functions
  • Catches NetworkError, TimeoutException, HTTPError
  • Added explicit 30s timeout to all AsyncClient instances
  • Created retry-wrapped bot helpers: _retry_bot_send_message(), _retry_bot_edit_message_text()

Bot Polling (main.py)

  • Automatic restart on TelegramNetworkError
  • Retry initialization (webhook deletion, command setup)
  • Infinite recovery loop with 5s delay between restart attempts

Notification Handlers (handlers/participant/notification.py)

  • Replaced direct bot calls with retry-wrapped versions

Example

@async_retry(max_attempts=3, delay=1.0, backoff=2.0, 
             exceptions=(NetworkError, TimeoutException, HTTPError))
async def get_all_events(count, chat_id):
    async with AsyncClient(timeout=30.0) as client:
        response = await client.get(url, headers=headers)
        return response.json() if response.status_code == 200 else None

Network errors now trigger automatic retry (1s, 2s, 4s delays) instead of crashing. Bot polling restarts automatically on Telegram API failures.

Original prompt

i usually get telegram network exception in bot. fix it. i suppose you should add retries


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits January 12, 2026 20:07
Co-authored-by: vl336 <44336551+vl336@users.noreply.github.com>
…tions for bot API calls

Co-authored-by: vl336 <44336551+vl336@users.noreply.github.com>
…e code, add comment for imports

Co-authored-by: vl336 <44336551+vl336@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix Telegram network exception in bot with retries Add retry logic with exponential backoff for Telegram network exceptions Jan 12, 2026
Copilot AI requested a review from vl336 January 12, 2026 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants