I’m trying to transmit a file through the Telegram Bot API, but I am unsure about the correct approach using Java while posting in multipart/form-data format with the sendFile
function from the Telegram Bot HTTP API. Below is an example of my implementation:
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost postRequest = new HttpPost("https://api.telegram.org/bot" + Main.token + "/sendFile?chat_id=" + id);
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
File fileToSend = new File(path);
entityBuilder.addBinaryBody(
"file",
new FileInputStream(fileToSend));
HttpEntity multipartEntity = entityBuilder.build();
postRequest.setEntity(multipartEntity);
CloseableHttpResponse serverResponse = httpClient.execute(postRequest);
For further guidance, please refer to the relevant documentation.