Telegram Bot API Server

repository·master·Indexed 26 days ago

https://github.com/tdlib/telegram-bot-api

A local implementation of the Telegram Bot API that allows developers to host their own bot server. It provides enhanced capabilities over the standard hosted API, including local mode for unlimited file download sizes, uploads up to 2000 MB via local paths, and flexible webhook configurations. The server is built with C++17 and provides the telegram_bot_api::Client class for managing queries, updates, and bot interactions.

Tokens
3.2K
Snippets
5
Records
23
Agent score
38%

What's inside tdlib/telegram-bot-api

  1. Move a bot between local servers without losing updates

    master

    To migrate a bot from one local server to another while minimizing update loss:

    1. Call deleteWebhook to remove the bot's webhook.
    2. Call close to close the bot instance on the old server.
    3. Locate the bot's subdirectory in the old server's working directory (named by the bot's user ID).
    4. Move that subdirectory to the working directory of the new server.
    5. Resume sending requests to the new server.
  2. Install the Telegram Bot API server

    master

    To build and install the Telegram Bot API server, you can use the Telegram Bot API server build instructions generator for your specific operating system.

    Alternatively, you can manually compile the source code using CMake following these steps:

    1. Clone the repository recursively.
    2. Create a build directory.
    3. Run CMake to configure the build type as Release.
    4. Build and install the target.
    git clone --recursive https://github.com/tdlib/telegram-bot-api.git
    cd telegram-bot-api
    mkdir build
    cd build
    cmake -DCMAKE_BUILD_TYPE=Release ..
    cmake --build . --target install
  3. Run the Telegram Bot API server

    master

    The Telegram Bot API server requires two mandatory credentials obtained from Telegram: api_id and api_hash. These can be provided via command-line options or environment variables.

    Mandatory Options/Environment Variables:

    • --api-id or TELEGRAM_API_ID
    • --api-hash or TELEGRAM_API_HASH

    Common Configuration:

    • --http-port: Changes the default port (default is 8081).
    • --local: Enables local mode to unlock advanced features (see below).
    • --help: Displays all available options.

    Note: The server only accepts HTTP requests. To handle remote HTTPS requests, you must use a TLS termination proxy.

  4. Move a bot to a local server

    master

    To ensure your bot receives all updates when switching from the official Telegram servers to your local server:

    1. Call the logOut method to deregister the bot from https://api.telegram.org.
    2. Redirect your bot's request address to your local server.
    3. If using --local mode, ensure your bot is configured to handle absolute file paths returned by getFile requests.
  5. Use Local Mode for advanced Bot API features

    master

    By specifying the --local option, the Telegram Bot API server enables features not available on the standard https://api.telegram.org endpoint:

    • File Handling:
      • Download files without a size limit.
      • Upload files up to 2000 MB.
      • Upload files using local paths and the file URI scheme.
      • Receive absolute local paths in the file_path field of a getFile response (avoiding the need to download the file).
    • Webhooks:
      • Use an HTTP URL for the webhook.
      • Use any local IP address for the webhook.
      • Use any port for the webhook.
      • Set max_webhook_connections up to 100,000.
  6. Dependencies for building and running the Telegram Bot API server

    master

    The following dependencies are required:

    Runtime Dependencies:

    • OpenSSL
    • zlib

    Build-time Dependencies:

    • C++17 compatible compiler (e.g., Clang 5.0+, GCC 7.0+, MSVC 19.1+, Intel C++ Compiler 19+)
    • gperf
    • CMake (3.10+)
  7. Retrieve MessageInfo for a message

    master

    Use get_message(int64 chat_id, int64 message_id, bool force_cache) to retrieve cached information about a specific message.

    MessageInfo contains:

    • id, chat_id, sender_user_id, sender_chat_id.
    • date, edit_date.
    • content: object_ptr<td_api::MessageContent>.
    • reply_to_message: object_ptr<td_api::messageReplyToMessage>.
    • reply_markup: object_ptr<td_api::ReplyMarkup>.
    • is_outgoing, is_self_destruct, is_scheduled, is_paid_post.
    • topic_id: object_ptr<td_api::MessageTopic>.
  8. Process Telegram Bot API queries

    master

    The Client class provides a large set of process_*_query methods to handle various Telegram Bot API requests. These methods take a PromisedQueryPtr &query as an argument and return a td::Status.

    Common query types include:

    • Bot Identity: process_get_me_query, process_get_my_name_query, process_get_my_commands_query.
    • Messaging: process_send_message_query, process_send_photo_query, process_send_video_query, process_send_audio_query, process_send_document_query, process_send_voice_query, process_send_sticker_query, process_send_animation_query, process_send_live_photo_query, process_send_video_note_query, process_send_paid_media_query, process_send_game_query, process_send_invoice_query, process_send_location_query, process_send_venue_query, process_send_contact_query, process_send_poll_query, process_send_checklist_query.
    • Message Management: process_edit_message_text_query, process_edit_message_media_query, process_edit_message_caption_query, process_edit_message_reply_markup_query, process_delete_message_query, process_delete_messages_query, process_copy_message_query, process_forward_message_query.
    • Chat/Group Management: process_set_chat_title_query, process_set_chat_description_query, process_set_chat_permissions_query, process_promote_chat_member_query, process_ban_chat_member_query, process_unban_chat_member_query, process_restrict_chat_member_query.
    • Webhook/Updates: process_get_updates_query, process_set_webhook_query, process_get_webhook_info_query.
    • Business/Premium: process_get_business_connection_query, process_get_my_star_balance_query, process_get_available_gifts_query.
  9. Manage Webhook configuration

    master

    The Client interface provides methods to manage the bot's webhook settings:

    • do_set_webhook(PromisedQueryPtr query, bool was_deleted): Initiates setting or deleting a webhook.
    • get_webhook_certificate(const Query *query): Returns a pointer to the td::HttpFile containing the webhook certificate.
    • get_webhook_max_connections(const Query *query): Returns the maximum number of connections.
    • get_webhook_certificate_path(): Returns the path to the webhook certificate.
    • save_webhook(): Persists the current webhook configuration.

    Webhook lifecycle callbacks include webhook_verified, webhook_success, webhook_error, and webhook_closed.

  10. Use the Client class for Telegram Bot API interactions

    master

    The telegram_bot_api::Client class is the primary interface for interacting with the Telegram Bot API server. It manages queries, handles updates, and provides utility methods for extracting data from incoming queries.

    Key capabilities include:

    • Sending queries via send(PromisedQueryPtr query).
    • Retrieving bot information via get_bot_info().
    • Handling asynchronous results through various on_... callback methods.
    • Extracting specific arguments (like user_id, message_id, or chat_id) from a Query object using static helper methods.
  11. Retrieve ChatInfo for a chat

    master

    Use get_chat(int64 chat_id) to retrieve cached information about a chat.

    ChatInfo contains:

    • type: Private, Group, Supergroup, or Unknown.
    • title.
    • message_auto_delete_time.
    • has_protected_content.
    • permissions: object_ptr<td_api::chatPermissions>.
    • photo_info: object_ptr<td_api::chatPhotoInfo>.
    • A union containing user_id, group_id, or supergroup_id depending on the type.
  12. Extract integer arguments from a Query

    master

    Use get_integer_arg to retrieve an integer value from a specific field in a Query, with optional bounds checking.

    static int32 get_integer_arg(const Query *query, td::Slice field_name, int32 default_value, 
                                   int32 min_value = std::numeric_limits<int32>::min(), 
                                   int32 max_value = std::numeric_limits<int32>::max());