From 49ff79f0cd7f32e3a59fcb7e3b6c245f22a24a39 Mon Sep 17 00:00:00 2001 From: Rubenlagu Date: Sun, 24 Jan 2016 03:20:54 +0100 Subject: [PATCH] Added sendAudio support to AbsSender Close #2 --- .../telegrambots/api/methods/SendAudio.java | 11 +++ .../telegram/telegrambots/bots/AbsSender.java | 73 ++++++++++++++++++- 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/telegram/telegrambots/api/methods/SendAudio.java b/src/main/java/org/telegram/telegrambots/api/methods/SendAudio.java index 93eb237e..53239f2e 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/SendAudio.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/SendAudio.java @@ -51,11 +51,22 @@ public class SendAudio { return audio; } + /** + * Use this method to set the audio to an audio existing in Telegram system + * @param audio File_id of the audio to send + * + * @note The file_id must have already been received or sent by your bot + */ public void setAudio(String audio) { this.audio = audio; this.isNewAudio = false; } + /** + * Use this method to set the audio to a new file + * @param audio Path to the new file in your server + * @param audioName Name of the file itself + */ public void setNewAudio(String audio, String audioName) { this.audio = audio; this.isNewAudio = true; diff --git a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java index 0779642c..3b8c65de 100644 --- a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java +++ b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java @@ -35,12 +35,16 @@ import java.util.concurrent.Executors; /** * @author Ruben Bermudez * @version 1.0 - * @brief TODO + * @brief Implementation of all the methods needed to interact with Telegram Servers * @date 14 of January of 2016 */ public abstract class AbsSender { private final ExecutorService exe = Executors.newSingleThreadExecutor(); + /** + * Returns the token of the bot to be able to perform Telegram Api Requests + * @return Token of the bot + */ public abstract String getBotToken(); public Message sendMessage(SendMessage sendMessage) throws TelegramApiException { @@ -390,6 +394,73 @@ public abstract class AbsSender { return new Message(jsonObject); } + /** + * Sends a file using Send Audio method (https://core.telegram.org/bots/api#sendaudio) + * @param sendAudio Information to send + * @return If success, the sent Message is returned + * @throws TelegramApiException If there is any error sending the audio + */ + public Message sendAudio(SendAudio sendAudio) throws TelegramApiException { + String responseContent; + + try { + CloseableHttpClient httpClient = HttpClients.createDefault(); + String url = getBaseUrl() + SendAudio.PATH; + HttpPost httppost = new HttpPost(url); + + if (sendAudio.isNewAudio()) { + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.addTextBody(SendAudio.CHATID_FIELD, sendAudio.getChatId()); + builder.addBinaryBody(SendAudio.AUDIO_FIELD, new java.io.File(sendAudio.getAudio()), ContentType.APPLICATION_OCTET_STREAM, sendAudio.getAudioName()); + if (sendAudio.getReplayMarkup() != null) { + builder.addTextBody(SendAudio.REPLYMARKUP_FIELD, sendAudio.getReplayMarkup().toJson().toString()); + } + if (sendAudio.getReplayToMessageId() != null) { + builder.addTextBody(SendAudio.REPLYTOMESSAGEID_FIELD, sendAudio.getReplayToMessageId().toString()); + } + if (sendAudio.getPerformer() != null) { + builder.addTextBody(SendAudio.PERFOMER_FIELD, sendAudio.getPerformer()); + } + if (sendAudio.getTitle() != null) { + builder.addTextBody(SendAudio.TITLE_FIELD, sendAudio.getTitle()); + } + HttpEntity multipart = builder.build(); + httppost.setEntity(multipart); + } else { + List nameValuePairs = new ArrayList<>(); + nameValuePairs.add(new BasicNameValuePair(SendAudio.CHATID_FIELD, sendAudio.getChatId())); + nameValuePairs.add(new BasicNameValuePair(SendAudio.AUDIO_FIELD, sendAudio.getAudio())); + if (sendAudio.getReplayMarkup() != null) { + nameValuePairs.add(new BasicNameValuePair(SendAudio.REPLYMARKUP_FIELD, sendAudio.getReplayMarkup().toString())); + } + if (sendAudio.getReplayToMessageId() != null) { + nameValuePairs.add(new BasicNameValuePair(SendAudio.REPLYTOMESSAGEID_FIELD, sendAudio.getReplayToMessageId().toString())); + } + if (sendAudio.getPerformer() != null) { + nameValuePairs.add(new BasicNameValuePair(SendAudio.PERFOMER_FIELD, sendAudio.getPerformer())); + } + if (sendAudio.getTitle() != null) { + nameValuePairs.add(new BasicNameValuePair(SendAudio.TITLE_FIELD, sendAudio.getTitle())); + } + httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); + } + + CloseableHttpResponse response = httpClient.execute(httppost); + HttpEntity ht = response.getEntity(); + BufferedHttpEntity buf = new BufferedHttpEntity(ht); + responseContent = EntityUtils.toString(buf, "UTF-8"); + } catch (IOException e) { + throw new TelegramApiException("Unable to send sticker", e); + } + + JSONObject jsonObject = new JSONObject(responseContent); + if (!jsonObject.getBoolean("ok")) { + throw new TelegramApiException("Error at sendAudio", jsonObject.getString("description")); + } + + return new Message(jsonObject); + } + private void sendApiMethodAsync(BotApiMethod method, SentCallback callback) { exe.submit(() -> { try {