diff --git a/src/Log/LoggerHandler.php b/src/Log/LoggerHandler.php index 68daca8..ee26077 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,16 @@ 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', - ]); - - config(['nutgram.config.split_long_messages' => $oldSplitConfig]); + $this->bot->sendChunkedMessage( + text: $this->formatText($record), + chat_id: $this->chatId, + parse_mode: 'html', + ); } - protected function formatText(array $record): string + protected function formatText(LogRecord $record): string { return sprintf( "%s %s (%s):\n
%s
", 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);