Skip to content

Commit

Permalink
Add support to set custom file URL.
Browse files Browse the repository at this point in the history
  • Loading branch information
irazasyed committed May 16, 2023
1 parent 2db8a86 commit 3a449a9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function __construct(array $config = [])
$this->api = new Api($this->config('token'));
$this->setHttpClientHandler($this->config('global.http.client', GuzzleHttpClient::class));
$this->api->setBaseApiUrl($this->config('global.http.api_url', 'https://api.telegram.org'));
$this->api->withFileUrl($this->config('global.http.file_url', ''));
$this->api->setHttpClientConfig($this->config('global.http.config', []));
$this->api->setAsyncRequest($this->config('global.http.async', false));

Expand Down
17 changes: 15 additions & 2 deletions src/Http/TelegramClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class TelegramClient

/** @var string Telegram Bot API URL. */
private string $baseApiUrl = 'https://api.telegram.org';

private string $fileUrl = '{BASE_API_URL}/file/bot{TOKEN}/{FILE_PATH}';
/** @var HttpClientInterface HTTP Client. */
private HttpClientInterface $httpClientHandler;

Expand Down Expand Up @@ -127,6 +127,15 @@ public function getLastResponse(): ?TelegramResponse
return $this->lastResponse;
}

public function withFileUrl(string $fileUrl): self
{
if ($fileUrl !== '') {
$this->fileUrl = $fileUrl;
}

return $this;
}

/**
* Get File URL.
*
Expand All @@ -135,7 +144,11 @@ public function getLastResponse(): ?TelegramResponse
*/
public function getFileUrl(string $path = null): string
{
return sprintf('%s/file/bot%s/%s', $this->baseApiUrl, $this->getToken(), $path);
return str_replace(
['{BASE_API_URL}', '{TOKEN}', '{FILE_PATH}'],
[$this->baseApiUrl, $this->getToken(), $path],
$this->fileUrl
);
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Traits/HasToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public function getToken(): string
* Set the bot token.
*
* @param string $token The bot token.
* @return $this
*/
public function setToken(string $token): self
{
Expand Down
1 change: 1 addition & 0 deletions src/Traits/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* @method self setToken(string $token) Set the bot token.
* @method string getBaseApiUrl() Get the Base API URL.
* @method self setBaseApiUrl(string $baseApiUrl) Set the Base API URL.
* @method self withFileUrl(string $fileUrl) Set the File URL.
* @method string getFileUrl(string $path = null) Get File URL.
* @method bool isAsyncRequest() Check if this is an asynchronous request (non-blocking).
* @method self setAsyncRequest(bool $isAsyncRequest) Make this request asynchronous (non-blocking).
Expand Down

0 comments on commit 3a449a9

Please sign in to comment.