Skip to content

Commit 4f558a6

Browse files
committed
file uploads
1 parent 8cec959 commit 4f558a6

File tree

9 files changed

+138
-17
lines changed

9 files changed

+138
-17
lines changed

src/Bot.php

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function sendMessage(SendMessage $sendMessage): Message
6161
return $message;
6262
}
6363

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

6767
public function sendPhoto(SendPhoto $sendPhoto): ResponseInterface
@@ -98,21 +98,21 @@ public function sendDocument(SendDocument $sendDocument): Message
9898

9999
if ($doc instanceof FileId || $doc instanceof FileUrl) {
100100
$data = get_values($sendDocument);
101-
$data['document'] = (string) $doc;
101+
$data['document'] = (string)$doc;
102102

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

107-
$json = json_decode((string) $response->getBody(), true);
107+
$json = json_decode((string)$response->getBody(), true);
108108
if (isset($json['ok']) && $json['ok']) {
109109
$message = new Message();
110110
set_values($message, $json['result']);
111111

112112
return $message;
113113
}
114114

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

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

137-
$json = json_decode((string) $response->getBody(), true);
137+
$json = json_decode((string)$response->getBody(), true);
138138
if (isset($json['ok']) && $json['ok']) {
139139
$message = new Message();
140140
set_values($message, $json['result']);
141141

142142
return $message;
143143
}
144144

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

148-
throw new \LogicException(sprintf('Unexpected document: %s'.get_class($doc)));
148+
throw new \LogicException(sprintf('Unexpected document: %s' . get_class($doc)));
149149
}
150150

151151
public function sendInvoice(SendInvoice $sendInvoice)
@@ -175,7 +175,7 @@ public function editMessageText(EditMessageText $editMessageText): ?Message
175175
'json' => get_values($editMessageText),
176176
]);
177177

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

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

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

190190
public function deleteMessage(DeleteMessage $deleteMessage): bool
@@ -193,7 +193,7 @@ public function deleteMessage(DeleteMessage $deleteMessage): bool
193193
'json' => get_values($deleteMessage),
194194
]);
195195

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

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

205+
/**
206+
* @see https://core.telegram.org/bots/api#getfile
207+
*/
208+
public function getFile(GetFile $getFile): File
209+
{
210+
$response = $this->httpClient->post($this->getMethodUrl('getFile'), [
211+
'json' => get_values($getFile),
212+
]);
213+
214+
$json = json_decode((string) $response->getBody(), true);
215+
216+
if (isset($json['ok']) && $json['ok']) {
217+
$file = new File();
218+
set_values($file, $json['result']);
219+
220+
if ($path = $file->getFilePath()) {
221+
$file->setFileUrl(sprintf('https://api.telegram.org/file/bot%s/%s', $this->token, $path));
222+
}
223+
224+
return $file;
225+
}
226+
227+
throw new \LogicException('Unexpected response: ' . (string) $response->getBody());
228+
}
229+
205230
private function getMethodUrl(string $method): string
206231
{
207232
return sprintf('https://api.telegram.org/bot%s/%s', $this->token, $method);

src/Document.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
namespace Formapro\TelegramBot;
3+
4+
use function Formapro\Values\get_value;
5+
6+
/**
7+
* @see https://core.telegram.org/bots/api#document
8+
*/
9+
class Document
10+
{
11+
private $values = [];
12+
13+
public function getFileId(): string
14+
{
15+
return get_value($this, 'file_id');
16+
}
17+
18+
public function getFileName(): ?string
19+
{
20+
return get_value($this, 'file_name');
21+
}
22+
23+
public function getFileSize(): ?int
24+
{
25+
return get_value($this, 'file_size');
26+
}
27+
28+
public function getMimeType(): ?string
29+
{
30+
return get_value($this, 'mime_type');
31+
}
32+
}

src/File.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,39 @@
11
<?php
22
namespace Formapro\TelegramBot;
33

4+
use function Formapro\Values\get_value;
5+
46
/**
5-
* @see https://core.telegram.org/bots/api#sending-files
7+
* @see https://core.telegram.org/bots/api#file
68
*/
7-
interface File
9+
class File
810
{
11+
private $values = [];
12+
13+
private $fileUrl;
14+
15+
public function getFileId(): string
16+
{
17+
return get_value($this, 'file_id');
18+
}
19+
20+
public function getFileSize(): ?int
21+
{
22+
return get_value($this, 'file_size');
23+
}
924

25+
public function getFilePath(): ?string
26+
{
27+
return get_value($this, 'file_path');
28+
}
29+
30+
public function setFileUrl(?string $fileUrl): void
31+
{
32+
$this->fileUrl = $fileUrl;
33+
}
34+
35+
public function getFileUrl(): ?string
36+
{
37+
return $this->fileUrl;
38+
}
1039
}

src/FileId.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/**
55
* @see https://core.telegram.org/bots/api#sending-files
66
*/
7-
class FileId implements File
7+
class FileId
88
{
99
private $fileId;
1010

src/FileUrl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/**
55
* @see https://core.telegram.org/bots/api#sending-files
66
*/
7-
class FileUrl implements File
7+
class FileUrl
88
{
99
private $url;
1010

src/GetFile.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Formapro\TelegramBot;
4+
5+
use function Formapro\Values\get_value;
6+
use function Formapro\Values\get_values;
7+
use function Formapro\Values\set_value;
8+
9+
/**
10+
* @see https://core.telegram.org/bots/api#getfile
11+
*/
12+
class GetFile
13+
{
14+
private $values = [];
15+
16+
public function __construct(string $fileId)
17+
{
18+
set_value($this, 'file_id', $fileId);
19+
}
20+
}

src/InputFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/**
55
* @see https://core.telegram.org/bots/api#sending-files
66
*/
7-
class InputFile implements File
7+
class InputFile
88
{
99
private $fileName;
1010

src/Message.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public function getContact(): ?Contact
3636
return get_object($this, 'contact', Contact::class);
3737
}
3838

39+
public function getDocument(): ?Document
40+
{
41+
return get_object($this, 'document', Document::class);
42+
}
43+
3944
/**
4045
* It can be used only after sending invoice and successful paying for it.
4146
*

src/SendDocument.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@ class SendDocument
1111
{
1212
private $values = [];
1313

14+
/**
15+
* @var FileId|FileUrl|InputFile
16+
*/
1417
private $document;
1518

16-
private function __construct(int $chatId, File $document)
19+
/**
20+
* @param int $chatId
21+
* @param FileId|FileUrl|InputFile $document
22+
*/
23+
private function __construct(int $chatId, $document)
1724
{
1825
set_value($this, 'chat_id', $chatId);
1926

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

28-
public function getDocument(): File
35+
/**
36+
* @return FileId|FileUrl|InputFile
37+
*/
38+
public function getDocument()
2939
{
3040
return $this->document;
3141
}

0 commit comments

Comments
 (0)