我想通过Telegram Bot API发送文件,但我不知道如何使用提供的Telegram Bot HTTP API
方法sendDocument
在Java(发布multipart / form-data)中执行此操作。
这是我的代码:
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);
答案 0 :(得分:0)
我希望这对你有所帮助。
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);
}
编辑: 这个页面可以帮助任何人开始使用telegram-bot api进行编码