Skip to content

Commit

Permalink
fix: Download telegram files
Browse files Browse the repository at this point in the history
Add method Core::downloadFile
  • Loading branch information
Mateodioev committed Dec 2, 2023
1 parent 6fb9666 commit e1b70ea
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 27 deletions.
27 changes: 9 additions & 18 deletions src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Mateodioev\Bots\Telegram\Config\Types as TypesConfig;
use Mateodioev\Bots\Telegram\Http\{AsyncClient, SyncClient, Request as HttpClient, HttpException};
use Mateodioev\Bots\Telegram\Types\{Response, Error};
use Mateodioev\Bots\Telegram\Types\{File, Response, Error};
use Mateodioev\Bots\Telegram\Exception\{TelegramParamException, TelegramApiException};
use Mateodioev\Bots\Telegram\Interfaces\{MethodInterface, TelegramInterface, TypesInterface};
use Mateodioev\Request\{Request, ResponseException};
Expand Down Expand Up @@ -182,27 +182,18 @@ private function parseRequestResult(MethodInterface $method): mixed
/**
* Download file sent to the bot
*
* @param string $file_path Use `$this->request(Method::create(['file_id' => 'bot-file_id'], 'getFile')->setReturnType(File::class))` to get file path
* @param string $filePath Use `$this->request(Method::create(['file_id' => 'bot-file_id'], 'getFile')->setReturnType(File::class))` to get file path
* @param string $destination Document name to save the file
*/
public function download(string $file_path, string $destination, int $timeout = 30): bool
public function download(string $filePath, string $destination): bool
{
$fh = fopen($destination, 'w');

// TODO: change this
$req = Request::GET($this->file_link)
->addOpt(CURLOPT_FILE, $fh)
->addOpt(CURLOPT_TIMEOUT, $timeout);

try {
$res = $req->Run($file_path);
return $this->getClient()->new($this->file_link)
->download($filePath, $destination);
}

return ($res->toJson(true) !== $res);
} catch (RequestException | ResponseException) {
return false;
} finally {
fclose($fh);
}
public function downloadFile(File $file, string $destination): bool
{
return $this->download($file->file_path, $destination);
}

/**
Expand Down
19 changes: 13 additions & 6 deletions src/Http/AsyncClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ public function setTimeout(int $timeout): static
return $this;
}

private function executeRequest(): \Amp\Http\Client\Response
private function executeRequest(AsyncRequest $request): \Amp\Http\Client\Response
{
try {
return $this->client->request($this->request);
return $this->client->request($request);
} catch (Throwable $e) {
throw new HttpException($e->getMessage(), $e->getCode(), $e);
}
Expand All @@ -96,7 +96,11 @@ private function executeRequest(): \Amp\Http\Client\Response
public function run(): Response
{
try {
return new Response($this->executeRequest()->getBody()->buffer());
return new Response(
$this->executeRequest($this->request)
->getBody()
->buffer()
);
} catch (BufferException|StreamException $e) {
throw new HttpException($e->getMessage(), $e->getCode(), $e);
}
Expand All @@ -109,13 +113,16 @@ public function isAsync(): bool

public function download(string $path, string $destination): bool
{
$this->request->setUri(
$request = clone $this->request;
$request->setUri(
$this->request
->getUri()
->withPath($path)
. $path
);

$response = $this->executeRequest();
$request->setMethod('GET');
$response = $this->executeRequest($request);

try {
$file = openFile($destination, 'w');
} catch (FilesystemException $e) {
Expand Down
2 changes: 1 addition & 1 deletion src/Http/NativeCurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function download(string $path, string $destination): bool
$file = fopen($destination, 'w');
$this->request->addOpt(CURLOPT_FILE, $file);
try {
$this->request->run($path);
$this->request->setMethod(Methods::GET->value())->run($path);
fclose($file);
return true;
} catch (HttpException) {
Expand Down
13 changes: 12 additions & 1 deletion src/Interfaces/TelegramInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@

namespace Mateodioev\Bots\Telegram\Interfaces;

use Mateodioev\Bots\Telegram\Types\File;
use stdClass;

interface TelegramInterface
{
public function request(MethodInterface $method): TypesInterface|stdClass|array;

public function download(string $file_path, string $destination, int $timeout = 30): bool;
/**
* Download telegram file
* @param string $filePath File to download
* @param string $destination File to save
*/
public function download(string $filePath, string $destination): bool;

/**
* Download telegram file
*/
public function downloadFile(File $file, string $destination): bool;

public function getApiLink(): string;

Expand Down
2 changes: 1 addition & 1 deletion src/Methods/availableMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ public function getUserProfilePhotos(int $userID, array $params = []): TypesInte
* @see https://core.telegram.org/bots/api#getfile
* @return File
*/
public function getFile(int $fileID): TypesInterface
public function getFile(string $fileID): TypesInterface
{
return $this->request(
Method::create(['file_id' => $fileID])
Expand Down

0 comments on commit e1b70ea

Please sign in to comment.