From ee12175eeaffd919e3d240c43b7df5d767d42f04 Mon Sep 17 00:00:00 2001 From: antonu17 Date: Fri, 3 Jun 2016 23:22:02 +0600 Subject: [PATCH 1/4] Change sendNew methods signature, now accepting File --- .../api/methods/send/SendAudio.java | 21 ++++++++++--------- .../api/methods/send/SendDocument.java | 17 ++++++++------- .../api/methods/send/SendPhoto.java | 15 ++++++------- .../api/methods/send/SendSticker.java | 17 ++++++++------- .../api/methods/send/SendVideo.java | 15 ++++++------- .../api/methods/send/SendVoice.java | 16 +++++++------- 6 files changed, 54 insertions(+), 47 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendAudio.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendAudio.java index 649906ca..6141adbd 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendAudio.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendAudio.java @@ -2,6 +2,8 @@ package org.telegram.telegrambots.api.methods.send; import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; +import java.io.File; + /** * @author Ruben Bermudez * @version 1.0 @@ -36,8 +38,9 @@ public class SendAudio { private ReplyKeyboard replayMarkup; ///< Optional. JSON-serialized object for a custom reply keyboard private String performer; ///< Optional. Performer of sent audio private String title; ///< Optional. Title of sent audio - private boolean isNewAudio; - private String audioName; + + private boolean isNewAudio; ///< True to upload a new audio, false to use a fileId + private File newAudioFile; ///< New audio file public SendAudio() { super(); @@ -80,13 +83,12 @@ public class SendAudio { /** * 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 + * @param file New audio file */ - public SendAudio setNewAudio(String audio, String audioName) { - this.audio = audio; + public SendAudio setNewAudio(File file) { + this.audio = file.getName(); this.isNewAudio = true; - this.audioName = audioName; + this.newAudioFile = file; return this; } @@ -144,8 +146,8 @@ public class SendAudio { return isNewAudio; } - public String getAudioName() { - return audioName; + public File getNewAudioFile() { + return newAudioFile; } @Override @@ -158,7 +160,6 @@ public class SendAudio { ", performer='" + performer + '\'' + ", title='" + title + '\'' + ", isNewAudio=" + isNewAudio + - ", audioName='" + audioName + '\'' + '}'; } } diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendDocument.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendDocument.java index dacf2bb2..9f3ddbe7 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendDocument.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendDocument.java @@ -2,6 +2,8 @@ package org.telegram.telegrambots.api.methods.send; import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; +import java.io.File; + /** * @author Ruben Bermudez * @version 1.0 @@ -28,8 +30,8 @@ public class SendDocument { private Integer replayToMessageId; ///< Optional. If the message is a reply, ID of the original message private ReplyKeyboard replayMarkup; ///< Optional. JSON-serialized object for a custom reply keyboard - private boolean isNewDocument; - private String documentName; + private boolean isNewDocument; ///< True to upload a new document, false to use a fileId + private File newDocumentFile; ///< New document file public SendDocument() { super(); @@ -54,10 +56,10 @@ public class SendDocument { return this; } - public SendDocument setNewDocument(String document, String documentName) { - this.document = document; + public SendDocument setNewDocument(File file) { + this.document = file.getName(); this.isNewDocument = true; - this.documentName = documentName; + this.newDocumentFile = file; return this; } @@ -65,8 +67,8 @@ public class SendDocument { return isNewDocument; } - public String getDocumentName() { - return documentName; + public File getNewDocumentFile() { + return newDocumentFile; } public Integer getReplayToMessageId() { @@ -118,7 +120,6 @@ public class SendDocument { ", replayToMessageId=" + replayToMessageId + ", replayMarkup=" + replayMarkup + ", isNewDocument=" + isNewDocument + - ", documentName='" + documentName + '\'' + '}'; } } diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendPhoto.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendPhoto.java index fcb59daa..18cb4802 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendPhoto.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendPhoto.java @@ -2,6 +2,8 @@ package org.telegram.telegrambots.api.methods.send; import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; +import java.io.File; + /** * @author Ruben Bermudez * @version 1.0 @@ -29,7 +31,7 @@ public class SendPhoto { private ReplyKeyboard replayMarkup; ///< Optional. JSON-serialized object for a custom reply keyboard private boolean isNewPhoto; ///< True if the photo must be uploaded from a file, file if it is a fileId - private String photoName; ///< Name of the photo + private File newPhotoFile; // New photo file public SendPhoto() { @@ -86,8 +88,8 @@ public class SendPhoto { return isNewPhoto; } - public String getPhotoName() { - return photoName; + public File getNewPhotoFile() { + return newPhotoFile; } public Boolean getDisableNotification() { @@ -104,10 +106,10 @@ public class SendPhoto { return this; } - public SendPhoto setNewPhoto(String photo, String photoName) { - this.photo = photo; + public SendPhoto setNewPhoto(File file) { + this.photo = file.getName(); + this.newPhotoFile = file; this.isNewPhoto = true; - this.photoName = photoName; return this; } @@ -120,7 +122,6 @@ public class SendPhoto { ", replayToMessageId=" + replayToMessageId + ", replayMarkup=" + replayMarkup + ", isNewPhoto=" + isNewPhoto + - ", photoName='" + photoName + '\'' + '}'; } } diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendSticker.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendSticker.java index b9c9cd9a..b67a0eaf 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendSticker.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendSticker.java @@ -2,6 +2,8 @@ package org.telegram.telegrambots.api.methods.send; import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; +import java.io.File; + /** * @author Ruben Bermudez * @version 1.0 @@ -26,8 +28,8 @@ public class SendSticker { private Integer replayToMessageId; ///< Optional. If the message is a reply, ID of the original message private ReplyKeyboard replayMarkup; ///< Optional. JSON-serialized object for a custom reply keyboard - private boolean isNewSticker; - private String stickerName; + private boolean isNewSticker; ///< True to upload a new sticker, false to use a fileId + private File newStickerFile; ///< New sticker file public SendSticker() { super(); @@ -70,10 +72,10 @@ public class SendSticker { return this; } - public SendSticker setSticker(String sticker, String stickerName) { - this.sticker = sticker; + public SendSticker setSticker(File file) { + this.sticker = file.getName(); this.isNewSticker = true; - this.stickerName = stickerName; + this.newStickerFile = file; return this; } @@ -95,8 +97,8 @@ public class SendSticker { return isNewSticker; } - public String getStickerName() { - return stickerName; + public File getNewStickerFile() { + return newStickerFile; } @Override @@ -107,7 +109,6 @@ public class SendSticker { ", replayToMessageId=" + replayToMessageId + ", replayMarkup=" + replayMarkup + ", isNewSticker=" + isNewSticker + - ", stickerName='" + stickerName + '\'' + '}'; } } diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendVideo.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendVideo.java index 135ad939..25fbc004 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendVideo.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendVideo.java @@ -2,6 +2,8 @@ package org.telegram.telegrambots.api.methods.send; import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; +import java.io.File; + /** * @author Ruben Bermudez * @version 1.0 @@ -36,7 +38,7 @@ public class SendVideo { private ReplyKeyboard replayMarkup; ///< Optional. JSON-serialized object for a custom reply keyboard private boolean isNewVideo; ///< True to upload a new video, false to use a fileId - private String videoName; ///< Name of the video + private File newVideoFile; ///< New video file public SendVideo() { super(); @@ -101,8 +103,8 @@ public class SendVideo { return isNewVideo; } - public String getVideoName() { - return videoName; + public File getNewVideoFile() { + return newVideoFile; } public Boolean getDisableNotification() { @@ -137,10 +139,10 @@ public class SendVideo { return this; } - public SendVideo setNewVideo(String video, String videoName) { - this.video = video; + public SendVideo setNewVideo(File file) { + this.video = file.getName(); this.isNewVideo = true; - this.videoName = videoName; + this.newVideoFile = file; return this; } @@ -154,7 +156,6 @@ public class SendVideo { ", replayToMessageId=" + replayToMessageId + ", replayMarkup=" + replayMarkup + ", isNewVideo=" + isNewVideo + - ", videoName='" + videoName + '\'' + '}'; } } diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendVoice.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendVoice.java index fb01b1ea..d6057548 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendVoice.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendVoice.java @@ -2,6 +2,8 @@ package org.telegram.telegrambots.api.methods.send; import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; +import java.io.File; + /** * @author Ruben Bermudez * @version 1.0 @@ -31,7 +33,7 @@ public class SendVoice { private Integer duration; ///< Optional. Duration of sent audio in seconds private boolean isNewVoice; ///< True to upload a new voice note, false to use a fileId - private String voiceName; ///< Name of the voice note + private File newVoiceFile; ///< New voice note file public SendVoice() { super(); @@ -81,10 +83,10 @@ public class SendVoice { return this; } - public SendVoice setNewAudio(String audio, String audioName) { - this.audio = audio; - this.isNewVoice = false; - this.voiceName = audioName; + public SendVoice setNewAudio(File file) { + this.audio = file.getName(); + this.isNewVoice = true; + this.newVoiceFile = file; return this; } @@ -119,7 +121,7 @@ public class SendVoice { return isNewVoice; } - public String getVoiceName() { - return voiceName; + public File getNewVoiceFile() { + return newVoiceFile; } } From 7b6a3769764891ace193d683a5225baf4c56ef83 Mon Sep 17 00:00:00 2001 From: antonu17 Date: Fri, 3 Jun 2016 23:22:13 +0600 Subject: [PATCH 2/4] Change MultipartEntityBuilder.addBinaryBody() function --- .../org/telegram/telegrambots/bots/AbsSender.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java index ebefba8c..0425a167 100644 --- a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java +++ b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java @@ -502,7 +502,7 @@ public abstract class AbsSender { if (sendDocument.isNewDocument()) { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody(SendDocument.CHATID_FIELD, sendDocument.getChatId()); - builder.addBinaryBody(SendDocument.DOCUMENT_FIELD, new java.io.File(sendDocument.getDocument()), ContentType.APPLICATION_OCTET_STREAM, sendDocument.getDocumentName()); + builder.addBinaryBody(SendDocument.DOCUMENT_FIELD, sendDocument.getNewDocumentFile()); if (sendDocument.getReplayMarkup() != null) { builder.addTextBody(SendDocument.REPLYMARKUP_FIELD, sendDocument.getReplayMarkup().toJson().toString()); } @@ -562,7 +562,7 @@ public abstract class AbsSender { if (sendPhoto.isNewPhoto()) { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody(SendPhoto.CHATID_FIELD, sendPhoto.getChatId()); - builder.addBinaryBody(SendPhoto.PHOTO_FIELD, new java.io.File(sendPhoto.getPhoto()), ContentType.APPLICATION_OCTET_STREAM, sendPhoto.getPhotoName()); + builder.addBinaryBody(SendPhoto.PHOTO_FIELD, sendPhoto.getNewPhotoFile()); if (sendPhoto.getReplayMarkup() != null) { builder.addTextBody(SendPhoto.REPLYMARKUP_FIELD, sendPhoto.getReplayMarkup().toJson().toString()); } @@ -622,7 +622,7 @@ public abstract class AbsSender { if (sendVideo.isNewVideo()) { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody(SendVideo.CHATID_FIELD, sendVideo.getChatId()); - builder.addBinaryBody(SendVideo.VIDEO_FIELD, new java.io.File(sendVideo.getVideo()), ContentType.APPLICATION_OCTET_STREAM, sendVideo.getVideoName()); + builder.addBinaryBody(SendVideo.VIDEO_FIELD, sendVideo.getNewVideoFile()); if (sendVideo.getReplayMarkup() != null) { builder.addTextBody(SendVideo.REPLYMARKUP_FIELD, sendVideo.getReplayMarkup().toJson().toString()); } @@ -701,7 +701,7 @@ public abstract class AbsSender { if (sendSticker.isNewSticker()) { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody(SendSticker.CHATID_FIELD, sendSticker.getChatId()); - builder.addBinaryBody(SendSticker.STICKER_FIELD, new java.io.File(sendSticker.getSticker()), ContentType.APPLICATION_OCTET_STREAM, sendSticker.getStickerName()); + builder.addBinaryBody(SendSticker.STICKER_FIELD, sendSticker.getNewStickerFile()); if (sendSticker.getReplayMarkup() != null) { builder.addTextBody(SendSticker.REPLYMARKUP_FIELD, sendSticker.getReplayMarkup().toJson().toString()); } @@ -763,7 +763,7 @@ public abstract class AbsSender { 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.create("audio/mpeg"), sendAudio.getAudioName()); + builder.addBinaryBody(SendAudio.AUDIO_FIELD, sendAudio.getNewAudioFile()); if (sendAudio.getReplayMarkup() != null) { builder.addTextBody(SendAudio.REPLYMARKUP_FIELD, sendAudio.getReplayMarkup().toJson().toString()); } @@ -831,6 +831,7 @@ public abstract class AbsSender { /** * Sends a voice note using Send Voice method (https://core.telegram.org/bots/api#sendvoice) + * For this to work, your audio must be in an .ogg file encoded with OPUS * @param sendVoice Information to send * @return If success, the sent Message is returned * @throws TelegramApiException If there is any error sending the audio @@ -845,7 +846,7 @@ public abstract class AbsSender { if (sendVoice.isNewVoice()) { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody(SendVoice.CHATID_FIELD, sendVoice.getChatId()); - builder.addBinaryBody(SendVoice.AUDIO_FIELD, new java.io.File(sendVoice.getAudio()), ContentType.create("audio/ogg"), sendVoice.getVoiceName()); + builder.addBinaryBody(SendVoice.AUDIO_FIELD, sendVoice.getNewVoiceFile()); if (sendVoice.getReplayMarkup() != null) { builder.addTextBody(SendVoice.REPLYMARKUP_FIELD, sendVoice.getReplayMarkup().toJson().toString()); } From 65d82076c2911906861adc421006d4986e41bf95 Mon Sep 17 00:00:00 2001 From: antonu17 Date: Sat, 4 Jun 2016 10:15:55 +0600 Subject: [PATCH 3/4] Deprecate old methods, overload setNew... methods with InputStream arguments --- .../api/methods/send/SendAudio.java | 31 +++++++++++++++++++ .../api/methods/send/SendDocument.java | 25 +++++++++++++++ .../api/methods/send/SendPhoto.java | 26 +++++++++++++++- .../api/methods/send/SendSticker.java | 27 +++++++++++++++- .../api/methods/send/SendVideo.java | 25 +++++++++++++++ .../api/methods/send/SendVoice.java | 25 +++++++++++++++ 6 files changed, 157 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendAudio.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendAudio.java index 6141adbd..6f101844 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendAudio.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendAudio.java @@ -3,6 +3,7 @@ package org.telegram.telegrambots.api.methods.send; import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; import java.io.File; +import java.io.InputStream; /** * @author Ruben Bermudez @@ -40,7 +41,9 @@ public class SendAudio { private String title; ///< Optional. Title of sent audio private boolean isNewAudio; ///< True to upload a new audio, false to use a fileId + private String audioName; private File newAudioFile; ///< New audio file + private InputStream newAudioStream; ///< New audio stream public SendAudio() { super(); @@ -80,6 +83,20 @@ public class SendAudio { return this; } + /** + * 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 + */ + @Deprecated + public SendAudio setNewAudio(String audio, String audioName) { + this.audio = audio; + this.isNewAudio = true; + this.audioName = audioName; + return this; + } + /** * Use this method to set the audio to a new file * @@ -92,6 +109,12 @@ public class SendAudio { return this; } + public SendAudio setNewAudio(InputStream inputStream) { + this.isNewAudio = true; + this.newAudioStream = inputStream; + return this; + } + public Integer getReplayToMessageId() { return replayToMessageId; } @@ -146,10 +169,18 @@ public class SendAudio { return isNewAudio; } + public String getAudioName() { + return audioName; + } + public File getNewAudioFile() { return newAudioFile; } + public InputStream getNewAudioStream() { + return newAudioStream; + } + @Override public String toString() { return "SendAudio{" + diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendDocument.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendDocument.java index 9f3ddbe7..6a1c8dd2 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendDocument.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendDocument.java @@ -3,6 +3,7 @@ package org.telegram.telegrambots.api.methods.send; import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; import java.io.File; +import java.io.InputStream; /** * @author Ruben Bermudez @@ -31,7 +32,9 @@ public class SendDocument { private ReplyKeyboard replayMarkup; ///< Optional. JSON-serialized object for a custom reply keyboard private boolean isNewDocument; ///< True to upload a new document, false to use a fileId + private String documentName; private File newDocumentFile; ///< New document file + private InputStream newDocumentStream; ///< New document stream public SendDocument() { super(); @@ -56,6 +59,14 @@ public class SendDocument { return this; } + @Deprecated + public SendDocument setNewDocument(String document, String documentName) { + this.document = document; + this.isNewDocument = true; + this.documentName = documentName; + return this; + } + public SendDocument setNewDocument(File file) { this.document = file.getName(); this.isNewDocument = true; @@ -63,14 +74,28 @@ public class SendDocument { return this; } + public SendDocument setNewDocument(InputStream inputStream) { + this.isNewDocument = true; + this.newDocumentStream = inputStream; + return this; + } + public boolean isNewDocument() { return isNewDocument; } + public String getDocumentName() { + return documentName; + } + public File getNewDocumentFile() { return newDocumentFile; } + public InputStream getNewDocumentStream() { + return newDocumentStream; + } + public Integer getReplayToMessageId() { return replayToMessageId; } diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendPhoto.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendPhoto.java index 18cb4802..57694a11 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendPhoto.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendPhoto.java @@ -3,6 +3,7 @@ package org.telegram.telegrambots.api.methods.send; import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; import java.io.File; +import java.io.InputStream; /** * @author Ruben Bermudez @@ -31,8 +32,9 @@ public class SendPhoto { private ReplyKeyboard replayMarkup; ///< Optional. JSON-serialized object for a custom reply keyboard private boolean isNewPhoto; ///< True if the photo must be uploaded from a file, file if it is a fileId + private String photoName; ///< Name of the photo private File newPhotoFile; // New photo file - + private InputStream newPhotoStream; // New photo stream public SendPhoto() { super(); @@ -88,10 +90,18 @@ public class SendPhoto { return isNewPhoto; } + public String getPhotoName() { + return photoName; + } + public File getNewPhotoFile() { return newPhotoFile; } + public InputStream getNewPhotoStream() { + return newPhotoStream; + } + public Boolean getDisableNotification() { return disableNotification; } @@ -106,6 +116,14 @@ public class SendPhoto { return this; } + @Deprecated + public SendPhoto setNewPhoto(String photo, String photoName) { + this.photo = photo; + this.isNewPhoto = true; + this.photoName = photoName; + return this; + } + public SendPhoto setNewPhoto(File file) { this.photo = file.getName(); this.newPhotoFile = file; @@ -113,6 +131,12 @@ public class SendPhoto { return this; } + public SendPhoto setNewPhoto(InputStream inputStream) { + this.newPhotoStream = inputStream; + this.isNewPhoto = true; + return this; + } + @Override public String toString() { return "SendPhoto{" + diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendSticker.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendSticker.java index b67a0eaf..4f663297 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendSticker.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendSticker.java @@ -3,6 +3,7 @@ package org.telegram.telegrambots.api.methods.send; import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; import java.io.File; +import java.io.InputStream; /** * @author Ruben Bermudez @@ -29,7 +30,9 @@ public class SendSticker { private ReplyKeyboard replayMarkup; ///< Optional. JSON-serialized object for a custom reply keyboard private boolean isNewSticker; ///< True to upload a new sticker, false to use a fileId + private String stickerName; private File newStickerFile; ///< New sticker file + private InputStream newStickerStream; ///< New sticker stream public SendSticker() { super(); @@ -72,13 +75,27 @@ public class SendSticker { return this; } - public SendSticker setSticker(File file) { + @Deprecated + public SendSticker setSticker(String sticker, String stickerName) { + this.sticker = sticker; + this.isNewSticker = true; + this.stickerName = stickerName; + return this; + } + + public SendSticker setNewSticker(File file) { this.sticker = file.getName(); this.isNewSticker = true; this.newStickerFile = file; return this; } + public SendSticker setNewSticker(InputStream inputStream) { + this.isNewSticker = true; + this.newStickerStream = inputStream; + return this; + } + public Boolean getDisableNotification() { return disableNotification; } @@ -97,10 +114,18 @@ public class SendSticker { return isNewSticker; } + public String getStickerName() { + return stickerName; + } + public File getNewStickerFile() { return newStickerFile; } + public InputStream getNewStickerStream() { + return newStickerStream; + } + @Override public String toString() { return "SendSticker{" + diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendVideo.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendVideo.java index 25fbc004..956588b6 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendVideo.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendVideo.java @@ -3,6 +3,7 @@ package org.telegram.telegrambots.api.methods.send; import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; import java.io.File; +import java.io.InputStream; /** * @author Ruben Bermudez @@ -38,7 +39,9 @@ public class SendVideo { private ReplyKeyboard replayMarkup; ///< Optional. JSON-serialized object for a custom reply keyboard private boolean isNewVideo; ///< True to upload a new video, false to use a fileId + private String videoName; ///< Name of the video private File newVideoFile; ///< New video file + private InputStream newVideoStream; ///< New video stream public SendVideo() { super(); @@ -103,10 +106,18 @@ public class SendVideo { return isNewVideo; } + public String getVideoName() { + return videoName; + } + public File getNewVideoFile() { return newVideoFile; } + public InputStream getNewVideoStream() { + return newVideoStream; + } + public Boolean getDisableNotification() { return disableNotification; } @@ -139,6 +150,14 @@ public class SendVideo { return this; } + @Deprecated + public SendVideo setNewVideo(String video, String videoName) { + this.video = video; + this.isNewVideo = true; + this.videoName = videoName; + return this; + } + public SendVideo setNewVideo(File file) { this.video = file.getName(); this.isNewVideo = true; @@ -146,6 +165,12 @@ public class SendVideo { return this; } + public SendVideo setNewVideo(InputStream inputStream) { + this.isNewVideo = true; + this.newVideoStream = inputStream; + return this; + } + @Override public String toString() { return "SendVideo{" + diff --git a/src/main/java/org/telegram/telegrambots/api/methods/send/SendVoice.java b/src/main/java/org/telegram/telegrambots/api/methods/send/SendVoice.java index d6057548..27f20367 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/send/SendVoice.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/send/SendVoice.java @@ -3,6 +3,7 @@ package org.telegram.telegrambots.api.methods.send; import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard; import java.io.File; +import java.io.InputStream; /** * @author Ruben Bermudez @@ -33,7 +34,9 @@ public class SendVoice { private Integer duration; ///< Optional. Duration of sent audio in seconds private boolean isNewVoice; ///< True to upload a new voice note, false to use a fileId + private String voiceName; ///< Name of the voice note private File newVoiceFile; ///< New voice note file + private InputStream newVoiceStream; ///< New voice note stream public SendVoice() { super(); @@ -83,6 +86,14 @@ public class SendVoice { return this; } + @Deprecated + public SendVoice setNewAudio(String audio, String audioName) { + this.audio = audio; + this.isNewVoice = false; + this.voiceName = audioName; + return this; + } + public SendVoice setNewAudio(File file) { this.audio = file.getName(); this.isNewVoice = true; @@ -90,6 +101,12 @@ public class SendVoice { return this; } + public SendVoice setNewAudio(InputStream inputStream) { + this.isNewVoice = true; + this.newVoiceStream = inputStream; + return this; + } + public Integer getReplayToMessageId() { return replayToMessageId; } @@ -121,7 +138,15 @@ public class SendVoice { return isNewVoice; } + public String getVoiceName() { + return voiceName; + } + public File getNewVoiceFile() { return newVoiceFile; } + + public InputStream getNewVoiceStream() { + return newVoiceStream; + } } From 510808714440b3a4bc29db2fc7f1ad0556e65acd Mon Sep 17 00:00:00 2001 From: antonu17 Date: Sat, 4 Jun 2016 10:17:00 +0600 Subject: [PATCH 4/4] Change send methods, add using InputStream --- .../telegram/telegrambots/bots/AbsSender.java | 48 ++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java index 0425a167..300d3539 100644 --- a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java +++ b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java @@ -502,7 +502,13 @@ public abstract class AbsSender { if (sendDocument.isNewDocument()) { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody(SendDocument.CHATID_FIELD, sendDocument.getChatId()); - builder.addBinaryBody(SendDocument.DOCUMENT_FIELD, sendDocument.getNewDocumentFile()); + if (sendDocument.getNewDocumentFile() != null) { + builder.addBinaryBody(SendDocument.DOCUMENT_FIELD, sendDocument.getNewDocumentFile()); + } else if (sendDocument.getNewDocumentStream() != null) { + builder.addBinaryBody(SendDocument.DOCUMENT_FIELD, sendDocument.getNewDocumentStream()); + } else { + builder.addBinaryBody(SendDocument.DOCUMENT_FIELD, new java.io.File(sendDocument.getDocument()), ContentType.APPLICATION_OCTET_STREAM, sendDocument.getDocumentName()); + } if (sendDocument.getReplayMarkup() != null) { builder.addTextBody(SendDocument.REPLYMARKUP_FIELD, sendDocument.getReplayMarkup().toJson().toString()); } @@ -562,7 +568,13 @@ public abstract class AbsSender { if (sendPhoto.isNewPhoto()) { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody(SendPhoto.CHATID_FIELD, sendPhoto.getChatId()); - builder.addBinaryBody(SendPhoto.PHOTO_FIELD, sendPhoto.getNewPhotoFile()); + if (sendPhoto.getNewPhotoFile() != null) { + builder.addBinaryBody(SendPhoto.PHOTO_FIELD, sendPhoto.getNewPhotoFile()); + } else if (sendPhoto.getNewPhotoStream() != null) { + builder.addBinaryBody(SendPhoto.PHOTO_FIELD, sendPhoto.getNewPhotoStream()); + } else { + builder.addBinaryBody(SendPhoto.PHOTO_FIELD, new java.io.File(sendPhoto.getPhoto()), ContentType.APPLICATION_OCTET_STREAM, sendPhoto.getPhotoName()); + } if (sendPhoto.getReplayMarkup() != null) { builder.addTextBody(SendPhoto.REPLYMARKUP_FIELD, sendPhoto.getReplayMarkup().toJson().toString()); } @@ -622,7 +634,13 @@ public abstract class AbsSender { if (sendVideo.isNewVideo()) { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody(SendVideo.CHATID_FIELD, sendVideo.getChatId()); - builder.addBinaryBody(SendVideo.VIDEO_FIELD, sendVideo.getNewVideoFile()); + if (sendVideo.getNewVideoFile() != null) { + builder.addBinaryBody(SendVideo.VIDEO_FIELD, sendVideo.getNewVideoFile()); + } else if (sendVideo.getNewVideoStream() != null) { + builder.addBinaryBody(SendVideo.VIDEO_FIELD, sendVideo.getNewVideoStream()); + } else { + builder.addBinaryBody(SendVideo.VIDEO_FIELD, new java.io.File(sendVideo.getVideo()), ContentType.APPLICATION_OCTET_STREAM, sendVideo.getVideoName()); + } if (sendVideo.getReplayMarkup() != null) { builder.addTextBody(SendVideo.REPLYMARKUP_FIELD, sendVideo.getReplayMarkup().toJson().toString()); } @@ -701,7 +719,13 @@ public abstract class AbsSender { if (sendSticker.isNewSticker()) { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody(SendSticker.CHATID_FIELD, sendSticker.getChatId()); - builder.addBinaryBody(SendSticker.STICKER_FIELD, sendSticker.getNewStickerFile()); + if (sendSticker.getNewStickerFile() != null) { + builder.addBinaryBody(SendSticker.STICKER_FIELD, sendSticker.getNewStickerFile()); + } else if (sendSticker.getNewStickerStream() != null) { + builder.addBinaryBody(SendSticker.STICKER_FIELD, sendSticker.getNewStickerStream()); + } else { + builder.addBinaryBody(SendSticker.STICKER_FIELD, new java.io.File(sendSticker.getSticker()), ContentType.APPLICATION_OCTET_STREAM, sendSticker.getStickerName()); + } if (sendSticker.getReplayMarkup() != null) { builder.addTextBody(SendSticker.REPLYMARKUP_FIELD, sendSticker.getReplayMarkup().toJson().toString()); } @@ -763,7 +787,13 @@ public abstract class AbsSender { if (sendAudio.isNewAudio()) { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody(SendAudio.CHATID_FIELD, sendAudio.getChatId()); - builder.addBinaryBody(SendAudio.AUDIO_FIELD, sendAudio.getNewAudioFile()); + if (sendAudio.getNewAudioFile() != null) { + builder.addBinaryBody(SendAudio.AUDIO_FIELD, sendAudio.getNewAudioFile()); + } else if (sendAudio.getNewAudioStream() != null) { + builder.addBinaryBody(SendAudio.AUDIO_FIELD, sendAudio.getNewAudioStream()); + } else { + builder.addBinaryBody(SendAudio.AUDIO_FIELD, new java.io.File(sendAudio.getAudio()), ContentType.create("audio/mpeg"), sendAudio.getAudioName()); + } if (sendAudio.getReplayMarkup() != null) { builder.addTextBody(SendAudio.REPLYMARKUP_FIELD, sendAudio.getReplayMarkup().toJson().toString()); } @@ -846,7 +876,13 @@ public abstract class AbsSender { if (sendVoice.isNewVoice()) { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody(SendVoice.CHATID_FIELD, sendVoice.getChatId()); - builder.addBinaryBody(SendVoice.AUDIO_FIELD, sendVoice.getNewVoiceFile()); + if (sendVoice.getNewVoiceFile() != null) { + builder.addBinaryBody(SendVoice.AUDIO_FIELD, sendVoice.getNewVoiceFile()); + } else if (sendVoice.getNewVoiceStream() != null) { + builder.addBinaryBody(SendVoice.AUDIO_FIELD, sendVoice.getNewVoiceStream()); + } else { + builder.addBinaryBody(SendVoice.AUDIO_FIELD, new java.io.File(sendVoice.getAudio()), ContentType.create("audio/ogg"), sendVoice.getVoiceName()); + } if (sendVoice.getReplayMarkup() != null) { builder.addTextBody(SendVoice.REPLYMARKUP_FIELD, sendVoice.getReplayMarkup().toJson().toString()); }