Skip to content

Commit

Permalink
Telegram Bot API 6.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadiducho committed Dec 4, 2023
1 parent 3d787ee commit 71991d0
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 32 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'com.cadiducho'
version = '6.6'
version = '6.7'
sourceCompatibility = JavaVersion.VERSION_11

java {
Expand Down
29 changes: 22 additions & 7 deletions src/main/java/com/cadiducho/telegrambotapi/BotAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +26,7 @@

/**
* Interface to build Telegrams Bots
* Telegram Bot API version 6.6
* Telegram Bot API version 6.7
*/
public interface BotAPI {

Expand Down Expand Up @@ -1723,6 +1724,23 @@ default List<BotCommand> 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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<InlineQueryResult> results) throws TelegramException {
return answerInlineQuery(inlineQueryId, results, null, null, null, null, null);
return answerInlineQuery(inlineQueryId, results, null, null, null, null);
}

/**
Expand All @@ -2190,14 +2207,12 @@ default Boolean answerInlineQuery(String inlineQueryId, List<InlineQueryResult>
* @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<InlineQueryResult> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import lombok.Setter;
import lombok.ToString;

/**
* This object represents the bot's description.
*/
@ToString
@Getter @Setter
public class BotDescription {
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/cadiducho/telegrambotapi/BotName.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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
*/

Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
43 changes: 38 additions & 5 deletions src/main/java/com/cadiducho/telegrambotapi/TelegramBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -1312,6 +1319,33 @@ public List<BotCommand> 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();
Expand Down Expand Up @@ -1806,16 +1840,15 @@ public Boolean setStickerKeywords(String sticker, List<String> keywords) throws

@Override
public Boolean answerInlineQuery(String inlineQueryId, List<InlineQueryResult> 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);
safeAdd(parameters, "results", moshi.adapter(Types.newParameterizedType(List.class, InlineQueryResult.class)).toJson(results));
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
Original file line number Diff line number Diff line change
@@ -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
*/

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Loading

0 comments on commit 71991d0

Please sign in to comment.