Skip to content

Commit f746dbb

Browse files
authored
Merge pull request #19 from sam130117/bugfix/too-long-message-for-telegram
Limit text message for telegram
2 parents 01c7cc6 + c2be264 commit f746dbb

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/TelegramHandler.php

Lines changed: 19 additions & 9 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->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,11 +104,24 @@ 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,
114110
])
115111
);
116112
}
113+
114+
/**
115+
* @param string $text
116+
*/
117+
private function sendMessage(string $text): void
118+
{
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);
126+
}
117127
}

0 commit comments

Comments
 (0)