skip to Main Content

Is it possible to receive images (and store it on the server) which was sent by any user to the bot?

If a image was sent, I receive this JSON post. This is a link to the Telegram Bot-API description. I don’t know if it’s possible to receive the whole image or not:




  1. Telegram support download file now with getFile:

    You can see it in the api documentation:

    Login or Signup to reply.
  2. It’s possible to download the image from Telegram server.
    Do this:
    1. Get the file using the getFile api

    //Telegram link
    $telegram_link = '' . $this->tg_configs['api_key'] . '/getFile?file_id=' . $photo['file_id'];

    2. Get the file path
    //Create guzzle client
    $guzzle_client = new GuzzleClient();

    //Call telegram
    $request = $guzzle_client->get($telegram_link);
    //Decode json
    $json_response = json_decode($request->getBody(), true);
    if ($json_response['ok'] == 'true') {
        //Telegram file link
        $telegram_file_link = '' . $this->tg_configs['api_key'] . '/' . $json_response['result']['file_path'];

    3. If using PHP use Intervention/Image to download the image and save it on your server.

    //Build upload path
    $upload_path = public_path() . Config::get('media::media.uploadPath');
    //Get image
    $image = $thumbnail = InterventionImage::make($telegram_file_link);
    //Get mime
    $mime = $image->mime();
    if ($mime == 'image/jpeg') {
        $extension = '.jpg';
    } elseif ($mime == 'image/png') {
        $extension = '.png';
    } elseif ($mime == 'image/gif') {
        $extension = '.gif';
    } else {
        $extension = '';
    }//E# if else statement
    //Resize images
    $image->resize(Config::get('media::media.mainWidth'), Config::get('media::media.mainHeight'));
    $thumbnail->resize(Config::get('media::media.thumbnailWidth'), Config::get('media::media.thumbnailHeight'));
    //Build media name
    $media_name = Str::random(Config::get('media::media.mediaNameLength')) . $extension;
    //Save images
    $image->save($upload_path . '/' . $media_name);
    $thumbnail->save($upload_path . '/thumbnails/' . $media_name);
    Login or Signup to reply.
  3. The accepted answer is fine. But here’s an answer in more detail. There are three main steps involved before you can get an actual image file. 1) Request from Telegram the file_id. 2) Load a Guzzle client. 3) Request from Telegram the file_path. Pay close attention to the different URL paths below.

    // get file id
    $id = $update['message']['photo'][0]['file_id'];
    $file_id = "" . 
     config('app.telegram_api_key') . "/getFile?file_id=" . $id;
    // guzzle client
    $guzzle_client = new GuzzleClient();
    $request = $guzzle_client->get($file_id);
    $response = json_decode($request->getBody(),true);
    // get file path
    $file_url = "" .
     config('app.telegram_api_key') . "/" . $response['result']['file_path'];
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top