From 986608d7adede40c548c0e8dc80436ee24a3a4ce Mon Sep 17 00:00:00 2001 From: tschulz Date: Tue, 31 May 2016 10:54:40 +0200 Subject: [PATCH 01/21] add java doc for CommandRegistry -> class purpose --- .../telegram/telegrambots/bots/commands/CommandRegistry.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java b/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java index 6d2d3eb7..5231c4f5 100644 --- a/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java +++ b/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java @@ -10,6 +10,8 @@ import java.util.Map; import java.util.function.BiConsumer; /** + * This class manages all the commands for a bot. You can register and deregister commands on demand + * * @author tschulz */ public final class CommandRegistry implements ICommandRegistry { From eefeb1e19f3ab33e8565c71d1fda5e081c8af7b6 Mon Sep 17 00:00:00 2001 From: tschulz Date: Tue, 31 May 2016 10:54:58 +0200 Subject: [PATCH 02/21] remove empty constructor --- .../telegram/telegrambots/bots/commands/CommandRegistry.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java b/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java index 5231c4f5..033e297a 100644 --- a/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java +++ b/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java @@ -19,9 +19,6 @@ public final class CommandRegistry implements ICommandRegistry { private final Map commandRegistryMap = new HashMap<>(); private BiConsumer defaultConsumer; - public CommandRegistry() { - } - @Override public void registerDefaultAction(BiConsumer defaultConsumer) { this.defaultConsumer = defaultConsumer; From d8abaec756a1ade9c0df166ff3ad003318437f05 Mon Sep 17 00:00:00 2001 From: tschulz Date: Tue, 31 May 2016 10:56:21 +0200 Subject: [PATCH 03/21] add javadoc (class purpose) for ICommandRegistry --- .../telegram/telegrambots/bots/commands/ICommandRegistry.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/telegram/telegrambots/bots/commands/ICommandRegistry.java b/src/main/java/org/telegram/telegrambots/bots/commands/ICommandRegistry.java index 676e1f8f..29559a0b 100644 --- a/src/main/java/org/telegram/telegrambots/bots/commands/ICommandRegistry.java +++ b/src/main/java/org/telegram/telegrambots/bots/commands/ICommandRegistry.java @@ -8,7 +8,9 @@ import java.util.Map; import java.util.function.BiConsumer; /** + * This Interface represents the gateway for registering and deregistering commands. * + * @author tschulz */ public interface ICommandRegistry { From cc9cda3226249d2061e0127ba05a2588f58f7ddf Mon Sep 17 00:00:00 2001 From: tschulz Date: Tue, 31 May 2016 11:13:28 +0200 Subject: [PATCH 04/21] add example for command bot to readme --- README.md | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d4fd43a1..7fb0a014 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ In order to use Long Polling mode, just create your own bot extending `org.teleg If you like to use Webhook, extend `org.telegram.telegrambots.bots.TelegramWebhookBot` -Once done, you just need to creat a `org.telegram.telegrambots.TelegramBotsApi`and register your bots: +Once done, you just need to create a `org.telegram.telegrambots.TelegramBotsApi`and register your bots: ```java @@ -44,7 +44,64 @@ Once done, you just need to creat a `org.telegram.telegrambots.TelegramBotsApi`a } ``` +## Command Bot +In order to make commands work for your bot by using the `org.telegram.telegrambots.bots.TelegramLongPollingCommandBot` you can +just make your Bot extend this class instead of the `org.telegram.telegrambots.bots.TelegramLongPollingBot`. + +Since this bot inherits from `org.telegram.telegrambots.bots.commands.ICommandRegistry` it is capable of register- and +deregistering commands to your bot. + +Here is an example: + +```java + + public class Main { + + private static final String LOGTAG = "MAIN"; + + public static void main(String[] args) { + TelegramBotsApi telegramBotsApi = new TelegramBotsApi(); + try { + telegramBotsApi.registerBot(new MySuperSpecialBotHandler()); + } catch (TelegramApiException e) { + BotLogger.error(LOGTAG, e); + } + } + } +``` +```java + + public class MySuperSpecialBotHandler extends TelegramLongPollingCommandBot { + + private static final String LOGTAG = "MY_SUPER_SPECIAL_BOT_HANDLER"; + + public MySuperSpecialBotHandler() { + register(new BotCommand("mycommand", "This command just demonstrates the use of commands") { + @Override + public void execute(AbsSender absSender, Chat chat, String[] arguments) { + SendMessage sendMessage = new SendMessage(); + sendMessage.setText("Wow you finally used that command " + chat.getUserName()); + sendMessage.setChatId(chat.getId().toString()); + try { + absSender.sendMessage(sendMessage); + } catch (TelegramApiException e) { + BotLogger.error(LOGTAG, e); + } + } + }); + } + + @Override + public void processNonCommandUpdate(Update update) { + //All non command updated end here + } + + . + . + . + } +``` ## Example bots Open them and send them */help* command to get some information about their capabilities: From 00a8ccfac62d84929618ea6c4e75db5fa8c9b202 Mon Sep 17 00:00:00 2001 From: tschulz Date: Tue, 31 May 2016 11:14:56 +0200 Subject: [PATCH 05/21] fiy typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7fb0a014..be734913 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ Here is an example: @Override public void processNonCommandUpdate(Update update) { - //All non command updated end here + //All non command updates are passed to this method } . From 0ff2e13f9691a92d4db86f4965c6235e5bf7c0b4 Mon Sep 17 00:00:00 2001 From: tschulz Date: Tue, 31 May 2016 11:14:56 +0200 Subject: [PATCH 06/21] fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7fb0a014..be734913 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ Here is an example: @Override public void processNonCommandUpdate(Update update) { - //All non command updated end here + //All non command updates are passed to this method } . From e4b92fd0ad3eee00ad923627d2cc715c7862067c Mon Sep 17 00:00:00 2001 From: Rubenlagus Date: Tue, 31 May 2016 19:08:56 +0200 Subject: [PATCH 07/21] Update Telegram api docs --- .../telegrambots/api/objects/Chat.java | 6 ++++ .../telegrambots/api/objects/Message.java | 32 +++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/api/objects/Chat.java b/src/main/java/org/telegram/telegrambots/api/objects/Chat.java index 065aac32..18b77d8f 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/Chat.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/Chat.java @@ -28,6 +28,12 @@ public class Chat implements IBotApiObject { private static final String CHANNELCHATTYPE = "channel"; private static final String SUPERGROUPCHATTYPE = "supergroup"; @JsonProperty(ID_FIELD) + /** + * Unique identifier for this chat. + * This number may be greater than 32 bits and some programming languages may + * have difficulty/silent defects in interpreting it. But it smaller than 52 bits, + * so a signed 64 bit integer or double-precision float type are safe for storing this identifier. + */ private Long id; ///< Unique identifier for this chat, not exciding 1e13 by absolute value @JsonProperty(TYPE_FIELD) private String type; ///< Type of the chat, one of “private”, “group” or “channel” diff --git a/src/main/java/org/telegram/telegrambots/api/objects/Message.java b/src/main/java/org/telegram/telegrambots/api/objects/Message.java index f14c2894..7cc91759 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/Message.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/Message.java @@ -111,12 +111,40 @@ public class Message implements IBotApiObject { @JsonProperty(CAPTION_FIELD) private String caption; ///< Optional. Caption for the document, photo or video, 0-200 characters @JsonProperty(SUPERGROUPCREATED_FIELD) - private Boolean superGroupCreated; ///< Optional. Informs that the supergroup has been created + /** + * Optional. Service message: the supergroup has been created. + * This field can‘t be received in a message coming through updates, + * because bot can’t be a member of a supergroup when it is created. + * It can only be found in reply_to_message + * if someone replies to a very first message in a directly created supergroup. + */ + private Boolean superGroupCreated; @JsonProperty(CHANNELCHATCREATED_FIELD) - private Boolean channelChatCreated; ///< Optional. Informs that the channel has been created + /** + * Optional. Service message: the channel has been created. + * This field can‘t be received in a message coming through updates, + * because bot can’t be a member of a channel when it is created. + * It can only be found in reply_to_message if someone + * replies to a very first message in a channel. + */ + private Boolean channelChatCreated; @JsonProperty(MIGRATETOCHAT_FIELD) + /** + * Optional. The group has been migrated to a supergroup with the specified identifier. + * This number may be greater than 32 bits and some programming languages + * may have difficulty/silent defects in interpreting it. + * But it smaller than 52 bits, so a signed 64 bit integer or double-precision + * float type are safe for storing this identifier. + */ private Long migrateToChatId; ///< Optional. The chat has been migrated to a chat with specified identifier, not exceeding 1e13 by absolute value @JsonProperty(MIGRATEFROMCHAT_FIELD) + /** + * Optional. The supergroup has been migrated from a group with the specified identifier. + * This number may be greater than 32 bits and some programming languages + * may have difficulty/silent defects in interpreting it. + * But it smaller than 52 bits, so a signed 64 bit integer or double-precision + * float type are safe for storing this identifier. + */ private Long migrateFromChatId; ///< Optional. The chat has been migrated from a chat with specified identifier, not exceeding 1e13 by absolute value @JsonProperty(EDITDATE_FIELD) private Integer editDate; ///< Optional. Date the message was last edited in Unix time From 01f321d917729f86b5953d7cbfbe83552cb0f3dd Mon Sep 17 00:00:00 2001 From: Timo Schulz Date: Tue, 31 May 2016 19:53:06 +0200 Subject: [PATCH 08/21] remove commandbot example from readme --- README.md | 57 ------------------------------------------------------- 1 file changed, 57 deletions(-) diff --git a/README.md b/README.md index be734913..e339dcb9 100644 --- a/README.md +++ b/README.md @@ -44,64 +44,7 @@ Once done, you just need to create a `org.telegram.telegrambots.TelegramBotsApi` } ``` -## Command Bot -In order to make commands work for your bot by using the `org.telegram.telegrambots.bots.TelegramLongPollingCommandBot` you can -just make your Bot extend this class instead of the `org.telegram.telegrambots.bots.TelegramLongPollingBot`. - -Since this bot inherits from `org.telegram.telegrambots.bots.commands.ICommandRegistry` it is capable of register- and -deregistering commands to your bot. - -Here is an example: - -```java - - public class Main { - - private static final String LOGTAG = "MAIN"; - - public static void main(String[] args) { - TelegramBotsApi telegramBotsApi = new TelegramBotsApi(); - try { - telegramBotsApi.registerBot(new MySuperSpecialBotHandler()); - } catch (TelegramApiException e) { - BotLogger.error(LOGTAG, e); - } - } - } -``` -```java - - public class MySuperSpecialBotHandler extends TelegramLongPollingCommandBot { - - private static final String LOGTAG = "MY_SUPER_SPECIAL_BOT_HANDLER"; - - public MySuperSpecialBotHandler() { - register(new BotCommand("mycommand", "This command just demonstrates the use of commands") { - @Override - public void execute(AbsSender absSender, Chat chat, String[] arguments) { - SendMessage sendMessage = new SendMessage(); - sendMessage.setText("Wow you finally used that command " + chat.getUserName()); - sendMessage.setChatId(chat.getId().toString()); - try { - absSender.sendMessage(sendMessage); - } catch (TelegramApiException e) { - BotLogger.error(LOGTAG, e); - } - } - }); - } - - @Override - public void processNonCommandUpdate(Update update) { - //All non command updates are passed to this method - } - - . - . - . - } -``` ## Example bots Open them and send them */help* command to get some information about their capabilities: From 3a41c3fd5e11cd887d28ab2eeca7121587013137 Mon Sep 17 00:00:00 2001 From: Timo Schulz Date: Tue, 31 May 2016 19:57:54 +0200 Subject: [PATCH 09/21] Add sender of the command to the execute method signature, to be able to do sth with it's data (e.g. userId) --- .../org/telegram/telegrambots/bots/commands/BotCommand.java | 4 +++- .../telegram/telegrambots/bots/commands/CommandRegistry.java | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/bots/commands/BotCommand.java b/src/main/java/org/telegram/telegrambots/bots/commands/BotCommand.java index f46e9a10..24c1cd3c 100644 --- a/src/main/java/org/telegram/telegrambots/bots/commands/BotCommand.java +++ b/src/main/java/org/telegram/telegrambots/bots/commands/BotCommand.java @@ -1,6 +1,7 @@ package org.telegram.telegrambots.bots.commands; import org.telegram.telegrambots.api.objects.Chat; +import org.telegram.telegrambots.api.objects.User; import org.telegram.telegrambots.bots.AbsSender; /** @@ -69,8 +70,9 @@ public abstract class BotCommand { * Execute the command * * @param absSender absSender to send messages over + * @param user the user who sent the command * @param chat the chat, to be able to send replies * @param arguments passed arguments */ - public abstract void execute(AbsSender absSender, Chat chat, String[] arguments); + public abstract void execute(AbsSender absSender, User user, Chat chat, String[] arguments); } \ No newline at end of file diff --git a/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java b/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java index 033e297a..816fa1cc 100644 --- a/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java +++ b/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java @@ -86,7 +86,7 @@ public final class CommandRegistry implements ICommandRegistry { if (commandRegistryMap.containsKey(command)) { String[] parameters = Arrays.copyOfRange(commandSplit, 1, commandSplit.length); - commandRegistryMap.get(command).execute(absSender, message.getChat(), parameters); + commandRegistryMap.get(command).execute(absSender, message.getFrom(), message.getChat(), parameters); return true; } else if (defaultConsumer != null) { defaultConsumer.accept(absSender, message); From 7047811eaaa8dd94bb604c70f3847fd579cf29c0 Mon Sep 17 00:00:00 2001 From: Timo Schulz Date: Tue, 31 May 2016 20:01:50 +0200 Subject: [PATCH 10/21] change @author tag to full name + github username for reference --- .../telegrambots/bots/TelegramLongPollingCommandBot.java | 2 +- .../org/telegram/telegrambots/bots/commands/BotCommand.java | 2 +- .../telegram/telegrambots/bots/commands/CommandRegistry.java | 2 +- .../telegram/telegrambots/bots/commands/ICommandRegistry.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java index fba5b986..f9f8091f 100644 --- a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java +++ b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java @@ -14,7 +14,7 @@ import java.util.function.BiConsumer; /** * This class adds command functionality to the TelegramLongPollingBot * - * @author tschulz + * @author Timo Schulz (Mit0x2) */ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingBot implements ICommandRegistry { private final CommandRegistry commandRegistry; diff --git a/src/main/java/org/telegram/telegrambots/bots/commands/BotCommand.java b/src/main/java/org/telegram/telegrambots/bots/commands/BotCommand.java index 24c1cd3c..4b0935fa 100644 --- a/src/main/java/org/telegram/telegrambots/bots/commands/BotCommand.java +++ b/src/main/java/org/telegram/telegrambots/bots/commands/BotCommand.java @@ -7,7 +7,7 @@ import org.telegram.telegrambots.bots.AbsSender; /** * Representation of a command, which can be executed * - * @author tschulz + * @author Timo Schulz (Mit0x2) */ public abstract class BotCommand { public final static String COMMAND_INIT_CHARACTER = "/"; diff --git a/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java b/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java index 816fa1cc..1cbafd7f 100644 --- a/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java +++ b/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java @@ -12,7 +12,7 @@ import java.util.function.BiConsumer; /** * This class manages all the commands for a bot. You can register and deregister commands on demand * - * @author tschulz + * @author Timo Schulz (Mit0x2) */ public final class CommandRegistry implements ICommandRegistry { diff --git a/src/main/java/org/telegram/telegrambots/bots/commands/ICommandRegistry.java b/src/main/java/org/telegram/telegrambots/bots/commands/ICommandRegistry.java index 29559a0b..d6cd992c 100644 --- a/src/main/java/org/telegram/telegrambots/bots/commands/ICommandRegistry.java +++ b/src/main/java/org/telegram/telegrambots/bots/commands/ICommandRegistry.java @@ -10,7 +10,7 @@ import java.util.function.BiConsumer; /** * This Interface represents the gateway for registering and deregistering commands. * - * @author tschulz + * @author Timo Schulz (Mit0x2) */ public interface ICommandRegistry { From 80ee84a3671e85edae937e15236db64a792a7484 Mon Sep 17 00:00:00 2001 From: Ruben Bermudez Date: Tue, 31 May 2016 22:09:41 +0200 Subject: [PATCH 11/21] Added new webhook sample --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index e339dcb9..51f8c9b7 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,8 @@ https://telegram.me/TGlanguagesbot (**Send files uploding them**) https://telegram.me/RaeBot (**Inline support**) +https://telegram.me/SnowcrashBot (**Webhook support**) + You can see code for those bots at [TelegramBotsExample](https://github.com/rubenlagus/TelegramBotsExample) project. ## Telegram Bot API From e39e20c7f933b24ac7c69d0602736c28cca3ed12 Mon Sep 17 00:00:00 2001 From: Rubenlagus Date: Wed, 1 Jun 2016 00:32:14 +0200 Subject: [PATCH 12/21] Remove setter for unmodificable fields in replymarkup --- .../api/objects/replykeyboard/ForceReplyKeyboard.java | 6 ------ .../api/objects/replykeyboard/ReplyKeyboardHide.java | 8 +------- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ForceReplyKeyboard.java b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ForceReplyKeyboard.java index f086dc89..b03c2d98 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ForceReplyKeyboard.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ForceReplyKeyboard.java @@ -19,7 +19,6 @@ import java.io.IOException; * @date 22 of June of 2015 */ public class ForceReplyKeyboard implements ReplyKeyboard { - private static final String FORCEREPLY_FIELD = "force_reply"; private static final String SELECTIVE_FIELD = "selective"; /** @@ -55,11 +54,6 @@ public class ForceReplyKeyboard implements ReplyKeyboard { return forceReply; } - public ForceReplyKeyboard setForceReply(Boolean forceReply) { - this.forceReply = forceReply; - return this; - } - public Boolean getSelective() { return selective; } diff --git a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardHide.java b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardHide.java index 2b5f9529..b47e5fc4 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardHide.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/replykeyboard/ReplyKeyboardHide.java @@ -19,7 +19,6 @@ import java.io.IOException; * @date 20 of June of 2015 */ public class ReplyKeyboardHide implements ReplyKeyboard { - private static final String HIDEKEYBOARD_FIELD = "hide_keyboard"; private static final String SELECTIVE_FIELD = "selective"; @JsonProperty(HIDEKEYBOARD_FIELD) @@ -34,7 +33,7 @@ public class ReplyKeyboardHide implements ReplyKeyboard { public ReplyKeyboardHide() { super(); - this.selective = true; + this.hideKeyboard = true; } public ReplyKeyboardHide(JSONObject jsonObject) { @@ -51,11 +50,6 @@ public class ReplyKeyboardHide implements ReplyKeyboard { return hideKeyboard; } - public ReplyKeyboardHide setHideKeyboard(Boolean hideKeyboard) { - this.hideKeyboard = hideKeyboard; - return this; - } - public Boolean getSelective() { return selective; } From 56ac18e2634b0f1451be5c8bb4034c9dd97d6f9c Mon Sep 17 00:00:00 2001 From: Rubenlagus Date: Wed, 1 Jun 2016 02:06:35 +0200 Subject: [PATCH 13/21] Fix ForwardMessage method --- .../api/methods/ForwardMessage.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/api/methods/ForwardMessage.java b/src/main/java/org/telegram/telegrambots/api/methods/ForwardMessage.java index 14da4fa7..c7254102 100644 --- a/src/main/java/org/telegram/telegrambots/api/methods/ForwardMessage.java +++ b/src/main/java/org/telegram/telegrambots/api/methods/ForwardMessage.java @@ -19,12 +19,12 @@ import java.io.IOException; public class ForwardMessage extends BotApiMethod { public static final String PATH = "forwardmessage"; - public static final String CHATID_FIELD = "chat_id"; - public static final String FROMCHATID_FIELD = "from_chat_id"; - public static final String MESSAGEID_FIELD = "message_id"; - public static final String DISABLENOTIFICATION_FIELD = "disable_notification"; + private static final String CHATID_FIELD = "chat_id"; + private static final String FROMCHATID_FIELD = "from_chat_id"; + private static final String MESSAGEID_FIELD = "message_id"; + private static final String DISABLENOTIFICATION_FIELD = "disable_notification"; private String chatId; ///< Unique identifier for the chat to send the message to (or username for channels) - private Integer fromChatId; ///< Unique identifier for the chat where the original message was sent — User or GroupChat id + private String fromChatId; ///< Unique identifier for the chat where the original message was sent — User or GroupChat id private Integer messageId; ///< Unique message identifier /** * Optional. Sends the message silently. @@ -47,11 +47,17 @@ public class ForwardMessage extends BotApiMethod { return this; } - public Integer getFromChatId() { + @Deprecated + public ForwardMessage setFromChatId(Integer fromChatId) { + this.fromChatId = fromChatId.toString(); + return this; + } + + public String getFromChatId() { return fromChatId; } - public ForwardMessage setFromChatId(Integer fromChatId) { + public ForwardMessage setFromChatId(String fromChatId) { this.fromChatId = fromChatId; return this; } @@ -82,7 +88,7 @@ public class ForwardMessage extends BotApiMethod { gen.writeStartObject(); gen.writeStringField(METHOD_FIELD, PATH); gen.writeStringField(CHATID_FIELD, chatId); - gen.writeNumberField(FROMCHATID_FIELD, fromChatId); + gen.writeStringField(FROMCHATID_FIELD, fromChatId); gen.writeNumberField(MESSAGEID_FIELD, messageId); if (disableNotification != null) { gen.writeBooleanField(DISABLENOTIFICATION_FIELD, disableNotification); From 4b08b8ef7b28fa8807f6272105020a13e523b391 Mon Sep 17 00:00:00 2001 From: antonu17 Date: Thu, 2 Jun 2016 00:28:01 +0600 Subject: [PATCH 14/21] Set UTF-8 character set for reply_markup field in sendDocument(), sendPhoto(), sendVideo(), sendSticker(), sendAudio(), sendVoice() methods --- .../org/telegram/telegrambots/bots/AbsSender.java | 12 ++++++------ 1 file changed, 6 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..d2999b31 100644 --- a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java +++ b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java @@ -504,7 +504,7 @@ public abstract class AbsSender { builder.addTextBody(SendDocument.CHATID_FIELD, sendDocument.getChatId()); 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()); + builder.addTextBody(SendDocument.REPLYMARKUP_FIELD, sendDocument.getReplayMarkup().toJson().toString(), ContentType.create("text/plain", StandardCharsets.UTF_8)); } if (sendDocument.getReplayToMessageId() != null) { builder.addTextBody(SendDocument.REPLYTOMESSAGEID_FIELD, sendDocument.getReplayToMessageId().toString()); @@ -564,7 +564,7 @@ public abstract class AbsSender { builder.addTextBody(SendPhoto.CHATID_FIELD, sendPhoto.getChatId()); 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()); + builder.addTextBody(SendPhoto.REPLYMARKUP_FIELD, sendPhoto.getReplayMarkup().toJson().toString(), ContentType.create("text/plain", StandardCharsets.UTF_8)); } if (sendPhoto.getReplayToMessageId() != null) { builder.addTextBody(SendPhoto.REPLYTOMESSAGEID_FIELD, sendPhoto.getReplayToMessageId().toString()); @@ -624,7 +624,7 @@ public abstract class AbsSender { builder.addTextBody(SendVideo.CHATID_FIELD, sendVideo.getChatId()); 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()); + builder.addTextBody(SendVideo.REPLYMARKUP_FIELD, sendVideo.getReplayMarkup().toJson().toString(), ContentType.create("text/plain", StandardCharsets.UTF_8)); } if (sendVideo.getReplayToMessageId() != null) { builder.addTextBody(SendVideo.REPLYTOMESSAGEID_FIELD, sendVideo.getReplayToMessageId().toString()); @@ -703,7 +703,7 @@ public abstract class AbsSender { builder.addTextBody(SendSticker.CHATID_FIELD, sendSticker.getChatId()); 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()); + builder.addTextBody(SendSticker.REPLYMARKUP_FIELD, sendSticker.getReplayMarkup().toJson().toString(), ContentType.create("text/plain", StandardCharsets.UTF_8)); } if (sendSticker.getReplayToMessageId() != null) { builder.addTextBody(SendSticker.REPLYTOMESSAGEID_FIELD, sendSticker.getReplayToMessageId().toString()); @@ -765,7 +765,7 @@ public abstract class AbsSender { builder.addTextBody(SendAudio.CHATID_FIELD, sendAudio.getChatId()); 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()); + builder.addTextBody(SendAudio.REPLYMARKUP_FIELD, sendAudio.getReplayMarkup().toJson().toString(), ContentType.create("text/plain", StandardCharsets.UTF_8)); } if (sendAudio.getReplayToMessageId() != null) { builder.addTextBody(SendAudio.REPLYTOMESSAGEID_FIELD, sendAudio.getReplayToMessageId().toString()); @@ -847,7 +847,7 @@ public abstract class AbsSender { builder.addTextBody(SendVoice.CHATID_FIELD, sendVoice.getChatId()); 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()); + builder.addTextBody(SendVoice.REPLYMARKUP_FIELD, sendVoice.getReplayMarkup().toJson().toString(), ContentType.create("text/plain", StandardCharsets.UTF_8)); } if (sendVoice.getReplayToMessageId() != null) { builder.addTextBody(SendVoice.REPLYTOMESSAGEID_FIELD, sendVoice.getReplayToMessageId().toString()); From ee12175eeaffd919e3d240c43b7df5d767d42f04 Mon Sep 17 00:00:00 2001 From: antonu17 Date: Fri, 3 Jun 2016 23:22:02 +0600 Subject: [PATCH 15/21] 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 16/21] 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 17/21] 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 18/21] 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()); } From 1c1b302b52d3a32e9923cd7f5f1bb7aa5b47f827 Mon Sep 17 00:00:00 2001 From: Rubenlagus Date: Thu, 9 Jun 2016 22:31:14 +0200 Subject: [PATCH 19/21] Extract constant text plain content type --- .../telegram/telegrambots/bots/AbsSender.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java index d2999b31..c54d19e7 100644 --- a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java +++ b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java @@ -74,6 +74,8 @@ import static org.telegram.telegrambots.Constants.ERRORDESCRIPTIONFIELD; */ @SuppressWarnings("unused") public abstract class AbsSender { + private static final ContentType TEXT_PLAIN_CONTENT_TYPE = ContentType.create("text/plain", StandardCharsets.UTF_8); + private final ExecutorService exe = Executors.newSingleThreadExecutor(); private volatile CloseableHttpClient httpclient; private volatile RequestConfig requestConfig; @@ -504,13 +506,13 @@ public abstract class AbsSender { builder.addTextBody(SendDocument.CHATID_FIELD, sendDocument.getChatId()); 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(), ContentType.create("text/plain", StandardCharsets.UTF_8)); + builder.addTextBody(SendDocument.REPLYMARKUP_FIELD, sendDocument.getReplayMarkup().toJson().toString(), TEXT_PLAIN_CONTENT_TYPE); } if (sendDocument.getReplayToMessageId() != null) { builder.addTextBody(SendDocument.REPLYTOMESSAGEID_FIELD, sendDocument.getReplayToMessageId().toString()); } if (sendDocument.getCaption() != null) { - builder.addTextBody(SendDocument.CAPTION_FIELD, sendDocument.getCaption(), ContentType.create("text/plain", StandardCharsets.UTF_8)); + builder.addTextBody(SendDocument.CAPTION_FIELD, sendDocument.getCaption(), TEXT_PLAIN_CONTENT_TYPE); } if (sendDocument.getDisableNotification() != null) { builder.addTextBody(SendDocument.DISABLENOTIFICATION_FIELD, sendDocument.getDisableNotification().toString()); @@ -564,13 +566,13 @@ public abstract class AbsSender { builder.addTextBody(SendPhoto.CHATID_FIELD, sendPhoto.getChatId()); 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(), ContentType.create("text/plain", StandardCharsets.UTF_8)); + builder.addTextBody(SendPhoto.REPLYMARKUP_FIELD, sendPhoto.getReplayMarkup().toJson().toString(), TEXT_PLAIN_CONTENT_TYPE); } if (sendPhoto.getReplayToMessageId() != null) { builder.addTextBody(SendPhoto.REPLYTOMESSAGEID_FIELD, sendPhoto.getReplayToMessageId().toString()); } if (sendPhoto.getCaption() != null) { - builder.addTextBody(SendPhoto.CAPTION_FIELD, sendPhoto.getCaption(), ContentType.create("text/plain", StandardCharsets.UTF_8)); + builder.addTextBody(SendPhoto.CAPTION_FIELD, sendPhoto.getCaption(), TEXT_PLAIN_CONTENT_TYPE); } if (sendPhoto.getDisableNotification() != null) { builder.addTextBody(SendPhoto.DISABLENOTIFICATION_FIELD, sendPhoto.getDisableNotification().toString()); @@ -624,13 +626,13 @@ public abstract class AbsSender { builder.addTextBody(SendVideo.CHATID_FIELD, sendVideo.getChatId()); 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(), ContentType.create("text/plain", StandardCharsets.UTF_8)); + builder.addTextBody(SendVideo.REPLYMARKUP_FIELD, sendVideo.getReplayMarkup().toJson().toString(), TEXT_PLAIN_CONTENT_TYPE); } if (sendVideo.getReplayToMessageId() != null) { builder.addTextBody(SendVideo.REPLYTOMESSAGEID_FIELD, sendVideo.getReplayToMessageId().toString()); } if (sendVideo.getCaption() != null) { - builder.addTextBody(SendVideo.CAPTION_FIELD, sendVideo.getCaption(), ContentType.create("text/plain", StandardCharsets.UTF_8)); + builder.addTextBody(SendVideo.CAPTION_FIELD, sendVideo.getCaption(), TEXT_PLAIN_CONTENT_TYPE); } if (sendVideo.getDuration() != null) { builder.addTextBody(SendVideo.DURATION_FIELD, sendVideo.getDuration().toString()); @@ -703,7 +705,7 @@ public abstract class AbsSender { builder.addTextBody(SendSticker.CHATID_FIELD, sendSticker.getChatId()); 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(), ContentType.create("text/plain", StandardCharsets.UTF_8)); + builder.addTextBody(SendSticker.REPLYMARKUP_FIELD, sendSticker.getReplayMarkup().toJson().toString(), TEXT_PLAIN_CONTENT_TYPE); } if (sendSticker.getReplayToMessageId() != null) { builder.addTextBody(SendSticker.REPLYTOMESSAGEID_FIELD, sendSticker.getReplayToMessageId().toString()); @@ -765,7 +767,7 @@ public abstract class AbsSender { builder.addTextBody(SendAudio.CHATID_FIELD, sendAudio.getChatId()); 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(), ContentType.create("text/plain", StandardCharsets.UTF_8)); + builder.addTextBody(SendAudio.REPLYMARKUP_FIELD, sendAudio.getReplayMarkup().toJson().toString(), TEXT_PLAIN_CONTENT_TYPE); } if (sendAudio.getReplayToMessageId() != null) { builder.addTextBody(SendAudio.REPLYTOMESSAGEID_FIELD, sendAudio.getReplayToMessageId().toString()); @@ -847,7 +849,7 @@ public abstract class AbsSender { builder.addTextBody(SendVoice.CHATID_FIELD, sendVoice.getChatId()); 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(), ContentType.create("text/plain", StandardCharsets.UTF_8)); + builder.addTextBody(SendVoice.REPLYMARKUP_FIELD, sendVoice.getReplayMarkup().toJson().toString(), TEXT_PLAIN_CONTENT_TYPE); } if (sendVoice.getReplayToMessageId() != null) { builder.addTextBody(SendVoice.REPLYTOMESSAGEID_FIELD, sendVoice.getReplayToMessageId().toString()); From ef66e35827dbefe4b2e5611a559ef861c0063ec1 Mon Sep 17 00:00:00 2001 From: Rubenlagus Date: Thu, 9 Jun 2016 22:52:08 +0200 Subject: [PATCH 20/21] Document deprecated methods --- .../api/methods/send/SendAudio.java | 2 + .../api/methods/send/SendDocument.java | 24 ++++++ .../api/methods/send/SendPhoto.java | 8 ++ .../api/methods/send/SendSticker.java | 8 ++ .../api/methods/send/SendVideo.java | 8 ++ .../api/methods/send/SendVoice.java | 77 +++++++++++++++---- .../telegram/telegrambots/bots/AbsSender.java | 10 +-- 7 files changed, 115 insertions(+), 22 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 6f101844..141fdf02 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 @@ -88,6 +88,8 @@ public class SendAudio { * * @param audio Path to the new file in your server * @param audioName Name of the file itself + * + * @deprecated use {@link #setNewAudio(File)} or {@link #setNewAudio(InputStream)} instead. */ @Deprecated public SendAudio setNewAudio(String audio, String 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 6a1c8dd2..3a2da623 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 @@ -53,12 +53,26 @@ public class SendDocument { return document; } + /** + * Use this method to set the document to an document existing in Telegram system + * + * @param document File_id of the document to send + * @note The file_id must have already been received or sent by your bot + */ public SendDocument setDocument(String document) { this.document = document; this.isNewDocument = false; return this; } + /** + * Use this method to set the document to a new file + * + * @param document Path to the new file in your server + * @param documentName Name of the file itself + * + * @deprecated use {@link #setNewDocument(File)} or {@link #setNewDocument(InputStream)} instead. + */ @Deprecated public SendDocument setNewDocument(String document, String documentName) { this.document = document; @@ -67,6 +81,11 @@ public class SendDocument { return this; } + /** + * Use this method to set the document to a new file + * + * @param file New document file + */ public SendDocument setNewDocument(File file) { this.document = file.getName(); this.isNewDocument = true; @@ -74,6 +93,11 @@ public class SendDocument { return this; } + /** + * Use this method to set the document to a new file + * + * @param inputStream New document file + */ public SendDocument setNewDocument(InputStream inputStream) { this.isNewDocument = true; this.newDocumentStream = inputStream; 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 57694a11..5ed0d039 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 @@ -116,6 +116,14 @@ public class SendPhoto { return this; } + /** + * Use this method to set the photo to a new file + * + * @param photo Path to the new file in your server + * @param photoName Name of the file itself + * + * @deprecated use {@link #setNewPhoto(File)} or {@link #setNewPhoto(InputStream)} instead. + */ @Deprecated public SendPhoto setNewPhoto(String photo, String photoName) { this.photo = photo; 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 4f663297..0999ad76 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 @@ -75,6 +75,14 @@ public class SendSticker { return this; } + /** + * Use this method to set the sticker to a new file + * + * @param sticker Path to the new file in your server + * @param stickerName Name of the file itself + * + * @deprecated use {@link #setNewSticker(File)} or {@link #setNewSticker(InputStream)} instead. + */ @Deprecated public SendSticker setSticker(String sticker, String stickerName) { this.sticker = sticker; 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 956588b6..d4338594 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 @@ -150,6 +150,14 @@ public class SendVideo { return this; } + /** + * Use this method to set the video to a new file + * + * @param video Path to the new file in your server + * @param videoName Name of the file itself + * + * @deprecated use {@link #setNewVideo(File)} or {@link #setNewVideo(InputStream)} instead. + */ @Deprecated public SendVideo setNewVideo(String video, String videoName) { this.video = video; 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 27f20367..3ceeb59a 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 @@ -17,13 +17,14 @@ public class SendVoice { public static final String PATH = "sendvoice"; public static final String CHATID_FIELD = "chat_id"; - public static final String AUDIO_FIELD = "audio"; + public static final String VOICE_FIELD = "voice"; public static final String DISABLENOTIFICATION_FIELD = "disable_notification"; public static final String REPLYTOMESSAGEID_FIELD = "reply_to_message_id"; public static final String REPLYMARKUP_FIELD = "reply_markup"; public static final String DURATION_FIELD = "duration"; + private String chatId; ///< Unique identifier for the chat sent message to (Or username for channels) - private String audio; ///< Audio file to send. You can either pass a file_id as String to resend an audio that is already on the Telegram servers, or upload a new audio file using multipart/form-data. + private String voice; ///< Audio file to send. You can either pass a file_id as String to resend an audio that is already on the Telegram servers, or upload a new audio file using multipart/form-data. /** * Optional. Sends the message silently. iOS users will not receive a notification, Android * users will receive a notification with no sound. Other apps coming soon @@ -46,7 +47,7 @@ public class SendVoice { public String toString() { return "SendVoice{" + "chatId='" + chatId + '\'' + - ", audio='" + audio + '\'' + + ", voice='" + voice + '\'' + ", replayToMessageId=" + replayToMessageId + ", replayMarkup=" + replayMarkup + ", duration=" + duration + @@ -76,32 +77,74 @@ public class SendVoice { return this; } - public String getAudio() { - return audio; - } - - public SendVoice setAudio(String audio) { - this.audio = audio; - this.isNewVoice = false; - return this; + public String getVoice() { + return voice; } + /** + * @deprecated Use {@link #getVoice()} instead + */ @Deprecated - public SendVoice setNewAudio(String audio, String audioName) { - this.audio = audio; + public String getAudio() { + return voice; + } + + public SendVoice setVoice(String voice) { + this.voice = voice; this.isNewVoice = false; - this.voiceName = audioName; return this; } - public SendVoice setNewAudio(File file) { - this.audio = file.getName(); + /** + * @deprecated Use {@link #setVoice(String)} instead + */ + @Deprecated + public SendVoice setAudio(String voice) { + this.voice = voice; + this.isNewVoice = false; + return this; + } + + /** + * Use this method to set the voice to a new file + * + * @param voice Path to the new file in your server + * @param voiceName Name of the file itself + * + * @deprecated use {@link #setNewVoice(File)} or {@link #setNewVoice(InputStream)} instead. + */ + @Deprecated + public SendVoice setNewVoice(String voice, String voiceName) { + this.voice = voice; + this.isNewVoice = false; + this.voiceName = voiceName; + return this; + } + + /** + * Use this method to set the voice to a new file + * + * @param voice Path to the new file in your server + * @param voiceName Name of the file itself + * + * @deprecated use {@link #setNewVoice(File)} or {@link #setNewVoice(InputStream)} instead. + */ + @Deprecated + public SendVoice setNewAudio(String voice, String voiceName) { + this.voice = voice; + this.isNewVoice = false; + this.voiceName = voiceName; + return this; + } + + public SendVoice setNewVoice(File file) { + this.voice = file.getName(); this.isNewVoice = true; this.newVoiceFile = file; return this; } - public SendVoice setNewAudio(InputStream inputStream) { + public SendVoice setNewVoice(InputStream inputStream) { this.isNewVoice = true; this.newVoiceStream = inputStream; return this; diff --git a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java index 2b6109f0..4196fa9d 100644 --- a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java +++ b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java @@ -81,7 +81,7 @@ public abstract class AbsSender { private volatile RequestConfig requestConfig; private static final int SOCKET_TIMEOUT = 75 * 1000; - public AbsSender() { + AbsSender() { httpclient = HttpClientBuilder.create() .setSSLHostnameVerifier(new NoopHostnameVerifier()) .setConnectionTimeToLive(70, TimeUnit.SECONDS) @@ -879,11 +879,11 @@ public abstract class AbsSender { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody(SendVoice.CHATID_FIELD, sendVoice.getChatId()); if (sendVoice.getNewVoiceFile() != null) { - builder.addBinaryBody(SendVoice.AUDIO_FIELD, sendVoice.getNewVoiceFile()); + builder.addBinaryBody(SendVoice.VOICE_FIELD, sendVoice.getNewVoiceFile()); } else if (sendVoice.getNewVoiceStream() != null) { - builder.addBinaryBody(SendVoice.AUDIO_FIELD, sendVoice.getNewVoiceStream()); + builder.addBinaryBody(SendVoice.VOICE_FIELD, sendVoice.getNewVoiceStream()); } else { - builder.addBinaryBody(SendVoice.AUDIO_FIELD, new java.io.File(sendVoice.getAudio()), ContentType.create("audio/ogg"), sendVoice.getVoiceName()); + builder.addBinaryBody(SendVoice.VOICE_FIELD, new java.io.File(sendVoice.getVoice()), ContentType.create("audio/ogg"), sendVoice.getVoiceName()); } if (sendVoice.getReplayMarkup() != null) { builder.addTextBody(SendVoice.REPLYMARKUP_FIELD, sendVoice.getReplayMarkup().toJson().toString(), TEXT_PLAIN_CONTENT_TYPE); @@ -902,7 +902,7 @@ public abstract class AbsSender { } else { List nameValuePairs = new ArrayList<>(); nameValuePairs.add(new BasicNameValuePair(SendVoice.CHATID_FIELD, sendVoice.getChatId())); - nameValuePairs.add(new BasicNameValuePair(SendVoice.AUDIO_FIELD, sendVoice.getAudio())); + nameValuePairs.add(new BasicNameValuePair(SendVoice.VOICE_FIELD, sendVoice.getVoice())); if (sendVoice.getReplayMarkup() != null) { nameValuePairs.add(new BasicNameValuePair(SendVoice.REPLYMARKUP_FIELD, sendVoice.getReplayMarkup().toJson().toString())); } From 4fc3cc34cf83d40fd5ef8a756f676629e274eb05 Mon Sep 17 00:00:00 2001 From: Rubenlagus Date: Thu, 9 Jun 2016 23:04:08 +0200 Subject: [PATCH 21/21] Update version v2.3.3.3 --- README.md | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 51f8c9b7..1e25b41f 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Both ways are supported, but I recommend long polling method. ## Usage -Just import add the library to your project using [Maven, Gradly, ...](https://jitpack.io/#rubenlagus/TelegramBots/v2.3.3.2) or download the jar(including all dependencies) from [here](https://github.com/rubenlagus/TelegramBots/releases/tag/v2.3.3.2) +Just import add the library to your project using [Maven, Gradly, ...](https://jitpack.io/#rubenlagus/TelegramBots/v2.3.3.3) or download the jar(including all dependencies) from [here](https://github.com/rubenlagus/TelegramBots/releases/tag/v2.3.3.3) In order to use Long Polling mode, just create your own bot extending `org.telegram.telegrambots.bots.TelegramLongPollingBot`. diff --git a/pom.xml b/pom.xml index db2329b4..b73f825a 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ jar org.telegram telegrambots - 2.3.3.2 + 2.3.3.3 Telegram Bots https://telegram.me/JavaBotsApi