Skip to content

Commit 93442b2

Browse files
committed
Add getApiUrl() and downloadFile() methods
1 parent fd7085f commit 93442b2

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

src/Telegram/Bot.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class Bot
2323
private string $apiURL;
2424
private bool $payload;
2525

26-
public const DEFAULT_API_URL = "https://api.telegram.org/bot";
26+
public const DEFAULT_API_URL = "https://api.telegram.org/";
27+
public
2728

2829
/**
2930
* Bot constructor.
@@ -46,6 +47,15 @@ public function __construct(
4647
$this->asPayload($replyWithPayload);
4748
}
4849

50+
/**
51+
* Gets the api url.
52+
*
53+
* @return string The url.
54+
*/
55+
public function getApiUrl(): string {
56+
return $this->apiURL;
57+
}
58+
4959
/**
5060
* Checks if the provided token is valid.
5161
*
@@ -89,6 +99,26 @@ protected function replyAsPayload(string $method, array|object|null $arguments =
8999
fastcgi_finish_request();
90100
}
91101

102+
/**
103+
* Download a file from the server.
104+
*
105+
* @param string $path The file_path given by /getFile.
106+
* @param string $destination The destination of the file to be downloaded.
107+
* @param int $timeout The request timeout.
108+
*
109+
* @return bool Wheter the download was successfull or not.
110+
*/
111+
public function downloadFile(string $path, string $destination, $timeout = 10) {
112+
try {
113+
$client = new GuzzleClient(['timeout' => $timeout]);
114+
$response = $client->request('GET', $this->apiURL . 'file/bot' . $this->token . "/$path");
115+
116+
return (bool)@file_put_contents($destination, $response->getBody());
117+
} catch(RequestException $e) {
118+
return false;
119+
}
120+
}
121+
92122
/**
93123
* Sends a request to the Telegram API.
94124
*
@@ -98,11 +128,11 @@ protected function replyAsPayload(string $method, array|object|null $arguments =
98128
*
99129
* @return TelegramResponse The response from the Telegram API or null on RequestException.
100130
*
101-
* @throws TelegramException If an error occurs during the request (in non-production mode).
131+
* @throws TelegramException If an error occurs during the request.
102132
*/
103133
protected function sendRequest(string $method, array|object|null $arguments = null, $timeout = 10): TelegramResponse
104134
{
105-
$telegramUrl = $this->apiURL . $this->token . "/$method";
135+
$telegramUrl = $this->apiURL . 'bot' . $this->token . "/$method";
106136
$client = new GuzzleClient(['timeout' => $timeout]);
107137

108138
try {

0 commit comments

Comments
 (0)