Skip to content

Commit 87789c2

Browse files
committed
Fix file downloader
1 parent d0b3a2d commit 87789c2

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

src/Exceptions/TelegramSDKException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,14 @@ public static function tokenNotProvided($tokenEnvName): self
1616
{
1717
return new static('Required "token" not supplied in config and could not find fallback environment variable '.$tokenEnvName.'');
1818
}
19+
20+
/**
21+
* Thrown when file download fails.
22+
*
23+
* @return static
24+
*/
25+
public static function fileDownloadFailed(string $reason, string $url = null): self
26+
{
27+
return new static($reason.': Failed to Download File '.$url);
28+
}
1929
}

src/Laravel/config/telegram.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
| If you'd like to use a custom Base Bot Url.
9191
| Should be a local bot api endpoint or a proxy to the telegram api endpoint
9292
|
93-
| Default: https://api.telegram.org/bot
93+
| Default: https://api.telegram.org
9494
|
9595
*/
9696
'base_bot_url' => null,

src/TelegramClient.php

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
use Psr\Http\Message\ResponseInterface;
77
use Telegram\Bot\HttpClients\GuzzleHttpClient;
88
use Telegram\Bot\HttpClients\HttpClientInterface;
9+
use Telegram\Bot\Exceptions\TelegramSDKException;
910

1011
final class TelegramClient
1112
{
1213
/**
1314
* @var string
1415
*/
15-
public const BASE_BOT_URL = 'https://api.telegram.org/bot';
16+
public const BASE_BOT_URL = 'https://api.telegram.org';
17+
private string $fileUrl = '{BASE_API_URL}/file/bot{TOKEN}/{FILE_PATH}';
1618

1719
private HttpClientInterface $httpClientHandler;
1820

@@ -55,9 +57,56 @@ public function sendRequest(TelegramRequest $request): TelegramResponse
5557
return $response;
5658
}
5759

60+
/**
61+
* Get File URL.
62+
*/
63+
public function getFileUrl(string $path, TelegramRequest $request): string
64+
{
65+
return str_replace(
66+
['{BASE_API_URL}', '{TOKEN}', '{FILE_PATH}'],
67+
[$this->baseBotUrl, $request->getAccessToken(), $path],
68+
$this->fileUrl
69+
);
70+
}
71+
72+
/**
73+
* Download file from Telegram server for given file path.
74+
*
75+
* @param string $filePath File path on Telegram server.
76+
* @param string $filename Download path to save file.
77+
*
78+
* @throws TelegramSDKException
79+
*/
80+
public function download(string $filePath, string $filename, TelegramRequest $request): string
81+
{
82+
$fileDir = dirname($filename);
83+
84+
// Ensure dir is created.
85+
if (! @mkdir($fileDir, 0755, true) && ! is_dir($fileDir)) {
86+
throw TelegramSDKException::fileDownloadFailed('Directory '.$fileDir.' can\'t be created');
87+
}
88+
89+
$response = $this->httpClientHandler
90+
->setTimeOut($request->getTimeOut())
91+
->setConnectTimeOut($request->getConnectTimeOut())
92+
->send(
93+
$url = $this->getFileUrl($filePath, $request),
94+
$request->getMethod(),
95+
$request->getHeaders(),
96+
['sink' => $filename],
97+
$request->isAsyncRequest(),
98+
);
99+
100+
if ($response->getStatusCode() !== 200) {
101+
throw TelegramSDKException::fileDownloadFailed($response->getReasonPhrase(), $url);
102+
}
103+
104+
return $filename;
105+
}
106+
58107
public function prepareRequest(TelegramRequest $request): array
59108
{
60-
$url = $this->baseBotUrl.$request->getAccessToken().'/'.$request->getEndpoint();
109+
$url = $this->baseBotUrl.'/bot'.$request->getAccessToken().'/'.$request->getEndpoint();
61110

62111
return [$url, $request->getMethod(), $request->getHeaders(), $request->isAsyncRequest()];
63112
}

src/Traits/Http.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public function setHttpClientHandler(HttpClientInterface $httpClientHandler): se
6363

6464
public function setBaseBotUrl(string $baseBotUrl): self
6565
{
66-
$this->baseBotUrl = $baseBotUrl;
66+
$baseBotUrl = str_replace('/bot', '', $baseBotUrl);
67+
$this->baseBotUrl = rtrim($baseBotUrl, '/');
6768

6869
return $this;
6970
}
@@ -110,7 +111,9 @@ public function downloadFile(File|BaseObject|string $file, string $filename): st
110111
$filename .= DIRECTORY_SEPARATOR.($originalFilename ?: basename($file->file_path));
111112
}
112113

113-
return $this->getClient()->download($file->file_path, $filename);
114+
$telegramRequest = $this->resolveTelegramRequest('GET', '');
115+
116+
return $this->getClient()->download($file->file_path, $filename, $telegramRequest);
114117
}
115118

116119
/**

0 commit comments

Comments
 (0)