Telegram uses its own custom HTTP-based API protocol. The Telegram Bot API uses HTTPS and returns responses in JSON format. It is not a standard REST API, but has some REST-like characteristics in its design. For more advanced functionality, Telegram offers the MTProto API, which is a custom protocol developed by Telegram for accessing the full Telegram functionality. It uses a custom encryption scheme and data serialization format. Telegram's APIs are custom-designed rather than following standard API architectures like REST or GraphQL.
The official Telegram Bot API supports webhooks.
Webhooks are a push mechanism where Telegram sends updates to your bot by making HTTPS POST requests to a specified URL. This eliminates the need for constant polling.
To set up a webhook:
Webhook requirements:
Webhooks can receive all types of updates that a bot normally receives, including new messages, edited messages, callback queries, and inline queries. You can specify which update types to receive when setting the webhook.
Advantages of webhooks:
Key considerations:
In summary, the Telegram API supports webhooks, offering a push-based mechanism for receiving bot updates. While more efficient than polling, it requires proper server setup with HTTPS support.
Here are the key points about the API rate limits for the Telegram Bot API:
When sending messages to a particular chat, avoid sending more than 1 message per second. Short bursts may be allowed, but eventually you'll get 429 errors if you exceed this limit consistently.
For bulk notifications to multiple users, the API allows up to 30 messages per second overall.
Bots can send up to 20 messages per minute to the same group.
Spread out bulk notifications over longer intervals of 8-12 hours for best results.
The API will not allow bulk notifications to more than ~30 users per second. Exceeding this will result in 429 errors.
Rate limits are applied per bot account, not per host/server.
You can create multiple bot accounts to avoid hitting rate limits if needed.
If you hit rate limits, you'll receive 429 errors.
Consider implementing retry logic with exponential backoff when receiving 429 errors.
Use queuing mechanisms to manage requests and stay within limits.
Implement proper rate limiting on your end to avoid hitting Telegram's limits.
Use webhooks instead of long polling for more efficient updates.
Spread out requests over time rather than sending in bursts.
Monitor your bot's usage and adjust sending patterns if needed.
By following these limits and best practices, you can ensure your Telegram bot operates smoothly within the API's constraints. Remember to always refer to the official Telegram Bot API documentation for the most up-to-date information on rate limits.
The most recent version of the Telegram Bot API is 7.1, released on May 28, 2024.
Key points to consider:
The Telegram Bot API is regularly updated with new features and improvements.
The latest update (version 7.1) introduced several new features, including:
Telegram provides a changelog that details the changes and additions in each version.
Developers should regularly check for updates to ensure they are using the latest features and following best practices.
The Bot API is separate from the main Telegram API, which is used for building custom Telegram clients.
Best practices:
Always refer to the official Telegram Bot API documentation for the most up-to-date information.
Keep your bot implementation updated to take advantage of new features and improvements.
Test your bot thoroughly when updating to a new API version to ensure compatibility and proper functionality.
Subscribe to Telegram's official channels or follow their blog to stay informed about new releases and changes.
If you don't already have one, download the Telegram app on your mobile device and create a Telegram account.
After creating the application, you'll see an "App configuration" screen that contains two important pieces of information:
These are your API keys that you'll need to use the Telegram API.
Here are the main data models you can interact with using the Telegram API, along with what is possible for each:
The Telegram API provides extensive capabilities to interact with these data models for building bots and applications. The exact methods available may vary slightly between the Bot API and other client APIs like TDLib.