Telegram Bot PHP SDK

repository·3.x·Indexed 25 days ago

https://github.com/irazasyed/telegram-bot-sdk

A PHP SDK providing a simplified interface for interacting with the Telegram Bot API. It includes native support for the Laravel framework, including a `telegram:webhook` Artisan command for managing webhooks. The library supports synchronous and asynchronous requests via GuzzleHttpClient and provides structured objects for handling Updates, CallbackQueries, and BotCommands. Current supported version is 3.x.

Tokens
3.6K
Snippets
2
Records
20
Agent score
84%

What's inside telegram-bot-sdk

  1. Overview of Telegram Bot PHP SDK

    3.x
    The Telegram Bot PHP SDK is a library designed to simplify the development of Telegram bots using PHP. It provides an easy-to-use interface for the Telegram Bot API, which is an HTTP-based interface for building bots. The SDK features built-in support for Laravel, making it highly compatible with the Laravel ecosystem.
  2. Instantiate the Api class

    3.x

    To interact with the Telegram Bot API, create a new instance of the Telegram\Bot\Api class. You can provide a bot token directly, or the SDK will attempt to retrieve it from the environment variable defined by Api::BOT_TOKEN_ENV_NAME (TELEGRAM_BOT_TOKEN).

    Supported constructor parameters:

    • string|null $token: The Telegram Bot API Access Token.
    • bool $async: If set to true, requests to Telegram will be asynchronous (non-blocking).
    • HttpClientInterface|null $httpClientHandler: An optional custom HTTP Client implementation.
    • string|null $baseBotUrl: An optional custom base URL for the Telegram Bot API.
  3. Configure webhook settings for the Artisan command

    3.x

    The telegram:webhook command relies on the bot configuration (retrieved via BotsManager::getBotConfig()) to perform its tasks. To ensure successful setup, ensure your bot configuration includes the following keys:

    • webhook_url: (Required) The HTTPS URL where Telegram will send updates. Must start with https://.
    • certificate_path: (Optional) The file path to your SSL certificate.
    • allowed_updates: (Optional) An array of update types that the webhook should receive.
    • bot: The identifier for the bot used in the configuration.
  4. Check supported SDK versions

    3.x

    The SDK primarily supports the latest released version. While backward-incompatible changes are rare to facilitate upgrades, ensure you are using the correct version for your project requirements:

    • 3.x: Current supported version.
    • 4.x: Currently in development.
    • 2.x: No longer supported.
  5. Extract the message or related content from an Update

    3.x

    To retrieve the primary content of an update (such as a Message, InlineQuery, or CallbackQuery), use the following methods:

    • getMessage(): Returns a Collection containing the message-like object (e.g., Message, EditedMessage, ChannelPost, InlineQuery, ShippingQuery, PreCheckoutQuery, or Poll).
    • getRelatedObject(): Returns the specific object instance corresponding to the update type (e.g., Message|InlineQuery|ChosenInlineResult|CallbackQuery|ShippingQuery|PreCheckoutQuery|Poll|PollAnswer).
    • getChat(): Returns the chat object associated with the update.
  6. Handle incoming Telegram Updates with the Update object

    3.x
    The Telegram\Bot\Objects\Update class represents an incoming update from the Telegram Bot API. It acts as a container for various types of interactions, such as messages, callback queries, or poll updates. You can use it to identify the type of update received and extract the relevant data (like the message or the chat object).
  7. Send requests with GuzzleHttpClient

    3.x

    The send method allows you to execute HTTP requests. It supports both synchronous and asynchronous execution.

    Parameters:

    • string $url: The target URL.
    • string $method: The HTTP method (e.g., 'GET', 'POST').
    • array $headers: An array of HTTP headers.
    • array $options: Additional Guzzle request options (e.g., ['body' => $data]).
    • bool $isAsyncRequest: If true, the method returns a PromiseInterface instead of waiting for the response. If false, it waits and returns a ResponseInterface.

    Returns: ResponseInterface|PromiseInterface|null

  8. Inspect webhook configuration with WebhookInfo

    3.x

    The WebhookInfo object contains information about the current status of a bot's webhook configuration. You can use this object to check if a webhook is set up, see how many updates are pending, and inspect recent delivery errors.

    Properties available on the WebhookInfo object:

    • url: The Webhook URL (may be empty if no webhook is set up).
    • hasCustomCertificate: Boolean indicating if a custom certificate was provided for webhook certificate checks.
    • pendingUpdateCount: The number of updates currently awaiting delivery.
    • lastErrorDate (Optional): Unix time for the most recent error during update delivery.
    • lastErrorMessage (Optional): Human-readable error message for the most recent error.
    • maxConnections (Optional): Maximum allowed number of simultaneous HTTPS connections to the webhook.
    • allowedUpdates (Optional): A list of update types the bot is subscribed to.