0

I want to download a file in my telegram bot code many tutorials say that I must use the getFile method that I can't find that in 4.2 version of the telegram API so how I can download a file to a specific destination in host pc? thanks

korosh roohi
  • 1
  • 1
  • 3
  • 1
    To you want the bot to download a file to the server or do you want the bot to force any client to download a file to the client's system? The latter won't be possible (hopyfully!!!). I think a download must be initiated by the user. And that is good. – bkis Mar 04 '20 at 10:40
  • you can check this if helps: [How can i get file_path of telegram bot](https://stackoverflow.com/a/50493645/5332914) – user404 Mar 04 '20 at 10:49

2 Answers2

4

Assuming you are using TelegramBot SDK from rubenlagus (https://github.com/rubenlagus/TelegramBots), as I faced same issue. Below is my solution.

GetFile getFile = new GetFile().setFileId(fileId);
String filePath = execute(getFile).getFilePath();
File file = downloadFile(filePath, outputFile);
Narendra
  • 127
  • 1
  • 6
0

I had the same problem. This was my solution. Not very nice but it works.

if (update.getMessage().hasDocument()){
        String doc_id = update.getMessage().getDocument().getFileId();
        String doc_name = update.getMessage().getDocument().getFileName();
        String doc_mine = update.getMessage().getDocument().getMimeType();
        int doc_size = update.getMessage().getDocument().getFileSize();
        String getID = String.valueOf(update.getMessage().getFrom().getId());

        Document document = new Document();
        document.setMimeType(doc_mine);
        document.setFileName(doc_name);
        document.setFileSize(doc_size);
        document.setFileId(doc_id);

        GetFile getFile = new GetFile();
        getFile.setFileId(document.getFileId());
        try {
            org.telegram.telegrambots.meta.api.objects.File file = execute(getFile);
           downloadFile(file, new File("./data/userDoc/"+getID+"_"+doc_name));
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }


    }

Here I got this solution enter link description here