|
|
@@ -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);
|