skip to Main Content

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

2

Answers


  1. 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);
    
    Login or Signup to reply.
  2. 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

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