Skip to content

Commit 4fc23dd

Browse files
feat(TelegramBotHandler): add truncateTextToTelegramLimit method
Getting the maximum character limit in Telegram messages.
1 parent e08255b commit 4fc23dd

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/TelegramBotHandler.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010
class TelegramBotHandler extends AbstractProcessingHandler implements HandlerInterface
1111
{
12+
/**
13+
* text parameter in sendMessage method
14+
* @see https://core.telegram.org/bots/api#sendmessage
15+
*/
16+
private const TELEGRAM_MESSAGE_SIZE = 4096;
17+
1218
/**
1319
* bot api url
1420
* @var string
@@ -76,6 +82,15 @@ protected function write($record): void
7682
$this->send($record['formatted']);
7783
}
7884

85+
private function truncateTextToTelegramLimit(string $textMessage): string
86+
{
87+
if (strlen($textMessage) <= self::TELEGRAM_MESSAGE_SIZE) {
88+
return $textMessage;
89+
}
90+
91+
return substr($textMessage, 0, self::TELEGRAM_MESSAGE_SIZE);
92+
}
93+
7994
/**
8095
* Send request to @link https://api.telegram.org/bot on SendMessage action.
8196
* @param string $message
@@ -99,6 +114,8 @@ protected function send(string $message, $option = []): void
99114
? $this->botApi
100115
: $this->botApi . $this->token . '/SendMessage';
101116

117+
$message = $this->truncateTextToTelegramLimit($message);
118+
102119
$params = [
103120
'text' => $message,
104121
'chat_id' => $this->chatId,

0 commit comments

Comments
 (0)