Skip to content

Commit

Permalink
Refactor and Revert Base Bot URL.
Browse files Browse the repository at this point in the history
  • Loading branch information
irazasyed committed May 28, 2023
1 parent 2d0c0e5 commit 8a36eef
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Laravel/config/telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
| If you'd like to use a custom Base Bot Url.
| Should be a local bot api endpoint or a proxy to the telegram api endpoint
|
| Default: https://api.telegram.org
| Default: https://api.telegram.org/bot
|
*/
'base_bot_url' => null,
Expand Down
22 changes: 11 additions & 11 deletions src/TelegramClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@

final class TelegramClient
{
/**
* @var string
*/
public const BASE_BOT_URL = 'https://api.telegram.org';

private string $fileUrl = '{BASE_API_URL}/file/bot{TOKEN}/{FILE_PATH}';
public const BASE_API_URL = 'https://api.telegram.org';
public const BASE_BOT_URL = self::BASE_API_URL.'/bot';
private string $fileUrl = '{BASE_FILE_URL}/file/bot{TOKEN}/{FILE_PATH}';

private HttpClientInterface $httpClientHandler;

private string $baseBotUrl;
private string $baseFileUrl;

public function __construct(HttpClientInterface $httpClientHandler = null, string $baseBotUrl = null)
public function __construct(?HttpClientInterface $httpClientHandler = null, ?string $baseBotUrl = null)
{
$this->httpClientHandler = $httpClientHandler ?? new GuzzleHttpClient();

$this->baseBotUrl = $baseBotUrl ?? self::BASE_BOT_URL;
$this->baseFileUrl = $baseBotUrl ?? self::BASE_API_URL;

This comment has been minimized.

Copy link
@ariaieboy

ariaieboy May 28, 2023

Contributor

The base file URL is wrong.
$baseBotUrl = 'https://example.com/bot';
so you should set https://example.com as the base file URL;
This code must work:

$this->baseFileUrl = !is_null($baseBotUrl)?str_replace('/bot','',$baseBotUrl):self::BASE_API_URL;

This comment has been minimized.

Copy link
@irazasyed

irazasyed May 28, 2023

Author Owner

Try this one 31a71ae

}

public function getHttpClientHandler(): HttpClientInterface
Expand Down Expand Up @@ -64,8 +64,8 @@ public function sendRequest(TelegramRequest $request): TelegramResponse
public function getFileUrl(string $path, TelegramRequest $request): string
{
return str_replace(
['{BASE_API_URL}', '{TOKEN}', '{FILE_PATH}'],
[$this->baseBotUrl, $request->getAccessToken(), $path],
['{BASE_FILE_URL}', '{TOKEN}', '{FILE_PATH}'],
[$this->baseFileUrl, $request->getAccessToken(), $path],
$this->fileUrl
);
}
Expand Down Expand Up @@ -95,7 +95,7 @@ public function download(string $filePath, string $filename, TelegramRequest $re
$request->getMethod(),
$request->getHeaders(),
['sink' => $filename],
$request->isAsyncRequest(),
$request->isAsyncRequest()
);

if ($response->getStatusCode() !== 200) {
Expand All @@ -107,7 +107,7 @@ public function download(string $filePath, string $filename, TelegramRequest $re

public function prepareRequest(TelegramRequest $request): array
{
$url = $this->baseBotUrl.'/bot'.$request->getAccessToken().'/'.$request->getEndpoint();
$url = $this->baseBotUrl.$request->getAccessToken().'/'.$request->getEndpoint();

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

0 comments on commit 8a36eef

Please sign in to comment.