Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit d816bfd

Browse files
authored
Merge pull request #138 from DJTommek/pr/file-upload-fix
Fixed uploading documents and thumbnails
2 parents 75cb55d + 5241ebd commit d816bfd

File tree

6 files changed

+51
-10
lines changed

6 files changed

+51
-10
lines changed
14.6 KB
Loading

examples/send-audio.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
$sendAudio->chat_id = A_USER_CHAT_ID;
1818
$sendAudio->audio = new InputFile('binary-test-data/ICQ-uh-oh.mp3');
1919
$sendAudio->title = 'The famous ICQ new message alert';
20+
$sendAudio->thumb = new InputFile(__DIR__ . '/binary-test-data/logo-php7-telegram-bot-api-thumbnail.jpg');
2021

2122
$promise = $tgLog->performApiRequest($sendAudio);
2223

examples/send-document.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,40 @@
66

77
use React\EventLoop\Factory;
88
use unreal4u\TelegramAPI\HttpClientRequestHandler;
9+
use unreal4u\TelegramAPI\Telegram\Methods\GetFile;
910
use unreal4u\TelegramAPI\Telegram\Methods\SendDocument;
1011
use unreal4u\TelegramAPI\Telegram\Types\Custom\InputFile;
1112
use unreal4u\TelegramAPI\TgLog;
13+
use function Clue\React\Block\await;
1214

1315
$loop = Factory::create();
1416
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));
1517

1618
$sendDocument = new SendDocument();
1719
$sendDocument->chat_id = A_USER_CHAT_ID;
1820
$sendDocument->document = new InputFile(__FILE__);
21+
$sendDocument->thumb = new InputFile(__DIR__ . '/binary-test-data/logo-php7-telegram-bot-api-thumbnail.jpg');
1922

2023
$promise = $tgLog->performApiRequest($sendDocument);
2124

2225
$promise->then(
23-
function ($response) {
26+
function ($response) use ($tgLog, $loop) {
2427
echo '<pre>';
2528
var_dump($response);
2629
echo '</pre>';
30+
/** @var $response \unreal4u\TelegramAPI\Telegram\Types\Message */
31+
32+
// Load uploaded video and generate URL to download using bot token
33+
$getFileVideo = new GetFile();
34+
$getFileVideo->file_id = $response->document->file_id;
35+
$fileVideo = await($tgLog->performApiRequest($getFileVideo), $loop);
36+
var_dump('Document url: ' . $tgLog->fileUrl($fileVideo));
37+
38+
// Load uploaded thumbnail and generate URL to download using bot token
39+
$getFileThumb = new GetFile();
40+
$getFileThumb->file_id = $response->document->thumb->file_id;
41+
$fileThumb = await($tgLog->performApiRequest($getFileThumb), $loop);
42+
var_dump('Thumbnail url: ' . $tgLog->fileUrl($fileThumb));
2743
},
2844
function (\Exception $exception) {
2945
// Onoes, an exception occurred...

src/InternalFunctionality/PostOptionsConstructor.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ private function checkIsMultipart(TelegramMethods $method): array
9292

9393
$return = [];
9494

95-
foreach ($method as $key => $value) {
96-
if ($method->hasLocalFiles() === true) {
97-
$this->logger->debug('About to send a file, so changing request to use multi-part instead');
98-
// If we are about to send a file, we must use the multipart/form-data way
99-
$this->formType = 'multipart/form-data';
100-
foreach ($method->getLocalFiles() as $identifier => $localFile) {
95+
if ($method->hasLocalFiles() === true) {
96+
$this->logger->debug('About to send a file, so changing request to use multi-part instead');
97+
// If we are about to send a file, we must use the multipart/form-data way
98+
$this->formType = 'multipart/form-data';
99+
foreach ($method->getLocalFiles() as $identifier => $localFile) {
100+
if ($localFile instanceof InputFile) {
101101
$return[$identifier . '~' . $this->numberOfLocalFiles] = [
102102
'id' => $this->numberOfLocalFiles,
103103
'filename' => basename($localFile->path),

src/Telegram/Methods/SendAudio.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ class SendAudio extends TelegramMethods
4141
*/
4242
public $audio = '';
4343

44+
/**
45+
* Optional. Thumbnail of the file sent; can be ignored if thumbnail generation for the file is supported
46+
* server-side. The thumbnail should be in JPEG format and less than 200 kB in size. A thumbnail's width and height
47+
* should not exceed 320. Ignored if the file is not uploaded using multipart/form-data. Thumbnails can't be reused
48+
* and can be only uploaded as a new file, so you can pass “attach://<file_attach_name>” if the thumbnail was
49+
* uploaded using multipart/form-data under <file_attach_name>.
50+
* @var string|InputFile
51+
*/
52+
public $thumb;
53+
4454
/**
4555
* Optional. Audio caption, 0-200 characters
4656
* @var string
@@ -108,6 +118,9 @@ public function hasLocalFiles(): bool
108118

109119
public function getLocalFiles(): Generator
110120
{
111-
yield 'audio' => $this->audio;
121+
yield from [
122+
'audio' => $this->audio,
123+
'thumb' => $this->thumb,
124+
];
112125
}
113126
}

src/TgLog.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ public function performApiRequest(TelegramMethods $method): PromiseInterface
102102
*/
103103
public function downloadFile(File $file): PromiseInterface
104104
{
105-
$url = 'https://api.telegram.org/file/bot' . $this->botToken . '/' . $file->file_path;
105+
$fileUrl = $this->fileUrl($file);
106106
$this->logger->debug('About to perform request to begin downloading file');
107107

108-
return $this->requestHandler->get($url)->then(
108+
return $this->requestHandler->get($fileUrl)->then(
109109
function (TelegramResponse $rawData) {
110110
return new TelegramDocument($rawData);
111111
}
@@ -156,4 +156,15 @@ protected function composeApiMethodUrl(TelegramMethods $call): string
156156

157157
return $this->apiUrl . $this->methodName;
158158
}
159+
160+
/**
161+
* Generate URL for file download using bot token
162+
*
163+
* @param File $file
164+
* @return string
165+
*/
166+
public function fileUrl(File $file): string
167+
{
168+
return 'https://api.telegram.org/file/bot' . $this->botToken . '/' . $file->file_path;
169+
}
159170
}

0 commit comments

Comments
 (0)