Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make BASE_BOT_URL customizable #1017

Merged
merged 3 commits into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ class Api
* @param string $token The Telegram Bot API Access Token.
* @param bool $async (Optional) Indicates if the request to Telegram will be asynchronous (non-blocking).
* @param HttpClientInterface|null $httpClientHandler (Optional) Custom HTTP Client Handler.
* @param string|null $base_bot_url (Optional) Custom base bot url.
*
* @throws TelegramSDKException
*/
public function __construct($token = null, $async = false, $httpClientHandler = null)
public function __construct($token = null, $async = false, $httpClientHandler = null, $base_bot_url = null)
{
$this->accessToken = $token ?? getenv(static::BOT_TOKEN_ENV_NAME);
$this->validateAccessToken();
Expand All @@ -60,6 +61,8 @@ public function __construct($token = null, $async = false, $httpClientHandler =
}

$this->httpClientHandler = $httpClientHandler;

$this->baseBotUrl = $base_bot_url;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/BotsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ protected function makeBot($name): Api
$telegram = new Api(
$token,
$this->getConfig('async_requests', false),
$this->getConfig('http_client_handler', null)
$this->getConfig('http_client_handler', null),
$this->getConfig('base_bot_url',null)
);

// Check if DI needs to be enabled for Commands
Expand Down
13 changes: 13 additions & 0 deletions src/Laravel/config/telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@
*/
'http_client_handler' => null,

/*
|--------------------------------------------------------------------------
| Base Bot Url [Optional]
|--------------------------------------------------------------------------
|
| 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/bot
|
*/
'base_bot_url' => null,

/*
|--------------------------------------------------------------------------
| Resolve Injected Dependencies in commands [Optional]
Expand Down
10 changes: 8 additions & 2 deletions src/TelegramClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ class TelegramClient
/** @var HttpClientInterface|null HTTP Client. */
protected $httpClientHandler;

/** @var string|null base bot url. */
protected $baseBotUrl;

/**
* Instantiates a new TelegramClient object.
*
* @param HttpClientInterface|null $httpClientHandler
* @param string|null $baseBotUrl
*/
public function __construct(HttpClientInterface $httpClientHandler = null)
public function __construct(HttpClientInterface $httpClientHandler = null, $baseBotUrl = null)
{
$this->httpClientHandler = $httpClientHandler ?? new GuzzleHttpClient();

$this->baseBotUrl = $baseBotUrl;
}

/**
Expand Down Expand Up @@ -111,7 +117,7 @@ public function prepareRequest(TelegramRequest $request): array
*/
public function getBaseBotUrl(): string
{
return static::BASE_BOT_URL;
return $this->baseBotUrl ?? static::BASE_BOT_URL;
}

/**
Expand Down
18 changes: 17 additions & 1 deletion src/Traits/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ trait Http
/** @var HttpClientInterface|null Http Client Handler */
protected $httpClientHandler = null;

/** @var string|null Base Bot Url */
protected $baseBotUrl = null;

/** @var bool Indicates if the request to Telegram will be asynchronous (non-blocking). */
protected $isAsyncRequest = false;

Expand All @@ -54,6 +57,19 @@ public function setHttpClientHandler(HttpClientInterface $httpClientHandler)
return $this;
}

/**
* Set Http Client Handler.
*
* @param string $baseBotUrl
* @return $this
*/
public function setBaseBotUrl(string $baseBotUrl)
{
$this->baseBotUrl = $baseBotUrl;

return $this;
}

/**
* Returns the TelegramClient service.
*
Expand All @@ -62,7 +78,7 @@ public function setHttpClientHandler(HttpClientInterface $httpClientHandler)
protected function getClient(): TelegramClient
{
if ($this->client === null) {
$this->client = new TelegramClient($this->httpClientHandler);
$this->client = new TelegramClient($this->httpClientHandler, $this->baseBotUrl);
}

return $this->client;
Expand Down