From f46863ca4ed67bfc67edf0d51171fcfef717caa5 Mon Sep 17 00:00:00 2001 From: Luca Patera Date: Thu, 1 Jun 2023 19:20:06 +0200 Subject: [PATCH 1/3] Fix Logger --- src/Log/LoggerHandler.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Log/LoggerHandler.php b/src/Log/LoggerHandler.php index 68daca8..5529907 100644 --- a/src/Log/LoggerHandler.php +++ b/src/Log/LoggerHandler.php @@ -6,6 +6,7 @@ use Monolog\Formatter\LineFormatter; use Monolog\Handler\AbstractProcessingHandler; use Monolog\Logger; +use Monolog\LogRecord; use SergiX44\Nutgram\Nutgram; class LoggerHandler extends AbstractProcessingHandler @@ -27,20 +28,21 @@ protected function getDefaultFormatter(): FormatterInterface return new LineFormatter("%message% %context% %extra%\n"); } - protected function write(array $record): void + protected function write(LogRecord $record): void { $oldSplitConfig = config('nutgram.config.split_long_messages', false); config(['nutgram.config.split_long_messages' => true]); - $this->bot->sendMessage($this->formatText($record), [ - 'chat_id' => $this->chatId, - 'parse_mode' => 'html', - ]); + $this->bot->sendMessage( + text: $this->formatText($record), + chat_id: $this->chatId, + parse_mode: 'html', + ); config(['nutgram.config.split_long_messages' => $oldSplitConfig]); } - protected function formatText(array $record): string + protected function formatText(LogRecord $record): string { return sprintf( "%s %s (%s):\n
%s
", From 7059a6146c66c602080c46462de6679fe6cab6ed Mon Sep 17 00:00:00 2001 From: Luca Patera Date: Thu, 1 Jun 2023 19:34:46 +0200 Subject: [PATCH 2/3] Fix Mixins --- src/Mixins/MixinUtils.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Mixins/MixinUtils.php b/src/Mixins/MixinUtils.php index ddcab8f..95af653 100644 --- a/src/Mixins/MixinUtils.php +++ b/src/Mixins/MixinUtils.php @@ -8,6 +8,7 @@ use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Psr\Http\Client\ClientInterface; +use RuntimeException; use SergiX44\Nutgram\Telegram\Types\Media\File; class MixinUtils @@ -24,22 +25,28 @@ class MixinUtils */ public static function saveFileToDisk(File $file, string $path, ?string $disk = null, array $clientOpt = []): bool { + $bot = $file->getBot(); + + if ($bot === null) { + throw new RuntimeException('Bot instance not found.'); + } + $storage = Storage::disk($disk); if (Str::endsWith($path, ['/', '\\'])) { $path .= basename($file->file_path ?? $file->file_id); } - if ($file->getConfig()->is_local ?? false) { - return $storage->put($path, $file->downloadUrl($file)); + if ($bot->getConfig()->is_local ?? false) { + return $storage->put($path, $bot->downloadUrl($file)); } //create temp file - $tmpFile = tempnam(sys_get_temp_dir(), uniqid(strftime('%G-%m-%d'), true)); + $tmpFile = tempnam(sys_get_temp_dir(), uniqid(time(), true)); //download file to temp file - $http = $file->getContainer()->get(ClientInterface::class); - $http->get($file->downloadUrl($file), array_merge(['sink' => $tmpFile], $clientOpt)); + $http = $bot->getContainer()->get(ClientInterface::class); + $http->get($bot->downloadUrl($file), array_merge(['sink' => $tmpFile], $clientOpt)); //save temp file to disk return $storage->putFileAs('/', new LaravelFile($tmpFile), $path); From abd9c08c7e35c7296f6a34c1320d577ce5c63ff2 Mon Sep 17 00:00:00 2001 From: Luca Patera Date: Thu, 1 Jun 2023 19:39:55 +0200 Subject: [PATCH 3/3] Use sendChunkedMessage method --- src/Log/LoggerHandler.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Log/LoggerHandler.php b/src/Log/LoggerHandler.php index 5529907..ee26077 100644 --- a/src/Log/LoggerHandler.php +++ b/src/Log/LoggerHandler.php @@ -30,16 +30,11 @@ protected function getDefaultFormatter(): FormatterInterface protected function write(LogRecord $record): void { - $oldSplitConfig = config('nutgram.config.split_long_messages', false); - config(['nutgram.config.split_long_messages' => true]); - - $this->bot->sendMessage( + $this->bot->sendChunkedMessage( text: $this->formatText($record), chat_id: $this->chatId, parse_mode: 'html', ); - - config(['nutgram.config.split_long_messages' => $oldSplitConfig]); } protected function formatText(LogRecord $record): string