How can i get file_path of telegram bot

2020-07-24 05:47发布

问题:

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?

回答1:

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



回答2:

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



回答3:

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
  }
}


回答4:

I want to display the image in a reply_markup. Normal images (web images) are displayed, but a downloadable link (https://api.telegram.org/file/bot1234:abcd/photos/file_name.jpg) is not shown