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
.
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);
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.