简体   繁体   English

如何使用Telegram Bot API中的“sendDocument”方法使用Java发送文件

[英]How to use “sendDocument” method in Telegram Bot API for send a file using Java

I want to send a file via Telegram Bot API, but I don't know how should I do that in Java (posting multipart/form-data) with provided Telegram Bot HTTP API method, sendDocument . 我想通过Telegram Bot API发送文件,但我不知道如何使用提供的Telegram Bot HTTP API方法sendDocument在Java中发布(发布multipart / form-data)。

Here is my code: 这是我的代码:

        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. 这是文档。

i hope this help you. 我希望这能帮助你。

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

EDIT : this pages can help any one to start coding with telegram-bot api 编辑:这个页面可以帮助任何人开始使用telegram-bot api进行编码

Telegram FAQ 电报常见问题

a good tutorial 一个很好的教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM