Skip to content

Add extension hooks to TelegramBotHandler for URL and curl headers#2029

Open
EgorGruzdev wants to merge 2 commits intoSeldaek:mainfrom
EgorGruzdev:main
Open

Add extension hooks to TelegramBotHandler for URL and curl headers#2029
EgorGruzdev wants to merge 2 commits intoSeldaek:mainfrom
EgorGruzdev:main

Conversation

@EgorGruzdev
Copy link
Copy Markdown

Add extension hooks to TelegramBotHandler for API URL and curl headers

Problem

TelegramBotHandler used a private const BOT_API and hardcoded self::BOT_API directly inside sendCurl(), making it impossible to:

  • point the handler at a self-hosted Telegram Bot API server
  • inject custom HTTP headers (auth tokens, tracing, proxies) into the curl request

Solution

Two protected hook methods added to sendCurl():

Method Default Purpose
botApiUrl(): string 'https://api.telegram.org/bot' Override to change the API base URL
getCurlHeaders(): array [] Override to add extra HTTP headers

No changes to the public API. Fully backwards compatible.


Usage example (English)

// Subclass pointing to a self-hosted Bot API + custom auth header
class MyTelegramHandler extends TelegramBotHandler
{
    protected function botApiUrl(): string
    {
        return 'https://telegram-api.internal.example.com/bot';
    }

    protected function getCurlHeaders(): array
    {
        return [
            'X-Internal-Token: my-secret-token',
            'X-Request-Source: monolog',
        ];
    }
}

$handler = new MyTelegramHandler(
    apiKey: 'YOUR_BOT_TOKEN',
    channel: '@your_channel',
);
$logger = new \Monolog\Logger('app');
$logger->pushHandler($handler);
$logger->error('Something went wrong');

Пример использования (Russian)

// Наследник, использующий собственный сервер Bot API и токен авторизации
class MyTelegramHandler extends TelegramBotHandler
{
    // Меняем базовый URL на свой сервер (self-hosted Telegram Bot API)
    protected function botApiUrl(): string
    {
        return 'https://telegram-api.internal.example.com/bot';
    }

    // Добавляем произвольные HTTP-заголовки к каждому curl-запросу
    protected function getCurlHeaders(): array
    {
        return [
            'X-Internal-Token: my-secret-token',
            'X-Request-Source: monolog',
        ];
    }
}

$handler = new MyTelegramHandler(
    apiKey: 'YOUR_BOT_TOKEN',
    channel: '@your_channel',
);

$logger = new \Monolog\Logger('app');
$logger->pushHandler($handler);
$logger->error('Что-то пошло не так');

Changes

  • src/Monolog/Handler/TelegramBotHandler.php
    • Added protected function botApiUrl(): string
    • Added protected function getCurlHeaders(): array
    • sendCurl() now calls $this->botApiUrl() instead of self::BOT_API
    • sendCurl() applies CURLOPT_HTTPHEADER when getCurlHeaders() returns a non-empty array

- Add protected botApiUrl() to allow overriding the Bot API base URL in subclasses
- Add protected getCurlHeaders() to allow injecting custom HTTP headers into curl requests

$ch = curl_init();
$url = self::BOT_API . $this->apiKey . '/SendMessage';
Copy link
Copy Markdown

@daniser daniser Apr 16, 2026

Choose a reason for hiding this comment

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

I think this should be changed to:

$url = $this->botApiUrl() . $this->apiKey . '/SendMessage';

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.

2 participants