如何在 Telegram Bot API 中使用 "sendDocument" 方法使用 Java 发送文件

How to use "sendDocument" method in Telegram Bot API for send a file using Java

我想通过 Telegram Bot API 发送文件,但我不知道在 Java(发布 multipart/form-data)中我应该怎么做 [=14] =]方法,sendDocument.

这是我的代码:

        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost upload = new HttpPost("https://api.telegram.org/bot"+Main.token+"/sendDocument?chat_id="+id);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();

        File file = new File(path);

        builder.addBinaryBody(
                "document",
                new FileInputStream(file));

        HttpEntity part = builder.build();

        upload.setEntity(part);

        CloseableHttpResponse response = client.execute(upload);

Here are the docs.

希望对你有所帮助。

private void sendDocUploadingAFile(Long chatId, java.io.File save,String caption) throws TelegramApiException {

    SendDocument sendDocumentRequest = new SendDocument();
    sendDocumentRequest.setChatId(chatId);
    sendDocumentRequest.setNewDocument(save);
    sendDocumentRequest.setCaption(caption);
    sendDocument(sendDocumentRequest);
}

编辑: 此页面可以帮助任何人开始使用电报机器人编码 api

Telegram FAQ

a good tutorial