diff --git a/README.md b/README.md index af445b95..caff2fa9 100644 --- a/README.md +++ b/README.md @@ -14,14 +14,14 @@ 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`. 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 @@ -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 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 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); 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..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 @@ -2,6 +2,9 @@ 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 * @version 1.0 @@ -36,8 +39,11 @@ 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 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(); @@ -82,7 +88,10 @@ 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) { this.audio = audio; this.isNewAudio = true; @@ -90,6 +99,24 @@ public class SendAudio { return this; } + /** + * Use this method to set the audio to a new file + * + * @param file New audio file + */ + public SendAudio setNewAudio(File file) { + this.audio = file.getName(); + this.isNewAudio = true; + this.newAudioFile = file; + return this; + } + + public SendAudio setNewAudio(InputStream inputStream) { + this.isNewAudio = true; + this.newAudioStream = inputStream; + return this; + } + public Integer getReplayToMessageId() { return replayToMessageId; } @@ -148,6 +175,14 @@ public class SendAudio { return audioName; } + public File getNewAudioFile() { + return newAudioFile; + } + + public InputStream getNewAudioStream() { + return newAudioStream; + } + @Override public String toString() { return "SendAudio{" + @@ -158,7 +193,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..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 @@ -2,6 +2,9 @@ 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 * @version 1.0 @@ -28,8 +31,10 @@ 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 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(); @@ -48,12 +53,27 @@ 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; this.isNewDocument = true; @@ -61,6 +81,29 @@ 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; + this.newDocumentFile = file; + 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; + return this; + } + public boolean isNewDocument() { return isNewDocument; } @@ -69,6 +112,14 @@ public class SendDocument { return documentName; } + public File getNewDocumentFile() { + return newDocumentFile; + } + + public InputStream getNewDocumentStream() { + return newDocumentStream; + } + public Integer getReplayToMessageId() { return replayToMessageId; } @@ -118,7 +169,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..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 @@ -2,6 +2,9 @@ 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 * @version 1.0 @@ -30,7 +33,8 @@ public class SendPhoto { 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(); @@ -90,6 +94,14 @@ public class SendPhoto { return photoName; } + public File getNewPhotoFile() { + return newPhotoFile; + } + + public InputStream getNewPhotoStream() { + return newPhotoStream; + } + public Boolean getDisableNotification() { return disableNotification; } @@ -104,6 +116,15 @@ 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; this.isNewPhoto = true; @@ -111,6 +132,19 @@ public class SendPhoto { return this; } + public SendPhoto setNewPhoto(File file) { + this.photo = file.getName(); + this.newPhotoFile = file; + this.isNewPhoto = true; + return this; + } + + public SendPhoto setNewPhoto(InputStream inputStream) { + this.newPhotoStream = inputStream; + this.isNewPhoto = true; + return this; + } + @Override public String toString() { return "SendPhoto{" + @@ -120,7 +154,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..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 @@ -2,6 +2,9 @@ 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 * @version 1.0 @@ -26,8 +29,10 @@ 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 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(); @@ -70,6 +75,15 @@ 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; this.isNewSticker = true; @@ -77,6 +91,19 @@ public class SendSticker { 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; } @@ -99,6 +126,14 @@ public class SendSticker { return stickerName; } + public File getNewStickerFile() { + return newStickerFile; + } + + public InputStream getNewStickerStream() { + return newStickerStream; + } + @Override public String toString() { return "SendSticker{" + @@ -107,7 +142,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..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 @@ -2,6 +2,9 @@ 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 * @version 1.0 @@ -37,6 +40,8 @@ public class SendVideo { 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(); @@ -105,6 +110,14 @@ public class SendVideo { return videoName; } + public File getNewVideoFile() { + return newVideoFile; + } + + public InputStream getNewVideoStream() { + return newVideoStream; + } + public Boolean getDisableNotification() { return disableNotification; } @@ -137,6 +150,15 @@ 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; this.isNewVideo = true; @@ -144,6 +166,19 @@ public class SendVideo { return this; } + public SendVideo setNewVideo(File file) { + this.video = file.getName(); + this.isNewVideo = true; + this.newVideoFile = file; + return this; + } + + public SendVideo setNewVideo(InputStream inputStream) { + this.isNewVideo = true; + this.newVideoStream = inputStream; + return this; + } + @Override public String toString() { return "SendVideo{" + @@ -154,7 +189,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..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 @@ -2,6 +2,9 @@ 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 * @version 1.0 @@ -14,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 @@ -32,6 +36,8 @@ public class SendVoice { 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(); @@ -41,7 +47,7 @@ public class SendVoice { public String toString() { return "SendVoice{" + "chatId='" + chatId + '\'' + - ", audio='" + audio + '\'' + + ", voice='" + voice + '\'' + ", replayToMessageId=" + replayToMessageId + ", replayMarkup=" + replayMarkup + ", duration=" + duration + @@ -71,20 +77,76 @@ public class SendVoice { return this; } - public String getAudio() { - return audio; + public String getVoice() { + return voice; } - public SendVoice setAudio(String audio) { - this.audio = audio; + /** + * @deprecated Use {@link #getVoice()} instead + */ + @Deprecated + public String getAudio() { + return voice; + } + + public SendVoice setVoice(String voice) { + this.voice = voice; this.isNewVoice = false; return this; } - public SendVoice setNewAudio(String audio, String audioName) { - this.audio = audio; + /** + * @deprecated Use {@link #setVoice(String)} instead + */ + @Deprecated + public SendVoice setAudio(String voice) { + this.voice = voice; this.isNewVoice = false; - this.voiceName = audioName; + 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 setNewVoice(InputStream inputStream) { + this.isNewVoice = true; + this.newVoiceStream = inputStream; return this; } @@ -122,4 +184,12 @@ public class SendVoice { public String getVoiceName() { return voiceName; } + + public File getNewVoiceFile() { + return newVoiceFile; + } + + public InputStream getNewVoiceStream() { + return newVoiceStream; + } } 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 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; } diff --git a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java index ebefba8c..4196fa9d 100644 --- a/src/main/java/org/telegram/telegrambots/bots/AbsSender.java +++ b/src/main/java/org/telegram/telegrambots/bots/AbsSender.java @@ -74,12 +74,14 @@ 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; private static final int SOCKET_TIMEOUT = 75 * 1000; - public AbsSender() { + AbsSender() { httpclient = HttpClientBuilder.create() .setSSLHostnameVerifier(new NoopHostnameVerifier()) .setConnectionTimeToLive(70, TimeUnit.SECONDS) @@ -502,15 +504,21 @@ 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()); + 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()); + 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()); @@ -562,15 +570,21 @@ 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()); + 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()); + 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()); @@ -622,15 +636,21 @@ 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()); + 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()); + 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()); @@ -701,9 +721,15 @@ 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()); + 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()); + 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()); @@ -763,9 +789,15 @@ 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()); + 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()); + 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()); @@ -831,6 +863,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,9 +878,15 @@ 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()); + if (sendVoice.getNewVoiceFile() != null) { + builder.addBinaryBody(SendVoice.VOICE_FIELD, sendVoice.getNewVoiceFile()); + } else if (sendVoice.getNewVoiceStream() != null) { + builder.addBinaryBody(SendVoice.VOICE_FIELD, sendVoice.getNewVoiceStream()); + } else { + 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()); + 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()); @@ -863,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())); } 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 f46e9a10..4b0935fa 100644 --- a/src/main/java/org/telegram/telegrambots/bots/commands/BotCommand.java +++ b/src/main/java/org/telegram/telegrambots/bots/commands/BotCommand.java @@ -1,12 +1,13 @@ 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; /** * 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 = "/"; @@ -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 6d2d3eb7..1cbafd7f 100644 --- a/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java +++ b/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java @@ -10,16 +10,15 @@ import java.util.Map; import java.util.function.BiConsumer; /** - * @author tschulz + * This class manages all the commands for a bot. You can register and deregister commands on demand + * + * @author Timo Schulz (Mit0x2) */ 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; @@ -87,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); 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..d6cd992c 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 Timo Schulz (Mit0x2) */ public interface ICommandRegistry {