Longman\TelegramBot\Request::downloadFile PHP Method

downloadFile() public static method

Download file
public static downloadFile ( Longman\TelegramBot\Entities\File $file ) : boolean
$file Longman\TelegramBot\Entities\File
return boolean
    public static function downloadFile(File $file)
    {
        $tg_file_path = $file->getFilePath();
        $file_path = self::$telegram->getDownloadPath() . '/' . $tg_file_path;
        $file_dir = dirname($file_path);
        //For safety reasons, first try to create the directory, then check that it exists.
        //This is in case some other process has created the folder in the meantime.
        if (!@mkdir($file_dir, 0755, true) && !is_dir($file_dir)) {
            throw new TelegramException('Directory ' . $file_dir . ' can\'t be created');
        }
        $debug_handle = TelegramLog::getDebugLogTempStream();
        try {
            self::$client->get('/file/bot' . self::$telegram->getApiKey() . '/' . $tg_file_path, ['debug' => $debug_handle, 'sink' => $file_path]);
            return filesize($file_path) > 0;
        } catch (RequestException $e) {
            return (string) $e->getResponse()->getBody();
        } finally {
            //Logging verbose debug output
            TelegramLog::endDebugLogTempStream("Verbose HTTP File Download Request output:\n%s\n");
        }
    }

Usage Example

 public function execute()
 {
     $update = $this->getUpdate();
     $message = $this->getMessage();
     $user_id = $message->getFrom()->getId();
     $chat_id = $message->getChat()->getId();
     $message_id = $message->getMessageId();
     $text = $message->getText(true);
     //send chat action
     Request::sendChatAction(['chat_id' => $chat_id, 'action' => 'typing']);
     $caption = 'Your Id: ' . $message->getFrom()->getId();
     $caption .= "\n" . 'Name: ' . $message->getFrom()->getFirstName() . ' ' . $message->getFrom()->getLastName();
     $caption .= "\n" . 'Username: '******'user_id' => $user_id, 'limit' => $limit, 'offset' => $offset]);
     //Check if the request isOK
     if ($ServerResponse->isOk()) {
         $UserProfilePhoto = $ServerResponse->getResult();
         $totalcount = $UserProfilePhoto->getTotalCount();
     } else {
         $totalcount = 0;
     }
     $data = [];
     $data['chat_id'] = $chat_id;
     $data['reply_to_message_id'] = $message_id;
     if ($totalcount > 0) {
         $photos = $UserProfilePhoto->getPhotos();
         //I pick the latest photo with the hight definition
         $photo = $photos[0][2];
         $file_id = $photo->getFileId();
         $data['photo'] = $file_id;
         $data['caption'] = $caption;
         $result = Request::sendPhoto($data);
         //Download the image pictures
         //Download after send message response to speedup response
         $file_id = $photo->getFileId();
         $ServerResponse = Request::getFile(['file_id' => $file_id]);
         if ($ServerResponse->isOk()) {
             Request::downloadFile($ServerResponse->getResult());
         }
     } else {
         //No Photo just send text
         $data['text'] = $caption;
         $result = Request::sendMessage($data);
     }
     return $result;
 }
All Usage Examples Of Longman\TelegramBot\Request::downloadFile