diff --git a/src/Telegram/Bot.php b/src/Telegram/Bot.php index d9d6669..97e37d9 100644 --- a/src/Telegram/Bot.php +++ b/src/Telegram/Bot.php @@ -23,7 +23,8 @@ class Bot private string $apiURL; private bool $payload; - public const DEFAULT_API_URL = "https://api.telegram.org/bot"; + public const DEFAULT_API_URL = "https://api.telegram.org/"; + public /** * Bot constructor. @@ -46,6 +47,15 @@ public function __construct( $this->asPayload($replyWithPayload); } + /** + * Gets the api url. + * + * @return string The url. + */ + public function getApiUrl(): string { + return $this->apiURL; + } + /** * Checks if the provided token is valid. * @@ -89,6 +99,26 @@ protected function replyAsPayload(string $method, array|object|null $arguments = fastcgi_finish_request(); } + /** + * Download a file from the server. + * + * @param string $path The file_path given by /getFile. + * @param string $destination The destination of the file to be downloaded. + * @param int $timeout The request timeout. + * + * @return bool Wheter the download was successfull or not. + */ + public function downloadFile(string $path, string $destination, $timeout = 10) { + try { + $client = new GuzzleClient(['timeout' => $timeout]); + $response = $client->request('GET', $this->apiURL . 'file/bot' . $this->token . "/$path"); + + return (bool)@file_put_contents($destination, $response->getBody()); + } catch(RequestException $e) { + return false; + } + } + /** * Sends a request to the Telegram API. * @@ -98,11 +128,11 @@ protected function replyAsPayload(string $method, array|object|null $arguments = * * @return TelegramResponse The response from the Telegram API or null on RequestException. * - * @throws TelegramException If an error occurs during the request (in non-production mode). + * @throws TelegramException If an error occurs during the request. */ protected function sendRequest(string $method, array|object|null $arguments = null, $timeout = 10): TelegramResponse { - $telegramUrl = $this->apiURL . $this->token . "/$method"; + $telegramUrl = $this->apiURL . 'bot' . $this->token . "/$method"; $client = new GuzzleClient(['timeout' => $timeout]); try {