Skip to content

Await Telegram local command responses#18

Merged
BukeLy merged 5 commits intomainfrom
copilot/fix-async-methods-in-handler
Jan 11, 2026
Merged

Await Telegram local command responses#18
BukeLy merged 5 commits intomainfrom
copilot/fix-async-methods-in-handler

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 11, 2026

Local/unknown Telegram commands weren’t sent because send_message (async in python-telegram-bot v22) was invoked without awaiting.

  • Async handling: _handle_local_command now uses asyncio.run to execute bot.send_message to completion, ensuring replies are dispatched before Lambda returns.
  • Error resilience: Existing logging remains to surface send failures.

Example:

asyncio.run(
    bot.send_message(
        chat_id=message.chat_id,
        text=text,
        message_thread_id=message.message_thread_id,
        reply_to_message_id=message.message_id,
    )
)
Original prompt

This section details on the original issue you should resolve

<issue_title>Bug: python-telegram-bot v22.x async methods called without await in handler.py</issue_title>
<issue_description>## Description

In agent-sdk-client/handler.py, the _handle_local_command function calls bot.send_message() synchronously, but python-telegram-bot v22.x is an async library. The send_message() method returns a coroutine that is never awaited, causing messages to not be sent.

Code Location

agent-sdk-client/handler.py lines 130-136:

def _handle_local_command(bot: Bot, message, config: Config, cmd: str) -> bool:
    ...
    try:
        bot.send_message(  # Returns coroutine, never awaited!
            chat_id=message.chat_id,
            text=text,
            message_thread_id=message.message_thread_id,
            reply_to_message_id=message.message_id,
        )
    except Exception:
        logger.warning("Failed to send local command response", exc_info=True)

Steps to Reproduce

  1. Configure config.toml with a local command (e.g., /help)
  2. Deploy the Lambda
  3. Send /help to the Telegram bot
  4. No response is received

Expected Behavior

Bot should respond with the configured local command response (e.g., "Hello World").

Suggested Fix

Use asyncio.run() or asyncio.get_event_loop().run_until_complete() to properly execute the async method:

import asyncio

def _handle_local_command(bot: Bot, message, config: Config, cmd: str) -> bool:
    ...
    try:
        asyncio.run(
            bot.send_message(
                chat_id=message.chat_id,
                text=text,
                message_thread_id=message.message_thread_id,
                reply_to_message_id=message.message_id,
            )
        )
    except Exception:
        logger.warning("Failed to send local command response", exc_info=True)

Environment

  • python-telegram-bot: 22.x
  • Runtime: AWS Lambda Python 3.12</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits January 11, 2026 09:08
Co-authored-by: BukeLy <19304666+BukeLy@users.noreply.github.com>
Co-authored-by: BukeLy <19304666+BukeLy@users.noreply.github.com>
Co-authored-by: BukeLy <19304666+BukeLy@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix async methods called without await in handler Await Telegram local command responses Jan 11, 2026
Copilot AI requested a review from BukeLy January 11, 2026 09:13
@BukeLy BukeLy marked this pull request as ready for review January 11, 2026 09:17
Copilot AI review requested due to automatic review settings January 11, 2026 09:17
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where Telegram local and unknown command responses were not being sent because bot.send_message() (an async method in python-telegram-bot v21+) was called without awaiting.

Changes:

  • Added asyncio import to handler.py
  • Modified _handle_local_command to properly await async bot.send_message() calls by creating a dedicated event loop

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@BukeLy
Copy link
Copy Markdown
Owner

BukeLy commented Jan 11, 2026

@copilot apply changes based on the comments in this thread

Co-authored-by: BukeLy <19304666+BukeLy@users.noreply.github.com>
Copilot AI requested a review from BukeLy January 11, 2026 09:23
@BukeLy BukeLy merged commit ae87300 into main Jan 11, 2026
@BukeLy BukeLy deleted the copilot/fix-async-methods-in-handler branch January 11, 2026 09:27
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.

Bug: python-telegram-bot v22.x async methods called without await in handler.py

3 participants