From 71991d0349427041c791998686ddd4937be7d142 Mon Sep 17 00:00:00 2001 From: Cadiducho Date: Mon, 4 Dec 2023 18:02:11 +0100 Subject: [PATCH] Telegram Bot API 6.7 --- build.gradle | 2 +- .../com/cadiducho/telegrambotapi/BotAPI.java | 29 +++++++++--- .../telegrambotapi/BotDescription.java | 3 ++ .../com/cadiducho/telegrambotapi/BotName.java | 25 ++++++++++ .../telegrambotapi/ChatMemberUpdated.java | 7 ++- .../SwitchInlineQueryChosenChat.java | 46 +++++++++++++++++++ .../cadiducho/telegrambotapi/TelegramBot.java | 43 +++++++++++++++-- .../telegrambotapi/WriteAccessAllowed.java | 25 +++++++++- .../inline/InlineKeyboardButton.java | 9 +++- .../inline/InlineQueryResultsButton.java | 42 +++++++++++++++++ .../telegrambotapi/TelegramBotTest.java | 39 ++++++++++------ 11 files changed, 238 insertions(+), 32 deletions(-) create mode 100644 src/main/java/com/cadiducho/telegrambotapi/BotName.java create mode 100644 src/main/java/com/cadiducho/telegrambotapi/SwitchInlineQueryChosenChat.java create mode 100644 src/main/java/com/cadiducho/telegrambotapi/inline/InlineQueryResultsButton.java diff --git a/build.gradle b/build.gradle index 6cee164..51e955b 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group = 'com.cadiducho' -version = '6.6' +version = '6.7' sourceCompatibility = JavaVersion.VERSION_11 java { diff --git a/src/main/java/com/cadiducho/telegrambotapi/BotAPI.java b/src/main/java/com/cadiducho/telegrambotapi/BotAPI.java index e9c3769..ea664f5 100644 --- a/src/main/java/com/cadiducho/telegrambotapi/BotAPI.java +++ b/src/main/java/com/cadiducho/telegrambotapi/BotAPI.java @@ -12,6 +12,7 @@ import com.cadiducho.telegrambotapi.handlers.BotUpdatesPoller; import com.cadiducho.telegrambotapi.inline.InlineKeyboardMarkup; import com.cadiducho.telegrambotapi.inline.InlineQueryResult; +import com.cadiducho.telegrambotapi.inline.InlineQueryResultsButton; import com.cadiducho.telegrambotapi.keyboard.ReplyKeyboardMarkup; import com.cadiducho.telegrambotapi.keyboard.ReplyKeyboardRemove; import com.cadiducho.telegrambotapi.payment.LabeledPrice; @@ -25,7 +26,7 @@ /** * Interface to build Telegrams Bots - * Telegram Bot API version 6.6 + * Telegram Bot API version 6.7 */ public interface BotAPI { @@ -1723,6 +1724,23 @@ default List getMyCommands() throws TelegramException { return getMyCommands(null, null); } + /** + * Use this method to change the bot's name. Returns True on success. + * @param name New bot name; 0-64 characters. Pass an empty string to remove the dedicated name for the given language. + * @param language_code A two-letter ISO 639-1 language code. If empty, the name will be shown to all users for whose language there is no dedicated name. + * @return True on success + * @throws com.cadiducho.telegrambotapi.exception.TelegramException if the method fails in Telegram servers + */ + Boolean setMyName(String name, String language_code) throws TelegramException; + + /** + * Use this method to get the current bot name for the given user language. Returns BotName on success. + * @param language_code + * @return Returns {@link BotName} on success. + * @throws TelegramException + */ + BotName getMyName(String language_code) throws TelegramException; + /** * Use this method to change the bot's description, which is shown in the chat with the bot if the chat is empty. Returns True on success. * @param description New bot description; 0-512 characters. Pass an empty string to remove the dedicated description for the given language. @@ -2052,7 +2070,6 @@ default Message sendSticker(Object chat_id, Object sticker, Boolean disable_noti * @param stickers A JSON-serialized list of 1-50 initial stickers to be added to the sticker set * @param sticker_format Format of stickers in the set, must be one of “static”, “animated”, “video” * @param sticker_type Type of stickers in the set, pass “regular”, “mask”, or “custom_emoji”. By default, a regular sticker set is created. - * @param sticker_type Type of stickers in the set, pass “regular” or “mask”. Custom emoji sticker sets can't be created via the Bot API at the moment. By default, a regular sticker set is created. * @param needs_repainting Pass True if stickers in the sticker set must be repainted to the color of text when used in messages, the accent color if used as emoji status, white on chat photos, or another appropriate color based on context; for custom emoji sticker sets only * @return True on success. * @throws com.cadiducho.telegrambotapi.exception.TelegramException if the method fails in Telegram servers @@ -2174,7 +2191,7 @@ default Message sendSticker(Object chat_id, Object sticker, Boolean disable_noti * @throws com.cadiducho.telegrambotapi.exception.TelegramException if the method fails in Telegram servers */ default Boolean answerInlineQuery(String inlineQueryId, List results) throws TelegramException { - return answerInlineQuery(inlineQueryId, results, null, null, null, null, null); + return answerInlineQuery(inlineQueryId, results, null, null, null, null); } /** @@ -2190,14 +2207,12 @@ default Boolean answerInlineQuery(String inlineQueryId, List * @param next_offset Pass the offset that a client should send in the next query with the same text to receive * more results. Pass an empty string if there are no more results or if you don‘t support * pagination. Offset length can’t exceed 64 bytes. - * @param switch_pm_text If passed, clients will display a button with specified text that switches the user to a private chat - * with the bot and sends the bot a start message with the parameter switch_pm_parameter - * @param switch_pm_parameter Deep-linking parameter for the /start message sent to the bot when user presses the switch button. 1-64 characters, only A-Z, a-z, 0-9, _ and - are allowed. + * @param button A JSON-serialized object describing a button to be shown above inline query results * @return On success, True is returned. * @throws com.cadiducho.telegrambotapi.exception.TelegramException if the method fails in Telegram servers */ Boolean answerInlineQuery(String inlineQueryId, List results, Integer cache_time, Boolean is_personal, String next_offset, - String switch_pm_text, String switch_pm_parameter) throws TelegramException; + InlineQueryResultsButton button) throws TelegramException; /** * Use this method to set the result of an interaction with a Web App and send a corresponding message on behalf of the user to the chat from which the query originated. diff --git a/src/main/java/com/cadiducho/telegrambotapi/BotDescription.java b/src/main/java/com/cadiducho/telegrambotapi/BotDescription.java index e40848b..6418aa6 100644 --- a/src/main/java/com/cadiducho/telegrambotapi/BotDescription.java +++ b/src/main/java/com/cadiducho/telegrambotapi/BotDescription.java @@ -11,6 +11,9 @@ import lombok.Setter; import lombok.ToString; +/** + * This object represents the bot's description. + */ @ToString @Getter @Setter public class BotDescription { diff --git a/src/main/java/com/cadiducho/telegrambotapi/BotName.java b/src/main/java/com/cadiducho/telegrambotapi/BotName.java new file mode 100644 index 0000000..c690dde --- /dev/null +++ b/src/main/java/com/cadiducho/telegrambotapi/BotName.java @@ -0,0 +1,25 @@ +/* + * The MIT License + * + * Copyright 2023 Cadiducho. + * Read more in https://github.com/Cadiducho/Telegram-Bot-API/blob/master/LICENSE + */ + +package com.cadiducho.telegrambotapi; + +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +/** + * This object represents the bot's name. + */ +@ToString +@Getter @Setter +public class BotName { + + /** + * The bot's name + */ + private String name; +} diff --git a/src/main/java/com/cadiducho/telegrambotapi/ChatMemberUpdated.java b/src/main/java/com/cadiducho/telegrambotapi/ChatMemberUpdated.java index 90becf7..9643330 100644 --- a/src/main/java/com/cadiducho/telegrambotapi/ChatMemberUpdated.java +++ b/src/main/java/com/cadiducho/telegrambotapi/ChatMemberUpdated.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2022 Cadiducho. + * Copyright 2023 Cadiducho. * Read more in https://github.com/Cadiducho/Telegram-Bot-API/blob/master/LICENSE */ @@ -49,4 +49,9 @@ public class ChatMemberUpdated { * Optional. Chat invite link, which was used by the user to join the chat; for joining by invite link events only. */ @Json(name = "invite_link") private ChatInviteLink inviteLink; + + /** + * Optional. True, if the user joined the chat via a chat folder invite link + */ + @Json(name = "via_chat_folder_invite_link") private Boolean viaChatFolderInviteLink; } diff --git a/src/main/java/com/cadiducho/telegrambotapi/SwitchInlineQueryChosenChat.java b/src/main/java/com/cadiducho/telegrambotapi/SwitchInlineQueryChosenChat.java new file mode 100644 index 0000000..24bbbdd --- /dev/null +++ b/src/main/java/com/cadiducho/telegrambotapi/SwitchInlineQueryChosenChat.java @@ -0,0 +1,46 @@ +/* + * The MIT License + * + * Copyright 2023 Cadiducho. + * Read more in https://github.com/Cadiducho/Telegram-Bot-API/blob/master/LICENSE + */ + +package com.cadiducho.telegrambotapi; + +import com.squareup.moshi.Json; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +/** + * This object represents an inline button that switches the current user to inline mode in a chosen chat, with an optional default inline query. + */ +@ToString +@Getter @Setter +public class SwitchInlineQueryChosenChat { + + /** + * Optional. The default inline query to be inserted in the input field. If left empty, only the bot's username will be inserted + */ + private String query; + + /** + * Optional. True, if private chats with users can be chosen + */ + @Json(name = "allow_user_chats") private Boolean allowUserChats; + + /** + * Optional. True, if private chats with bots can be chosen + */ + @Json(name = "allow_bot_chats") private Boolean allowBotChats; + + /** + * Optional. True, if group and supergroup chats can be chosen + */ + @Json(name = "allow_group_chats") private Boolean allowGroupChats; + + /** + * Optional. True, if channel chats can be chosen + */ + @Json(name = "allow_channel_chats") private Boolean allowChannelChats; +} diff --git a/src/main/java/com/cadiducho/telegrambotapi/TelegramBot.java b/src/main/java/com/cadiducho/telegrambotapi/TelegramBot.java index 575eeb6..e7e2f66 100644 --- a/src/main/java/com/cadiducho/telegrambotapi/TelegramBot.java +++ b/src/main/java/com/cadiducho/telegrambotapi/TelegramBot.java @@ -13,6 +13,7 @@ import com.cadiducho.telegrambotapi.handlers.DefaultBotUpdatesPoller; import com.cadiducho.telegrambotapi.inline.InlineKeyboardMarkup; import com.cadiducho.telegrambotapi.inline.InlineQueryResult; +import com.cadiducho.telegrambotapi.inline.InlineQueryResultsButton; import com.cadiducho.telegrambotapi.keyboard.ReplyKeyboardMarkup; import com.cadiducho.telegrambotapi.keyboard.ReplyKeyboardRemove; import com.cadiducho.telegrambotapi.payment.LabeledPrice; @@ -36,7 +37,7 @@ /** * Default implementation to build Telegrams Bots - * Telegram Bot API version 6.6 + * Telegram Bot API version 6.7 */ public class TelegramBot implements BotAPI { @@ -110,6 +111,10 @@ private Object getSafeChatId(Object rawChatId) { private void safeAdd(MultipartBody.Builder parameters, String str, Object obj) { + if (str == null) { + return; + } + //Check markup style if exists if (str.equals("reply_markup") && obj != null) { JsonAdapter adapter; @@ -136,7 +141,9 @@ else throw new IllegalArgumentException("The replyMarkup must be on of the follo } //Return normal values (check optionals -> null) - if (obj != null) parameters.addFormDataPart(str, obj.toString()); + if (obj != null) { + parameters.addFormDataPart(str, obj.toString()); + } } private void addFile(MultipartBody.Builder parameters, String name, Object obj, MediaType type) { @@ -1312,6 +1319,33 @@ public List getMyCommands(BotCommandScope scope, String language_cod return handleRequest(request, Types.newParameterizedType(List.class, BotCommand.class)); } + @Override + public Boolean setMyName(String name, String language_code) throws TelegramException { + final MultipartBody.Builder parameters = bodyBuilder(); + + safeAdd(parameters, "name", name); + safeAdd(parameters, "language_code", language_code); + + final Request request = new Request.Builder() + .url(apiUrl + "setMyName") + .post(parameters.build()) + .build(); + return handleRequest(request, Boolean.class); + } + + @Override + public BotName getMyName(String language_code) throws TelegramException { + final MultipartBody.Builder parameters = bodyBuilder(); + + safeAdd(parameters, "language_code", language_code); + + final Request request = new Request.Builder() + .url(apiUrl + "getMyName") + .post(parameters.build()) + .build(); + return handleRequest(request, BotName.class); + } + @Override public Boolean setMyDescription(String description, String language_code) throws TelegramException { final MultipartBody.Builder parameters = bodyBuilder(); @@ -1806,7 +1840,7 @@ public Boolean setStickerKeywords(String sticker, List keywords) throws @Override public Boolean answerInlineQuery(String inlineQueryId, List results, Integer cache_time, Boolean is_personal, String next_offset, - String switch_pm_text, String switch_pm_parameter) throws TelegramException { + InlineQueryResultsButton button) throws TelegramException { final MultipartBody.Builder parameters = bodyBuilder(); safeAdd(parameters, "inline_query_id", inlineQueryId); @@ -1814,8 +1848,7 @@ public Boolean answerInlineQuery(String inlineQueryId, List r safeAdd(parameters, "cache_time", cache_time); safeAdd(parameters, "is_personal", is_personal); safeAdd(parameters, "next_offset", next_offset); - safeAdd(parameters, "switch_pm_text", switch_pm_text); - safeAdd(parameters, "switch_pm_parameter", switch_pm_parameter); + safeAdd(parameters, "button", button); final Request request = new Request.Builder() .url(apiUrl + "answerInlineQuery") diff --git a/src/main/java/com/cadiducho/telegrambotapi/WriteAccessAllowed.java b/src/main/java/com/cadiducho/telegrambotapi/WriteAccessAllowed.java index 2a04bb5..102ad48 100644 --- a/src/main/java/com/cadiducho/telegrambotapi/WriteAccessAllowed.java +++ b/src/main/java/com/cadiducho/telegrambotapi/WriteAccessAllowed.java @@ -7,8 +7,31 @@ package com.cadiducho.telegrambotapi; +import com.squareup.moshi.Json; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + /** - * This object represents a service message about a user allowing a bot added to the attachment menu to write messages. Currently holds no information. + * This object represents a service message about a user allowing a bot to write messages after adding it to the attachment menu, launching a Web App from a link, or accepting an explicit request from a Web App sent by the method requestWriteAccess. */ +@ToString +@Getter @Setter public class WriteAccessAllowed { + + /** + * Optional. True, if the access was granted after the user accepted an explicit request from a Web App sent by the method requestWriteAccess + */ + @Json(name = "from_request") private Boolean fromRequest; + + /** + * Optional. Name of the Web App, if the access was granted when the Web App was launched from a link + */ + @Json(name = "web_app_name") private String webAppName; + + /** + * Optional. True, if the access was granted when the bot was added to the attachment or side menu + */ + @Json(name = "from_attachment_menu") private Boolean fromAttachmentMenu; + } diff --git a/src/main/java/com/cadiducho/telegrambotapi/inline/InlineKeyboardButton.java b/src/main/java/com/cadiducho/telegrambotapi/inline/InlineKeyboardButton.java index da9cc96..de44741 100644 --- a/src/main/java/com/cadiducho/telegrambotapi/inline/InlineKeyboardButton.java +++ b/src/main/java/com/cadiducho/telegrambotapi/inline/InlineKeyboardButton.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright 2018 Cadiducho. + * Copyright 2023 Cadiducho. * Read more in https://github.com/Cadiducho/Telegram-Bot-API/blob/master/LICENSE */ @@ -63,7 +63,12 @@ public class InlineKeyboardButton { * Can be empty, in which case only the bot’s username will be inserted. */ @Json(name = "switch_inline_query_current_chat") private String switchInlineQueryCurrentChat; - + + /** + * Optional. If set, pressing the button will prompt the user to select one of their chats of the specified type, open that chat and insert the bot's username and the specified inline query in the input field + */ + @Json(name = "switch_inline_query_chosen_chat") private String switchInlineQueryChosenChat; + /** * Optional. Specify True, to send a Pay button. * NOTE: This type of button must always be the first button in the first row. diff --git a/src/main/java/com/cadiducho/telegrambotapi/inline/InlineQueryResultsButton.java b/src/main/java/com/cadiducho/telegrambotapi/inline/InlineQueryResultsButton.java new file mode 100644 index 0000000..bbcd666 --- /dev/null +++ b/src/main/java/com/cadiducho/telegrambotapi/inline/InlineQueryResultsButton.java @@ -0,0 +1,42 @@ +/* + * The MIT License + * + * Copyright 2023 Cadiducho. + * Read more in https://github.com/Cadiducho/Telegram-Bot-API/blob/master/LICENSE + */ +package com.cadiducho.telegrambotapi.inline; + +import com.cadiducho.telegrambotapi.WebAppInfo; +import com.squareup.moshi.Json; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +/** + * This object represents a button to be shown above inline query results. You must use exactly one of the optional fields. + */ +@ToString +@Getter @Setter +public class InlineQueryResultsButton { + + /** + * Label text on the button + */ + private String text; + + /** + * Optional. Description of the Web App that will be launched when the user presses the button. + * The Web App will be able to switch back to the inline mode using the method switchInlineQuery inside the Web App. + */ + @Json(name = "web_app") WebAppInfo webApp; + + /** + * Optional. Deep-linking parameter for the /start message sent to the bot when a user presses the button. 1-64 characters, only A-Z, a-z, 0-9, _ and - are allowed. + * + * Example: An inline bot that sends YouTube videos can ask the user to connect the bot to their YouTube account to adapt search results accordingly. + * To do this, it displays a 'Connect your YouTube account' button above the results, or even before showing any. + * The user presses the button, switches to a private chat with the bot and, in doing so, passes a start parameter that instructs the bot to return an OAuth link. + * Once done, the bot can offer a switch_inline button so that the user can easily return to the chat where they wanted to use the bot's inline capabilities. + */ + @Json(name = "start_parameter") String startParameter; +} diff --git a/src/test/java/com/cadiducho/telegrambotapi/TelegramBotTest.java b/src/test/java/com/cadiducho/telegrambotapi/TelegramBotTest.java index 3728b68..3f122f4 100644 --- a/src/test/java/com/cadiducho/telegrambotapi/TelegramBotTest.java +++ b/src/test/java/com/cadiducho/telegrambotapi/TelegramBotTest.java @@ -152,21 +152,30 @@ void setAndGetBotCommands() throws TelegramException { @Nested class DescriptionTest { - @Test - void setAndGetBotDescription() throws TelegramException { - String description = "Bot description test"; - String languageCode = "en"; - assertTrue(bot.setMyDescription(description, languageCode)); - assertEquals(bot.getMyDescription(languageCode).getDescription(), description); - } - - @Test - void setAndGetBotShortDescription() throws TelegramException { - String shortDescription = "Bot short description test"; - String languageCode = "en"; - assertTrue(bot.setMyShortDescription(shortDescription, languageCode)); - assertEquals(bot.getMyShortDescription(languageCode).getShortDescription(), shortDescription); - } + @Test + void setAndGetBotName() throws TelegramException { + String name = "Bot name test"; + String languageCode = "en"; + + assertTrue(bot.setMyName(name, languageCode)); + assertEquals(bot.getMyName(languageCode).getName(), name); + } + + @Test + void setAndGetBotDescription() throws TelegramException { + String description = "Bot description test"; + String languageCode = "en"; + assertTrue(bot.setMyDescription(description, languageCode)); + assertEquals(bot.getMyDescription(languageCode).getDescription(), description); + } + + @Test + void setAndGetBotShortDescription() throws TelegramException { + String shortDescription = "Bot short description test"; + String languageCode = "en"; + assertTrue(bot.setMyShortDescription(shortDescription, languageCode)); + assertEquals(bot.getMyShortDescription(languageCode).getShortDescription(), shortDescription); + } } @Nested