Skip to content

Commit

Permalink
Merge pull request #5 from Niklan/message-thread-id
Browse files Browse the repository at this point in the history
Add support for Message Thread ID (Topics)
  • Loading branch information
jacklul authored Jan 24, 2025
2 parents 3d535b4 + 6c4d2de commit 47c85c4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ $handler = new TelegramHandler(
true, // Use cURL or not? default: true = use when available
10, // Timeout for API requests, default: 10
true // Verify SSL certificate or not? default: true, false only useful for development - avoid in production
2, // (optional) Target Thread ID for group chats with Topics enabled
);

$handler->setFormatter(new TelegramFormatter()); // Usage of this formatter is optional but recommended if you want better message layout
Expand Down
31 changes: 23 additions & 8 deletions src/TelegramHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,32 @@ class TelegramHandler extends AbstractProcessingHandler
private bool $verifyPeer;

/**
* @param string $token Telegram bot API token
* @param int $chatId Chat ID to which logs will be sent
* @param Level $level The minimum logging level at which this handler will be triggered
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
* @param bool $useCurl Whether to use cURL extension when available or not
* @param int $timeout Maximum time to wait for requests to finish
* @param bool $verifyPeer Whether to use SSL certificate verification or not
* Thread ID for group chats with Topics feature enabled.
*
* Allows to send a message in a specific thread, instead of "General".
*
* @var int|null
*/
public function __construct(string $token, int $chatId, int|string|Level $level = Level::Debug, bool $bubble = true, bool $useCurl = true, int $timeout = 10, bool $verifyPeer = true)
private ?int $messageThreadId;

/**
* @param string $token Telegram bot API token
* @param int $chatId Chat ID to which logs will be sent
* @param int|string|Level $level The minimum logging level at which this handler will be triggered
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
* @param bool $useCurl Whether to use cURL extension when available or not
* @param int $timeout Maximum time to wait for requests to finish
* @param bool $verifyPeer Whether to use SSL certificate verification or not
* @param int|null $messageThreadId Thread ID for group chats with Topics feature enabled
*/
public function __construct(string $token, int $chatId, int|string|Level $level = Level::Debug, bool $bubble = true, bool $useCurl = true, int $timeout = 10, bool $verifyPeer = true, ?int $messageThreadId = null)
{
$this->token = $token;
$this->chatId = $chatId;
$this->useCurl = $useCurl;
$this->timeout = $timeout;
$this->verifyPeer = $verifyPeer;
$this->messageThreadId = $messageThreadId;

parent::__construct($level, $bubble);
}
Expand Down Expand Up @@ -153,6 +164,10 @@ private function send(string $message): bool
$data['parse_mode'] = 'HTML';
}

if ($this->messageThreadId) {
$data['message_thread_id'] = $this->messageThreadId;
}

if ($this->useCurl === true && extension_loaded('curl')) {
$ch = curl_init();

Expand Down

0 comments on commit 47c85c4

Please sign in to comment.