Skip to content

Commit

Permalink
improve file uploading.
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Mar 13, 2019
1 parent fff3ea1 commit 8cec959
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 29 deletions.
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,28 @@ gif, pdf and zip files._ (from documentation)
<?php
use Formapro\TelegramBot\Bot;
use Formapro\TelegramBot\Update;
use Formapro\TelegramBot\SendMessage;
use Formapro\TelegramBot\SendDocument;
use Formapro\TelegramBot\FileUrl;
use Formapro\TelegramBot\FileId;
use Formapro\TelegramBot\InputFile;

$requestBody = file_get_contents('php://input');
$data = json_decode($requestBody, true);

$update = Update::create($data);

// You can pass URI of image or a path to file
$document = 'https://some-server.com/some.pdf'; // OR /path/to/document

$sendDocument = new SendDocument(
//$document = new FileUrl('https://some-server.com/some.pdf');

// You can pass an ID of already stored file on Telegram side.
//$document = new FileId('123');

// You can pass local file.
$document = new InputFile('test.txt', 'Hi there!');

$sendDocument = SendDocument::withInputFile(
$update->getMessage()->getChat()->getId(),
$document // or file_get_contents($document) if it's a local file
$document
);

// also you can set `caption` to image
Expand Down
66 changes: 47 additions & 19 deletions src/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions src/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace Formapro\TelegramBot;

/**
* @see https://core.telegram.org/bots/api#sending-files
*/
interface File
{

}
25 changes: 25 additions & 0 deletions src/FileId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace Formapro\TelegramBot;

/**
* @see https://core.telegram.org/bots/api#sending-files
*/
class FileId implements File
{
private $fileId;

public function __construct(string $fileId)
{
$this->fileId = $fileId;
}

public function getFileId(): string
{
return $this->fileId;
}

public function __toString()
{
return $this->fileId;
}
}
25 changes: 25 additions & 0 deletions src/FileUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace Formapro\TelegramBot;

/**
* @see https://core.telegram.org/bots/api#sending-files
*/
class FileUrl implements File
{
private $url;

public function __construct(string $url)
{
$this->url = $url;
}

public function getUrl(): string
{
return $this->url;
}

public function __toString()
{
return $this->url;
}
}
33 changes: 33 additions & 0 deletions src/InputFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace Formapro\TelegramBot;

/**
* @see https://core.telegram.org/bots/api#sending-files
*/
class InputFile implements File
{
private $fileName;

private $content;

public function __construct(string $fileName, string $content)
{
$this->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;
}
}
26 changes: 21 additions & 5 deletions src/SendDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@ 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
{
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
Expand All @@ -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);
}
}

0 comments on commit 8cec959

Please sign in to comment.