Last active
April 19, 2023 22:23
-
-
Save sh-sh-dev/e57263da1bdc852299843c864bae3892 to your computer and use it in GitHub Desktop.
Get Telegram files direct link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Configuration | |
$token = '123:abc'; | |
$adminId = 123456; | |
$webhookSecret = 'random-long-text'; | |
// Security | |
if ($_SERVER['REQUEST_METHOD'] !== 'POST') | |
die('method'); | |
else if ($_GET['secret'] !== $webhookSecret) | |
die('secret'); | |
// Update | |
$update = json_decode(file_get_contents('php://input'))->message; | |
$chatId = $update->chat->id; | |
$messageId = $update->message_id; | |
$type = detectUpdateType($update); | |
// Main Process | |
if ((string)$chatId !== (string)$adminId) | |
die(); | |
$fileDetails = getFileDetails($type, $update); | |
$getFile = bot('getFile', ['file_id' => $fileDetails->file_id], true); | |
if (!$getFile->ok) | |
die('error'); | |
$filePath = $getFile->result->file_path; | |
$fileExtension = explode('.', $filePath)[1]; | |
$fileLink = 'https://api.telegram.org/file/bot' . $token . '/' . $filePath; | |
$localFilePath = 'files/' . $fileDetails->file_unique_id . '.' . $fileExtension; | |
bot('sendMessage', [ | |
'chat_id' => $chatId, | |
'text' => print_r([ | |
$type, | |
$update->$type, | |
$getFile, | |
$fileDetails, | |
$localFilePath, | |
$fileExtension, | |
$fileLink, | |
$localFilePath | |
]) | |
]); | |
bot('sendMessage', [ | |
'chat_id' => $chatId, | |
'text' => $fileLink | |
]); | |
// Functions | |
/** | |
* Send a request to the Telegram API | |
* | |
* @param string $method | |
* @param null|array $data | |
* @param bool $return | |
* | |
* @return bool|object | |
*/ | |
function bot(string $method, array $data = null, bool $return = false) { | |
global $token; | |
$url = 'https://api.telegram.org/bot' . $token . '/' . $method; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
if (!empty($data)) | |
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); | |
$result = curl_exec($ch); | |
if (curl_error($ch)) | |
return false; | |
else | |
return $return ? json_decode($result) : null; | |
} | |
function readableFileSize(int $size) { | |
static $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | |
$step = 1024; | |
$i = 0; | |
while (($size / $step) > 0.9) { | |
$size = $size / $step; | |
$i++; | |
} | |
return round($size, 2) . $units[$i]; | |
} | |
/** | |
* Detect type of update (photo, audio etc.) | |
* | |
* @param array $update | |
* | |
* @return bool|string | |
*/ | |
function detectUpdateType(object $update) { | |
$types = ['photo', 'video', 'voice', 'document', 'audio', 'video_note', 'animation']; | |
foreach ($types as $type) { | |
if (isset($update->$type)) | |
return $type; | |
} | |
return false; | |
} | |
function getFileDetails(string $type, object $update) { | |
$result = $update->$type; | |
if ($type === 'photo') | |
$result = end($result); | |
return $result ?? null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment