Skip to content

Commit

Permalink
file uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Mar 14, 2019
1 parent 8cec959 commit 4f558a6
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 17 deletions.
45 changes: 35 additions & 10 deletions src/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function sendMessage(SendMessage $sendMessage): Message
return $message;
}

throw new \LogicException('Unexpected response: '.(string) $response->getBody());
throw new \LogicException('Unexpected response: ' . (string)$response->getBody());
}

public function sendPhoto(SendPhoto $sendPhoto): ResponseInterface
Expand Down Expand Up @@ -98,21 +98,21 @@ public function sendDocument(SendDocument $sendDocument): Message

if ($doc instanceof FileId || $doc instanceof FileUrl) {
$data = get_values($sendDocument);
$data['document'] = (string) $doc;
$data['document'] = (string)$doc;

$response = $this->httpClient->post($this->getMethodUrl('sendDocument'), [
'json' => $data,
]);

$json = json_decode((string) $response->getBody(), true);
$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());
throw new \LogicException('Unexpected response: ' . (string)$response->getBody());
}

if ($doc instanceof InputFile) {
Expand All @@ -134,18 +134,18 @@ public function sendDocument(SendDocument $sendDocument): Message
'multipart' => $data,
]);

$json = json_decode((string) $response->getBody(), true);
$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());
throw new \LogicException('Unexpected response: ' . (string) $response->getBody());
}

throw new \LogicException(sprintf('Unexpected document: %s'.get_class($doc)));
throw new \LogicException(sprintf('Unexpected document: %s' . get_class($doc)));
}

public function sendInvoice(SendInvoice $sendInvoice)
Expand Down Expand Up @@ -175,7 +175,7 @@ public function editMessageText(EditMessageText $editMessageText): ?Message
'json' => get_values($editMessageText),
]);

$json = json_decode((string) $response->getBody(), true);
$json = json_decode((string)$response->getBody(), true);

if (isset($json['ok']) && $json['ok']) {
$message = new Message();
Expand All @@ -184,7 +184,7 @@ public function editMessageText(EditMessageText $editMessageText): ?Message
return $message;
}

throw new \LogicException('Unexpected response: '.(string) $response->getBody());
throw new \LogicException('Unexpected response: ' . (string) $response->getBody());
}

public function deleteMessage(DeleteMessage $deleteMessage): bool
Expand All @@ -193,7 +193,7 @@ public function deleteMessage(DeleteMessage $deleteMessage): bool
'json' => get_values($deleteMessage),
]);

$response = json_decode((string) $response->getBody(), true);
$response = json_decode((string)$response->getBody(), true);

if (isset($response['ok']) && $response['ok']) {
return true;
Expand All @@ -202,6 +202,31 @@ public function deleteMessage(DeleteMessage $deleteMessage): bool
return false;
}

/**
* @see https://core.telegram.org/bots/api#getfile
*/
public function getFile(GetFile $getFile): File
{
$response = $this->httpClient->post($this->getMethodUrl('getFile'), [
'json' => get_values($getFile),
]);

$json = json_decode((string) $response->getBody(), true);

if (isset($json['ok']) && $json['ok']) {
$file = new File();
set_values($file, $json['result']);

if ($path = $file->getFilePath()) {
$file->setFileUrl(sprintf('https://api.telegram.org/file/bot%s/%s', $this->token, $path));
}

return $file;
}

throw new \LogicException('Unexpected response: ' . (string) $response->getBody());
}

private function getMethodUrl(string $method): string
{
return sprintf('https://api.telegram.org/bot%s/%s', $this->token, $method);
Expand Down
32 changes: 32 additions & 0 deletions src/Document.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace Formapro\TelegramBot;

use function Formapro\Values\get_value;

/**
* @see https://core.telegram.org/bots/api#document
*/
class Document
{
private $values = [];

public function getFileId(): string
{
return get_value($this, 'file_id');
}

public function getFileName(): ?string
{
return get_value($this, 'file_name');
}

public function getFileSize(): ?int
{
return get_value($this, 'file_size');
}

public function getMimeType(): ?string
{
return get_value($this, 'mime_type');
}
}
33 changes: 31 additions & 2 deletions src/File.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
<?php
namespace Formapro\TelegramBot;

use function Formapro\Values\get_value;

/**
* @see https://core.telegram.org/bots/api#sending-files
* @see https://core.telegram.org/bots/api#file
*/
interface File
class File
{
private $values = [];

private $fileUrl;

public function getFileId(): string
{
return get_value($this, 'file_id');
}

public function getFileSize(): ?int
{
return get_value($this, 'file_size');
}

public function getFilePath(): ?string
{
return get_value($this, 'file_path');
}

public function setFileUrl(?string $fileUrl): void
{
$this->fileUrl = $fileUrl;
}

public function getFileUrl(): ?string
{
return $this->fileUrl;
}
}
2 changes: 1 addition & 1 deletion src/FileId.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* @see https://core.telegram.org/bots/api#sending-files
*/
class FileId implements File
class FileId
{
private $fileId;

Expand Down
2 changes: 1 addition & 1 deletion src/FileUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* @see https://core.telegram.org/bots/api#sending-files
*/
class FileUrl implements File
class FileUrl
{
private $url;

Expand Down
20 changes: 20 additions & 0 deletions src/GetFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Formapro\TelegramBot;

use function Formapro\Values\get_value;
use function Formapro\Values\get_values;
use function Formapro\Values\set_value;

/**
* @see https://core.telegram.org/bots/api#getfile
*/
class GetFile
{
private $values = [];

public function __construct(string $fileId)
{
set_value($this, 'file_id', $fileId);
}
}
2 changes: 1 addition & 1 deletion src/InputFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* @see https://core.telegram.org/bots/api#sending-files
*/
class InputFile implements File
class InputFile
{
private $fileName;

Expand Down
5 changes: 5 additions & 0 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public function getContact(): ?Contact
return get_object($this, 'contact', Contact::class);
}

public function getDocument(): ?Document
{
return get_object($this, 'document', Document::class);
}

/**
* It can be used only after sending invoice and successful paying for it.
*
Expand Down
14 changes: 12 additions & 2 deletions src/SendDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ class SendDocument
{
private $values = [];

/**
* @var FileId|FileUrl|InputFile
*/
private $document;

private function __construct(int $chatId, File $document)
/**
* @param int $chatId
* @param FileId|FileUrl|InputFile $document
*/
private function __construct(int $chatId, $document)
{
set_value($this, 'chat_id', $chatId);

Expand All @@ -25,7 +32,10 @@ public function getChatId(): int
return get_value($this, 'chat_id');
}

public function getDocument(): File
/**
* @return FileId|FileUrl|InputFile
*/
public function getDocument()
{
return $this->document;
}
Expand Down

0 comments on commit 4f558a6

Please sign in to comment.