1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-06 13:16:39 +02:00

Add TelegramBotHandler topics support (#1802)

This commit is contained in:
Samson Endale
2023-06-20 17:42:38 +03:00
committed by GitHub
parent 4356885fbb
commit 1fd8e8c2c7
2 changed files with 26 additions and 5 deletions

View File

@@ -88,11 +88,18 @@ class TelegramBotHandler extends AbstractProcessingHandler
*/ */
private bool $delayBetweenMessages; private bool $delayBetweenMessages;
/**
* Telegram message thread id, unique identifier for the target message thread (topic) of the forum; for forum supergroups only
* See how to get the `message_thread_id` https://stackoverflow.com/a/75178418
*/
private int|null $topic;
/** /**
* @param string $apiKey Telegram bot access token provided by BotFather * @param string $apiKey Telegram bot access token provided by BotFather
* @param string $channel Telegram channel name * @param string $channel Telegram channel name
* @param bool $splitLongMessages Split a message longer than MAX_MESSAGE_LENGTH into parts and send in multiple messages * @param bool $splitLongMessages Split a message longer than MAX_MESSAGE_LENGTH into parts and send in multiple messages
* @param bool $delayBetweenMessages Adds delay between sending a split message according to Telegram API * @param bool $delayBetweenMessages Adds delay between sending a split message according to Telegram API
* @param int $topic Telegram message thread id, unique identifier for the target message thread (topic) of the forum
* @throws MissingExtensionException If the curl extension is missing * @throws MissingExtensionException If the curl extension is missing
*/ */
public function __construct( public function __construct(
@@ -104,7 +111,8 @@ class TelegramBotHandler extends AbstractProcessingHandler
bool $disableWebPagePreview = null, bool $disableWebPagePreview = null,
bool $disableNotification = null, bool $disableNotification = null,
bool $splitLongMessages = false, bool $splitLongMessages = false,
bool $delayBetweenMessages = false bool $delayBetweenMessages = false,
int $topic = null
) { ) {
if (!extension_loaded('curl')) { if (!extension_loaded('curl')) {
throw new MissingExtensionException('The curl extension is needed to use the TelegramBotHandler'); throw new MissingExtensionException('The curl extension is needed to use the TelegramBotHandler');
@@ -119,6 +127,7 @@ class TelegramBotHandler extends AbstractProcessingHandler
$this->disableNotification($disableNotification); $this->disableNotification($disableNotification);
$this->splitLongMessages($splitLongMessages); $this->splitLongMessages($splitLongMessages);
$this->delayBetweenMessages($delayBetweenMessages); $this->delayBetweenMessages($delayBetweenMessages);
$this->setTopic($topic);
} }
public function setParseMode(string $parseMode = null): self public function setParseMode(string $parseMode = null): self
@@ -169,6 +178,13 @@ class TelegramBotHandler extends AbstractProcessingHandler
return $this; return $this;
} }
public function setTopic(int $topic = null): self
{
$this->topic = $topic;
return $this;
}
/** /**
* @inheritDoc * @inheritDoc
*/ */
@@ -224,13 +240,17 @@ class TelegramBotHandler extends AbstractProcessingHandler
curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([ $params = [
'text' => $message, 'text' => $message,
'chat_id' => $this->channel, 'chat_id' => $this->channel,
'parse_mode' => $this->parseMode, 'parse_mode' => $this->parseMode,
'disable_web_page_preview' => $this->disableWebPagePreview, 'disable_web_page_preview' => $this->disableWebPagePreview,
'disable_notification' => $this->disableNotification, 'disable_notification' => $this->disableNotification,
])); ];
if ($this->topic !== null) {
$params['message_thread_id'] = $this->topic;
}
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
$result = Curl\Util::execute($ch); $result = Curl\Util::execute($ch);
if (!is_string($result)) { if (!is_string($result)) {

View File

@@ -34,9 +34,10 @@ class TelegramBotHandlerTest extends TestCase
string $channel = 'testChannel', string $channel = 'testChannel',
string $parseMode = 'Markdown', string $parseMode = 'Markdown',
bool $disableWebPagePreview = false, bool $disableWebPagePreview = false,
bool $disableNotification = true bool $disableNotification = true,
int $topic = 1
): void { ): void {
$constructorArgs = [$apiKey, $channel, Level::Debug, true, $parseMode, $disableWebPagePreview, $disableNotification]; $constructorArgs = [$apiKey, $channel, Level::Debug, true, $parseMode, $disableWebPagePreview, $disableNotification, $topic];
$this->handler = $this->getMockBuilder(TelegramBotHandler::class) $this->handler = $this->getMockBuilder(TelegramBotHandler::class)
->setConstructorArgs($constructorArgs) ->setConstructorArgs($constructorArgs)