Skip to main content

Prerequisites

  • Python 3.10 or newer
  • A Telegram bot token from @BotFather
  • The numeric ID of the chat where you want to send messages

Install

From a checkout of the repository:
For development, install the test and lint tools as well:
requests is installed automatically.

Create a bot

  1. Open @BotFather in Telegram.
  2. Send /newbot and follow the prompts.
  3. Copy the token BotFather returns.
Keep the token private. Anyone with the token can control the bot.

Find your chat ID

Send /my_id to @get_id_bot. It replies with your numeric user ID. You can also send a message to your bot and inspect the raw updates:
Look for message.chat.id in the response.

Send a message

The client can also be used as a context manager, which closes its HTTP session when the block exits:

Receive updates

poll_updates() yields parsed Message objects and keeps polling until you stop the loop:
Long polling is enabled by default. Telegram holds the request open for up to 30 seconds, so the loop does not need a busy wait.

Next steps