skip to Main Content

I have a telegram bot webhook message like

{  
   "update_id":236420475,
   "message":{  
      "message_id":26577,
      "from":{  
         "id":xxxxxxxx,
         "first_name":"DB",
         "last_name":"Ks",
         "username":"xxxxxxxx"
      },
      "chat":{  
         "id":193044649,
         "first_name":"DB",
         "last_name":"Ks",
         "username":"xxxxxxxx",
         "type":"private"
      },
      "date":1493266832,
      "voice":{  
         "duration":2,
         "mime_type":"audio/ogg",
         "file_id":"AwADBQADBAADQKMIVC978KStO6ZhAg",
         "file_size":7532
      }
   }
} 

From the telegram bot API documentation there is a file_path specified for downloading the file. How can i get the file_path or any API for getting file_path by using file_id?

3

Answers


  1. You should try to do that by getFile method, which returnes a File object. Just get file_path field from File and use it.

    Login or Signup to reply.
  2. It’s simple:

    request:

    https://api.telegram.org/bot<token>/getFile?file_id=<file_id>
    

    response:

    {
      "ok": true,
      "result": {
        "file_id": "---",
        "file_size": 999,
        "file_path": "photos/file_59.jpg" <<--- file_path
      }
    }
    
    Login or Signup to reply.
  3. You can download a file in 2 steps:

    1. call getFile() , in response you will be given a response as Alexey Shablowski has shown above where a file_path is returned with the response value
    2. use this file path to call their file-downloading endpoint.

    Ex. let’s say, your bot auth token: 1234:abcd and file_id: ‘xyz890

    getFile request:

    https://api.telegram.org/bot1234:abcd/getFile?file_id=xyz890
    

    response:

       {
          "ok": true,
          "result": 
            {
               "file_id": "xyz890",
               "file_size": 911,
               "file_path": "photos/file_name.jpg" 
             }
        }
    

    Now get the file_path string value and construct your full downloadable link:

    https://api.telegram.org/file/bot1234:abcd/photos/file_name.jpg
    

    API detail

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search