From 8cec9591513e73652b8219acba9892cebc2ca737 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Wed, 13 Mar 2019 23:21:01 +0200 Subject: [PATCH] improve file uploading. --- README.md | 19 +++++++++---- src/Bot.php | 66 +++++++++++++++++++++++++++++++------------- src/File.php | 10 +++++++ src/FileId.php | 25 +++++++++++++++++ src/FileUrl.php | 25 +++++++++++++++++ src/InputFile.php | 33 ++++++++++++++++++++++ src/SendDocument.php | 26 +++++++++++++---- 7 files changed, 175 insertions(+), 29 deletions(-) create mode 100644 src/File.php create mode 100644 src/FileId.php create mode 100644 src/FileUrl.php create mode 100644 src/InputFile.php diff --git a/README.md b/README.md index 3fb4a88..95d1b31 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,10 @@ gif, pdf and zip files._ (from documentation) getMessage()->getChat()->getId(), - $document // or file_get_contents($document) if it's a local file + $document ); // also you can set `caption` to image diff --git a/src/Bot.php b/src/Bot.php index 8496162..03f0e92 100644 --- a/src/Bot.php +++ b/src/Bot.php @@ -46,7 +46,7 @@ public function getWebhookInfo(): ResponseInterface return $this->httpClient->post($this->getMethodUrl('getWebhookInfo')); } - public function sendMessage(SendMessage $sendMessage): ?Message + public function sendMessage(SendMessage $sendMessage): Message { $response = $this->httpClient->post($this->getMethodUrl('sendMessage'), [ 'json' => get_values($sendMessage), @@ -92,32 +92,60 @@ public function sendPhoto(SendPhoto $sendPhoto): ResponseInterface ]); } - public function sendDocument(SendDocument $sendDocument): ResponseInterface + public function sendDocument(SendDocument $sendDocument): Message { - if (strpos($sendDocument->getDocument(), 'http') === 0) { - return $this->httpClient->post($this->getMethodUrl('sendDocument'), [ - 'json' => get_values($sendDocument), + $doc = $sendDocument->getDocument(); + + if ($doc instanceof FileId || $doc instanceof FileUrl) { + $data = get_values($sendDocument); + $data['document'] = (string) $doc; + + $response = $this->httpClient->post($this->getMethodUrl('sendDocument'), [ + 'json' => $data, ]); - } - $values = get_values($sendDocument); - $data[] = [ - 'name' => 'document', - 'contents' => $values['document'], - 'filename' => 'picture.jpg', - ]; - unset($values['document']); + $json = json_decode((string) $response->getBody(), true); + if (isset($json['ok']) && $json['ok']) { + $message = new Message(); + set_values($message, $json['result']); - foreach ($values as $name => $value) { + return $message; + } + + throw new \LogicException('Unexpected response: '.(string) $response->getBody()); + } + + if ($doc instanceof InputFile) { $data[] = [ - 'name' => $name, - 'contents' => $value, + 'name' => 'document', + 'contents' => $doc->getContent(), + 'filename' => $doc->getFileName(), ]; + + $values = get_values($sendDocument); + foreach ($values as $name => $value) { + $data[] = [ + 'name' => $name, + 'contents' => $value, + ]; + } + + $response = $this->httpClient->post($this->getMethodUrl('sendDocument'), [ + 'multipart' => $data, + ]); + + $json = json_decode((string) $response->getBody(), true); + if (isset($json['ok']) && $json['ok']) { + $message = new Message(); + set_values($message, $json['result']); + + return $message; + } + + throw new \LogicException('Unexpected response: '.(string) $response->getBody()); } - return $this->httpClient->post($this->getMethodUrl('sendDocument'), [ - 'multipart' => $data, - ]); + throw new \LogicException(sprintf('Unexpected document: %s'.get_class($doc))); } public function sendInvoice(SendInvoice $sendInvoice) diff --git a/src/File.php b/src/File.php new file mode 100644 index 0000000..f2c3748 --- /dev/null +++ b/src/File.php @@ -0,0 +1,10 @@ +fileId = $fileId; + } + + public function getFileId(): string + { + return $this->fileId; + } + + public function __toString() + { + return $this->fileId; + } +} \ No newline at end of file diff --git a/src/FileUrl.php b/src/FileUrl.php new file mode 100644 index 0000000..49777c9 --- /dev/null +++ b/src/FileUrl.php @@ -0,0 +1,25 @@ +url = $url; + } + + public function getUrl(): string + { + return $this->url; + } + + public function __toString() + { + return $this->url; + } +} \ No newline at end of file diff --git a/src/InputFile.php b/src/InputFile.php new file mode 100644 index 0000000..eaaba0d --- /dev/null +++ b/src/InputFile.php @@ -0,0 +1,33 @@ +fileName = $fileName; + $this->content = $content; + } + + public function getFileName(): string + { + return $this->fileName; + } + + public function getContent(): string + { + return $this->content; + } + + public function __toString() + { + return $this->content; + } +} \ No newline at end of file diff --git a/src/SendDocument.php b/src/SendDocument.php index bedb4f9..ff2af80 100644 --- a/src/SendDocument.php +++ b/src/SendDocument.php @@ -11,12 +11,13 @@ class SendDocument { private $values = []; - private $objects = []; + private $document; - public function __construct(int $chatId, string $document) + private function __construct(int $chatId, File $document) { set_value($this, 'chat_id', $chatId); - set_value($this, 'document', $document); + + $this->document = $document; } public function getChatId(): int @@ -24,9 +25,9 @@ public function getChatId(): int return get_value($this, 'chat_id'); } - public function getDocument(): string + public function getDocument(): File { - return get_value($this, 'document'); + return $this->document; } public function getCaption(): string @@ -53,4 +54,19 @@ public function setReplyMarkup(ReplyMarkup $replyMarkup): void { set_value($this, 'reply_markup', json_encode(get_values($replyMarkup))); } + + public static function withInputFile(int $chatId, InputFile $file): self + { + return new static($chatId, $file); + } + + public static function withFileUrl(int $chatId, FileUrl $file): self + { + return new static($chatId, $file); + } + + public static function withFileId(int $chatId, FileId $file): self + { + return new static($chatId, $file); + } }