如何使用;“发送文件”;电报机器人API中使用Java发送文件的方法
我想通过Telegram Bot API发送文件,但我不知道如何使用Java(发布多部分/表单数据)和提供的如何使用;“发送文件”;电报机器人API中使用Java发送文件的方法,java,http,telegram-bot,Java,Http,Telegram Bot,我想通过Telegram Bot API发送文件,但我不知道如何使用Java(发布多部分/表单数据)和提供的Telegram Bot HTTP API方法,sendDocument 这是我的密码: CloseableHttpClient client = HttpClients.createDefault(); HttpPost upload = new HttpPost("https://api.telegram.org/bot"+Main.token+"/se
Telegram Bot HTTP API
方法,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);
我希望这对你有帮助
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编码
如果您共享部分项目源代码并尝试使用一些库,我们可以提供更好的帮助。例如,虽然此代码可能解决询问者的问题,但最好解释它是如何工作的,以及它与询问者尝试过的代码之间的区别。@dorukayhan这个问题是7个月前提出的。GitHub上有一个很好的FAQ。。。我想没有必要再多解释。但我添加了这个常见问题解答地址。谢谢你的提示。