Skip to content

Commit

Permalink
Telegram Handler: support additional API parameters (#1451)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal-So authored May 22, 2020
1 parent de87dad commit 7a801dd
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
62 changes: 61 additions & 1 deletion src/Monolog/Handler/TelegramBotHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -55,14 +84,42 @@ public function __construct(
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);

$this->apiKey = $apiKey;
$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;
}

/**
Expand All @@ -87,6 +144,9 @@ protected function send(string $message): void
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);
Expand Down
24 changes: 22 additions & 2 deletions tests/Monolog/Handler/TelegramBotHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ public function testSendTelegramRequest(): void
* @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)
Expand All @@ -47,4 +53,18 @@ private function createHandler(string $apiKey = 'testKey', string $channel = 'te
$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');
}
}

0 comments on commit 7a801dd

Please sign in to comment.