Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Message Thread ID (Topics) #5

Merged
merged 6 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading