mirror of
https://github.com/Seldaek/monolog.git
synced 2025-07-30 18:00:17 +02:00
Telegram Handler: support additional API parameters (#1451)
This commit is contained in:
@@ -32,6 +32,15 @@ class TelegramBotHandler extends AbstractProcessingHandler
|
||||
{
|
||||
private const BOT_API = 'https://api.telegram.org/bot';
|
||||
|
||||
/**
|
||||
* @var array AVAILABLE_PARSE_MODES The available values of parseMode according to the Telegram api documentation
|
||||
*/
|
||||
private const AVAILABLE_PARSE_MODES = [
|
||||
'HTML',
|
||||
'MarkdownV2',
|
||||
'Markdown' // legacy mode without underline and strikethrough, use MarkdownV2 instead
|
||||
];
|
||||
|
||||
/**
|
||||
* Telegram bot access token provided by BotFather.
|
||||
* Create telegram bot with https://telegram.me/BotFather and use access token from it.
|
||||
@@ -46,6 +55,26 @@ class TelegramBotHandler extends AbstractProcessingHandler
|
||||
*/
|
||||
private $channel;
|
||||
|
||||
/**
|
||||
* The kind of formatting that is used for the message.
|
||||
* See available options at https://core.telegram.org/bots/api#formatting-options
|
||||
* or in AVAILABLE_PARSE_MODES
|
||||
* @var string|null
|
||||
*/
|
||||
private $parseMode;
|
||||
|
||||
/**
|
||||
* Disables link previews for links in the message.
|
||||
* @var bool|null
|
||||
*/
|
||||
private $disableWebPagePreview;
|
||||
|
||||
/**
|
||||
* Sends the message silently. Users will receive a notification with no sound.
|
||||
* @var bool|null
|
||||
*/
|
||||
private $disableNotification;
|
||||
|
||||
/**
|
||||
* @param string $apiKey Telegram bot access token provided by BotFather
|
||||
* @param string $channel Telegram channel name
|
||||
@@ -55,7 +84,10 @@ class TelegramBotHandler extends AbstractProcessingHandler
|
||||
string $apiKey,
|
||||
string $channel,
|
||||
$level = Logger::DEBUG,
|
||||
bool $bubble = true
|
||||
bool $bubble = true,
|
||||
string $parseMode = null,
|
||||
bool $disableWebPagePreview = null,
|
||||
bool $disableNotification = null
|
||||
) {
|
||||
parent::__construct($level, $bubble);
|
||||
|
||||
@@ -63,6 +95,31 @@ class TelegramBotHandler extends AbstractProcessingHandler
|
||||
$this->channel = $channel;
|
||||
$this->level = $level;
|
||||
$this->bubble = $bubble;
|
||||
$this->setParseMode($parseMode);
|
||||
$this->disableWebPagePreview($disableWebPagePreview);
|
||||
$this->disableNotification($disableNotification);
|
||||
}
|
||||
|
||||
public function setParseMode(string $parseMode = null): self
|
||||
{
|
||||
if ($parseMode !== null && !in_array($parseMode, self::AVAILABLE_PARSE_MODES)) {
|
||||
throw new \InvalidArgumentException('Unknown parseMode, use one of these: ' . implode(', ', self::AVAILABLE_PARSE_MODES) . '.');
|
||||
}
|
||||
|
||||
$this->parseMode = $parseMode;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function disableWebPagePreview(bool $disableWebPagePreview = null): self
|
||||
{
|
||||
$this->disableWebPagePreview = $disableWebPagePreview;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function disableNotification(bool $disableNotification = null): self
|
||||
{
|
||||
$this->disableNotification = $disableNotification;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,6 +144,9 @@ class TelegramBotHandler extends AbstractProcessingHandler
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
|
||||
'text' => $message,
|
||||
'chat_id' => $this->channel,
|
||||
'parse_mode' => $this->parseMode,
|
||||
'disable_web_page_preview' => $this->disableWebPagePreview,
|
||||
'disable_notification' => $this->disableNotification,
|
||||
]));
|
||||
|
||||
$result = Curl\Util::execute($ch);
|
||||
|
@@ -35,9 +35,15 @@ class TelegramBotHandlerTest extends TestCase
|
||||
* @param string $apiKey
|
||||
* @param string $channel
|
||||
*/
|
||||
private function createHandler(string $apiKey = 'testKey', string $channel = 'testChannel'): void
|
||||
private function createHandler(
|
||||
string $apiKey = 'testKey',
|
||||
string $channel = 'testChannel',
|
||||
string $parseMode = 'Markdown',
|
||||
bool $disableWebPagePreview = false,
|
||||
bool $disableNotification = true
|
||||
): void
|
||||
{
|
||||
$constructorArgs = [$apiKey, $channel, Logger::DEBUG, true];
|
||||
$constructorArgs = [$apiKey, $channel, Logger::DEBUG, true, $parseMode, $disableWebPagePreview, $disableNotification];
|
||||
|
||||
$this->handler = $this->getMockBuilder(TelegramBotHandler::class)
|
||||
->setConstructorArgs($constructorArgs)
|
||||
@@ -47,4 +53,18 @@ class TelegramBotHandlerTest extends TestCase
|
||||
$this->handler->expects($this->atLeast(1))
|
||||
->method('send');
|
||||
}
|
||||
|
||||
public function testSetInvalidParseMode(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
|
||||
$handler = new TelegramBotHandler('testKey', 'testChannel');
|
||||
$handler->setParseMode('invalid parse mode');
|
||||
}
|
||||
|
||||
public function testSetParseMode(): void
|
||||
{
|
||||
$handler = new TelegramBotHandler('testKey', 'testChannel');
|
||||
$handler->setParseMode('HTML');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user