Skip to content

Commit 8cec959

Browse files
committed
improve file uploading.
1 parent fff3ea1 commit 8cec959

File tree

7 files changed

+175
-29
lines changed

7 files changed

+175
-29
lines changed

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,28 @@ gif, pdf and zip files._ (from documentation)
9898
<?php
9999
use Formapro\TelegramBot\Bot;
100100
use Formapro\TelegramBot\Update;
101-
use Formapro\TelegramBot\SendMessage;
101+
use Formapro\TelegramBot\SendDocument;
102+
use Formapro\TelegramBot\FileUrl;
103+
use Formapro\TelegramBot\FileId;
104+
use Formapro\TelegramBot\InputFile;
102105

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

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

108111
// You can pass URI of image or a path to file
109-
$document = 'https://some-server.com/some.pdf'; // OR /path/to/document
110-
111-
$sendDocument = new SendDocument(
112+
//$document = new FileUrl('https://some-server.com/some.pdf');
113+
114+
// You can pass an ID of already stored file on Telegram side.
115+
//$document = new FileId('123');
116+
117+
// You can pass local file.
118+
$document = new InputFile('test.txt', 'Hi there!');
119+
120+
$sendDocument = SendDocument::withInputFile(
112121
$update->getMessage()->getChat()->getId(),
113-
$document // or file_get_contents($document) if it's a local file
122+
$document
114123
);
115124

116125
// also you can set `caption` to image

src/Bot.php

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function getWebhookInfo(): ResponseInterface
4646
return $this->httpClient->post($this->getMethodUrl('getWebhookInfo'));
4747
}
4848

49-
public function sendMessage(SendMessage $sendMessage): ?Message
49+
public function sendMessage(SendMessage $sendMessage): Message
5050
{
5151
$response = $this->httpClient->post($this->getMethodUrl('sendMessage'), [
5252
'json' => get_values($sendMessage),
@@ -92,32 +92,60 @@ public function sendPhoto(SendPhoto $sendPhoto): ResponseInterface
9292
]);
9393
}
9494

95-
public function sendDocument(SendDocument $sendDocument): ResponseInterface
95+
public function sendDocument(SendDocument $sendDocument): Message
9696
{
97-
if (strpos($sendDocument->getDocument(), 'http') === 0) {
98-
return $this->httpClient->post($this->getMethodUrl('sendDocument'), [
99-
'json' => get_values($sendDocument),
97+
$doc = $sendDocument->getDocument();
98+
99+
if ($doc instanceof FileId || $doc instanceof FileUrl) {
100+
$data = get_values($sendDocument);
101+
$data['document'] = (string) $doc;
102+
103+
$response = $this->httpClient->post($this->getMethodUrl('sendDocument'), [
104+
'json' => $data,
100105
]);
101-
}
102-
$values = get_values($sendDocument);
103106

104-
$data[] = [
105-
'name' => 'document',
106-
'contents' => $values['document'],
107-
'filename' => 'picture.jpg',
108-
];
109-
unset($values['document']);
107+
$json = json_decode((string) $response->getBody(), true);
108+
if (isset($json['ok']) && $json['ok']) {
109+
$message = new Message();
110+
set_values($message, $json['result']);
110111

111-
foreach ($values as $name => $value) {
112+
return $message;
113+
}
114+
115+
throw new \LogicException('Unexpected response: '.(string) $response->getBody());
116+
}
117+
118+
if ($doc instanceof InputFile) {
112119
$data[] = [
113-
'name' => $name,
114-
'contents' => $value,
120+
'name' => 'document',
121+
'contents' => $doc->getContent(),
122+
'filename' => $doc->getFileName(),
115123
];
124+
125+
$values = get_values($sendDocument);
126+
foreach ($values as $name => $value) {
127+
$data[] = [
128+
'name' => $name,
129+
'contents' => $value,
130+
];
131+
}
132+
133+
$response = $this->httpClient->post($this->getMethodUrl('sendDocument'), [
134+
'multipart' => $data,
135+
]);
136+
137+
$json = json_decode((string) $response->getBody(), true);
138+
if (isset($json['ok']) && $json['ok']) {
139+
$message = new Message();
140+
set_values($message, $json['result']);
141+
142+
return $message;
143+
}
144+
145+
throw new \LogicException('Unexpected response: '.(string) $response->getBody());
116146
}
117147

118-
return $this->httpClient->post($this->getMethodUrl('sendDocument'), [
119-
'multipart' => $data,
120-
]);
148+
throw new \LogicException(sprintf('Unexpected document: %s'.get_class($doc)));
121149
}
122150

123151
public function sendInvoice(SendInvoice $sendInvoice)

src/File.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Formapro\TelegramBot;
3+
4+
/**
5+
* @see https://core.telegram.org/bots/api#sending-files
6+
*/
7+
interface File
8+
{
9+
10+
}

src/FileId.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace Formapro\TelegramBot;
3+
4+
/**
5+
* @see https://core.telegram.org/bots/api#sending-files
6+
*/
7+
class FileId implements File
8+
{
9+
private $fileId;
10+
11+
public function __construct(string $fileId)
12+
{
13+
$this->fileId = $fileId;
14+
}
15+
16+
public function getFileId(): string
17+
{
18+
return $this->fileId;
19+
}
20+
21+
public function __toString()
22+
{
23+
return $this->fileId;
24+
}
25+
}

src/FileUrl.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace Formapro\TelegramBot;
3+
4+
/**
5+
* @see https://core.telegram.org/bots/api#sending-files
6+
*/
7+
class FileUrl implements File
8+
{
9+
private $url;
10+
11+
public function __construct(string $url)
12+
{
13+
$this->url = $url;
14+
}
15+
16+
public function getUrl(): string
17+
{
18+
return $this->url;
19+
}
20+
21+
public function __toString()
22+
{
23+
return $this->url;
24+
}
25+
}

src/InputFile.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
namespace Formapro\TelegramBot;
3+
4+
/**
5+
* @see https://core.telegram.org/bots/api#sending-files
6+
*/
7+
class InputFile implements File
8+
{
9+
private $fileName;
10+
11+
private $content;
12+
13+
public function __construct(string $fileName, string $content)
14+
{
15+
$this->fileName = $fileName;
16+
$this->content = $content;
17+
}
18+
19+
public function getFileName(): string
20+
{
21+
return $this->fileName;
22+
}
23+
24+
public function getContent(): string
25+
{
26+
return $this->content;
27+
}
28+
29+
public function __toString()
30+
{
31+
return $this->content;
32+
}
33+
}

src/SendDocument.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,23 @@ class SendDocument
1111
{
1212
private $values = [];
1313

14-
private $objects = [];
14+
private $document;
1515

16-
public function __construct(int $chatId, string $document)
16+
private function __construct(int $chatId, File $document)
1717
{
1818
set_value($this, 'chat_id', $chatId);
19-
set_value($this, 'document', $document);
19+
20+
$this->document = $document;
2021
}
2122

2223
public function getChatId(): int
2324
{
2425
return get_value($this, 'chat_id');
2526
}
2627

27-
public function getDocument(): string
28+
public function getDocument(): File
2829
{
29-
return get_value($this, 'document');
30+
return $this->document;
3031
}
3132

3233
public function getCaption(): string
@@ -53,4 +54,19 @@ public function setReplyMarkup(ReplyMarkup $replyMarkup): void
5354
{
5455
set_value($this, 'reply_markup', json_encode(get_values($replyMarkup)));
5556
}
57+
58+
public static function withInputFile(int $chatId, InputFile $file): self
59+
{
60+
return new static($chatId, $file);
61+
}
62+
63+
public static function withFileUrl(int $chatId, FileUrl $file): self
64+
{
65+
return new static($chatId, $file);
66+
}
67+
68+
public static function withFileId(int $chatId, FileId $file): self
69+
{
70+
return new static($chatId, $file);
71+
}
5672
}

0 commit comments

Comments
 (0)