@@ -23,7 +23,8 @@ class Bot
23
23
private string $ apiURL ;
24
24
private bool $ payload ;
25
25
26
- public const DEFAULT_API_URL = "https://api.telegram.org/bot " ;
26
+ public const DEFAULT_API_URL = "https://api.telegram.org/ " ;
27
+ public
27
28
28
29
/**
29
30
* Bot constructor.
@@ -46,6 +47,15 @@ public function __construct(
46
47
$ this ->asPayload ($ replyWithPayload );
47
48
}
48
49
50
+ /**
51
+ * Gets the api url.
52
+ *
53
+ * @return string The url.
54
+ */
55
+ public function getApiUrl (): string {
56
+ return $ this ->apiURL ;
57
+ }
58
+
49
59
/**
50
60
* Checks if the provided token is valid.
51
61
*
@@ -89,6 +99,26 @@ protected function replyAsPayload(string $method, array|object|null $arguments =
89
99
fastcgi_finish_request ();
90
100
}
91
101
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
+
92
122
/**
93
123
* Sends a request to the Telegram API.
94
124
*
@@ -98,11 +128,11 @@ protected function replyAsPayload(string $method, array|object|null $arguments =
98
128
*
99
129
* @return TelegramResponse The response from the Telegram API or null on RequestException.
100
130
*
101
- * @throws TelegramException If an error occurs during the request (in non-production mode) .
131
+ * @throws TelegramException If an error occurs during the request.
102
132
*/
103
133
protected function sendRequest (string $ method , array |object |null $ arguments = null , $ timeout = 10 ): TelegramResponse
104
134
{
105
- $ telegramUrl = $ this ->apiURL . $ this ->token . "/ $ method " ;
135
+ $ telegramUrl = $ this ->apiURL . ' bot ' . $ this ->token . "/ $ method " ;
106
136
$ client = new GuzzleClient (['timeout ' => $ timeout ]);
107
137
108
138
try {
0 commit comments