2016-08-02 49 views
1

我想通过Telegram Bot API发送文件,但我不知道我应该如何在Java中执行此操作(发布multipart/form-数据)提供Telegram Bot HTTP API方法,sendDocument如何在Telegram Bot API中使用“sendDocument”方法使用Java发送文件

这里是我的代码:

 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.

+0

我们可以帮助更好,如果你共享的项目源代码的一部分 –

+0

尝试使用一些库,例如https://github.com/pengrad/java-telegram-bot-api –

回答

-1

我希望这会帮助你。

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编码

Telegram FAQ

a good tutorial

+2

尽管此代码可能会解决提问者的问题,但最好是exp说明它是如何工作的,它与提问者所尝试的有什么区别。 – dorukayhan

+0

@dorukayhan这个问题被问了7个口前。 GitHub上存在一个很好的常见问题解答...我认为更多解释不是必需的,但我添加了这个常见问题地址。感谢您的提示。 – mohandes

相关问题