Skip to content

Commit c2be264

Browse files
committed
Cut too long message into several messages
1 parent a6559af commit c2be264

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/TelegramHandler.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,11 @@ public function write(array $record): void
8080

8181
// trying to make request and send notification
8282
try {
83-
file_get_contents(
84-
'https://api.telegram.org/bot' . $this->botToken . '/sendMessage?'
85-
. http_build_query([
86-
'text' => $this->applyTelegramMaxCharactersLimit($this->formatText($record)),
87-
'chat_id' => $this->chatId,
88-
'parse_mode' => 'html'
89-
])
90-
);
83+
$textChunks = str_split($this->formatText($record), 4096);
84+
85+
foreach ($textChunks as $textChunk) {
86+
$this->sendMessage($textChunk);
87+
}
9188
} catch (Exception $exception) {
9289
\Log::channel('single')->error($exception->getMessage());
9390
}
@@ -107,7 +104,6 @@ protected function getDefaultFormatter(): FormatterInterface
107104
*/
108105
private function formatText(array $record): string
109106
{
110-
111107
return view($this->template, array_merge($record,[
112108
'appName' => $this->appName,
113109
'appEnv' => $this->appEnv,
@@ -116,11 +112,16 @@ private function formatText(array $record): string
116112
}
117113

118114
/**
119-
* @param string $text
120-
* @return string
115+
* @param string $text
121116
*/
122-
private function applyTelegramMaxCharactersLimit(string $text): string
117+
private function sendMessage(string $text): void
123118
{
124-
return mb_substr($text, 0, 4096);
119+
$httpQuery = http_build_query([
120+
'text' => $text,
121+
'chat_id' => $this->chatId,
122+
'parse_mode' => 'html',
123+
]);
124+
125+
file_get_contents('https://api.telegram.org/bot'.$this->botToken.'/sendMessage?' . $httpQuery);
125126
}
126127
}

0 commit comments

Comments
 (0)