Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i get file_path of telegram bot

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?

like image 864
joe Avatar asked Oct 17 '25 14:10

joe


1 Answers

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

like image 173
user404 Avatar answered Oct 21 '25 12:10

user404