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

Update TelegramClient.php #1031

Merged
merged 2 commits into from
Mar 7, 2023
Merged
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
96 changes: 20 additions & 76 deletions src/TelegramClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,127 +8,71 @@
use Telegram\Bot\HttpClients\GuzzleHttpClient;
use Telegram\Bot\HttpClients\HttpClientInterface;

/**
* Class TelegramClient.
*/
class TelegramClient
{
/** @var string Telegram Bot API URL. */
const BASE_BOT_URL = 'https://api.telegram.org/bot';

/** @var HttpClientInterface|null HTTP Client. */
protected $httpClientHandler;
protected HttpClientInterface $httpClientHandler;

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

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

$this->baseBotUrl = $baseBotUrl;
$this->baseBotUrl = $baseBotUrl ?? static::BASE_BOT_URL;
}

/**
* Returns the HTTP client handler.
*
* @return HttpClientInterface
*/
public function getHttpClientHandler()
public function getHttpClientHandler(): HttpClientInterface
{
return $this->httpClientHandler;
}

/**
* Sets the HTTP client handler.
*/
public function setHttpClientHandler(HttpClientInterface $httpClientHandler): self
{
$this->httpClientHandler = $httpClientHandler;

return $this;
}

/**
* Send an API request and process the result.
*
*
* @throws TelegramSDKException
*/
public function sendRequest(TelegramRequest $request): TelegramResponse
{
[$url, $method, $headers, $isAsyncRequest] = $this->prepareRequest($request);

$options = $this->getOption($request, $method);
$options = $this->getOptions($request, $method);

$rawResponse = $this->getHttpClientHandler()
->setTimeOut($request->getTimeOut())
->setConnectTimeOut($request->getConnectTimeOut())
->send(
$url,
$method,
$headers,
$options,
$isAsyncRequest
);

$returnResponse = $this->getResponse($request, $rawResponse);

if ($returnResponse->isError()) {
throw $returnResponse->getThrownException();
->send($url, $method, $headers, $options, $isAsyncRequest);

$response = $this->getResponse($request, $rawResponse);

if ($response->isError()) {
throw $response->getThrownException();
}

return $returnResponse;
return $response;
}

/**
* Prepares the API request for sending to the client handler.
*/
public function prepareRequest(TelegramRequest $request): array
{
$url = $this->getBaseBotUrl().$request->getAccessToken().'/'.$request->getEndpoint();

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

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

/**
* Returns the base Bot URL.
*/
public function getBaseBotUrl(): string
{
return $this->baseBotUrl ?? static::BASE_BOT_URL;
return $this->baseBotUrl;
}

/**
* Creates response object.
*
* @param ResponseInterface|PromiseInterface $response
*/
protected function getResponse(TelegramRequest $request, $response): TelegramResponse
{
return new TelegramResponse($request, $response);
}

/**
* @param string $method
* @return array
*/
private function getOption(TelegramRequest $request, $method)
private function getOptions(TelegramRequest $request, string $method): array
{
if ($method === 'POST') {
return $request->getPostParams();
}

return ['query' => $request->getParams()];
return $method === 'POST' ? $request->getPostParams() : ['query' => $request->getParams()];
}
}