Skip to content

Commit 21a0a8a

Browse files
add File download feature
1 parent c611ed4 commit 21a0a8a

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

src/Bot.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,61 @@ public static function sendRequest(string $method, array $parameters = [], ?stri
191191
return $response;
192192
}
193193

194+
/**
195+
* downloadFile function
196+
*
197+
* @param string $fileId
198+
* @param string $localFilePath
199+
* @return bool true if file downloaded
200+
*/
201+
public static function downloadFile(string $filePath, string $localFilePath)
202+
{
203+
$fileSource = self::$config->botApiServerFileUrl . self::$token . '/' . $filePath;
204+
205+
$file = fopen($fileSource, 'rb');
206+
$localFile = fopen($localFilePath, 'wb');
207+
208+
if (!$file || !$localFile) {
209+
return false;
210+
}
211+
212+
while (!feof($file)) {
213+
214+
if (fwrite($localFile, fread($file, 8192), 8192) === FALSE) {
215+
return false;
216+
}
217+
218+
}
219+
220+
fclose($file);
221+
fclose($localFile);
222+
223+
return true;
224+
}
225+
226+
/**
227+
* downloadFileByFileId function
228+
*
229+
* @param string $fileId
230+
* @param string $localFilePath
231+
* @return bool|Response true if file downloaded
232+
*/
233+
public static function downloadFileByFileId(string $fileId, string $localFilePath)
234+
{
235+
236+
$file = self::getFile([
237+
'file_id' => $fileId,
238+
]);
239+
240+
if ($file->isOk()) {
241+
242+
$filePath = $file->getResult()->filePath;
243+
return self::downloadFile($filePath, $localFilePath);
244+
}
245+
246+
return $file;
247+
}
248+
194249
/**
195250
* isTelegramIp function
196251
* Check if a given ip is in a telegram ip range

0 commit comments

Comments
 (0)