Skip to content

Commit

Permalink
API 5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenlagus committed Mar 9, 2021
1 parent ecca28c commit 6653ffe
Show file tree
Hide file tree
Showing 58 changed files with 915 additions and 143 deletions.
10 changes: 9 additions & 1 deletion TelegramBots.wiki/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
### <a id="5.1.0"></a>5.1.0 ###
1. Update Api version [5.1](https://core.telegram.org/bots/api-changelog#march-9-2021)
2. Bug fixing: #832, #841, #844, #851, #857
3. Update Spring boot version 2.4.3
4. Update Gradle docs
5. Added CommandMessage to extensions
6. Abilities: Inject bot instance to reply update consumer support for multiple reply declarations.

### <a id="5.0.1"></a>5.0.1 ###
1. Fixing couple of bugs from 5.0.0
2. Buf fixing: #794
2. Bug fixing: #794
3. Docs updated to reflect usage for version 5.0.0
4. EditMessageText setChatIId(Long) is removed to keep consistency

Expand Down
3 changes: 3 additions & 0 deletions TelegramBots.wiki/How-To-Update.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### <a id="5.1.0"></a>To version 5.1.0 ###
1. All users IDs fields are now Long type as per API guidelines.

### <a id="5.0.0"></a>To version 5.0.0 ###
1. ApiContextInitializer.init(); has been removed and is not required anymore, instead:
```java
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>org.telegram</groupId>
<artifactId>Bots</artifactId>
<packaging>pom</packaging>
<version>5.0.1</version>
<version>5.1.0</version>

<modules>
<module>telegrambots</module>
Expand Down
4 changes: 2 additions & 2 deletions telegrambots-abilities/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.telegram</groupId>
<artifactId>Bots</artifactId>
<version>5.0.1</version>
<version>5.1.0</version>
</parent>

<artifactId>telegrambots-abilities</artifactId>
Expand Down Expand Up @@ -84,7 +84,7 @@
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>5.0.1</version>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public abstract class BaseAbilityBot extends DefaultAbsSender implements Ability
// Reply registry
private List<Reply> replies;

public abstract int creatorId();
public abstract long creatorId();

protected BaseAbilityBot(String botToken, String botUsername, DBContext db, AbilityToggle toggle, DefaultBotOptions botOptions) {
super(botOptions);
Expand Down Expand Up @@ -155,28 +155,28 @@ public SilentSender silent() {
/**
* @return the map of <ID,User>
*/
public Map<Integer, User> users() {
public Map<Long, User> users() {
return db.getMap(USERS);
}

/**
* @return the map of <Username,ID>
*/
public Map<String, Integer> userIds() {
public Map<String, Long> userIds() {
return db.getMap(USER_ID);
}

/**
* @return a blacklist containing all the IDs of the banned users
*/
public Set<Integer> blacklist() {
public Set<Long> blacklist() {
return db.getSet(BLACKLIST);
}

/**
* @return an admin set of all the IDs of bot administrators
*/
public Set<Integer> admins() {
public Set<Long> admins() {
return db.getSet(ADMINS);
}

Expand Down Expand Up @@ -246,29 +246,29 @@ public String getBotUsername() {
return botUsername;
}

public Privacy getPrivacy(Update update, int id) {
public Privacy getPrivacy(Update update, long id) {
return isCreator(id) ?
CREATOR : isAdmin(id) ?
ADMIN : (isGroupUpdate(update) || isSuperGroupUpdate(update)) && isGroupAdmin(update, id) ?
GROUP_ADMIN : PUBLIC;
}

public boolean isGroupAdmin(Update update, int id) {
public boolean isGroupAdmin(Update update, long id) {
return isGroupAdmin(getChatId(update), id);
}

public boolean isGroupAdmin(long chatId, int id) {
public boolean isGroupAdmin(long chatId, long id) {
GetChatAdministrators admins = GetChatAdministrators.builder().chatId(Long.toString(chatId)).build();
return silent.execute(admins)
.orElse(new ArrayList<>()).stream()
.anyMatch(member -> member.getUser().getId() == id);
}

public boolean isCreator(int id) {
public boolean isCreator(long id) {
return id == creatorId();
}

public boolean isAdmin(Integer id) {
public boolean isAdmin(long id) {
return admins().contains(id);
}

Expand Down Expand Up @@ -508,7 +508,7 @@ boolean checkBlacklist(Update update) {
return true;
}

int id = user.getId();
long id = user.getId();
return id == creatorId() || !blacklist().contains(id);
}

Expand Down Expand Up @@ -549,7 +549,7 @@ boolean checkPrivacy(Trio<Update, Ability, String[]> trio) {
Update update = trio.a();
User user = AbilityUtils.getUser(update);
Privacy privacy;
int id = user.getId();
long id = user.getId();

privacy = getPrivacy(update, id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public Ability banUser() {
.input(1)
.action(ctx -> {
String username = stripTag(ctx.firstArg());
int userId = getUserIdSendError(username, ctx);
long userId = getUserIdSendError(username, ctx);
String bannedUser;

// Protection from abuse
Expand All @@ -304,7 +304,7 @@ public Ability banUser() {
bannedUser = addTag(username);
}

Set<Integer> blacklist = bot.blacklist();
Set<Long> blacklist = bot.blacklist();
if (blacklist.contains(userId))
sendMd(ABILITY_BAN_FAIL, ctx, escape(bannedUser));
else {
Expand All @@ -328,9 +328,9 @@ public Ability unbanUser() {
.input(1)
.action(ctx -> {
String username = stripTag(ctx.firstArg());
Integer userId = getUserIdSendError(username, ctx);
Long userId = getUserIdSendError(username, ctx);

Set<Integer> blacklist = bot.blacklist();
Set<Long> blacklist = bot.blacklist();

if (!blacklist.remove(userId))
bot.silent.sendMd(getLocalizedMessage(ABILITY_UNBAN_FAIL, ctx.user().getLanguageCode(), escape(username)), ctx.chatId());
Expand All @@ -352,9 +352,9 @@ public Ability promoteAdmin() {
.input(1)
.action(ctx -> {
String username = stripTag(ctx.firstArg());
Integer userId = getUserIdSendError(username, ctx);
Long userId = getUserIdSendError(username, ctx);

Set<Integer> admins = bot.admins();
Set<Long> admins = bot.admins();
if (admins.contains(userId))
sendMd(ABILITY_PROMOTE_FAIL, ctx, escape(username));
else {
Expand All @@ -376,9 +376,9 @@ public Ability demoteAdmin() {
.input(1)
.action(ctx -> {
String username = stripTag(ctx.firstArg());
Integer userId = getUserIdSendError(username, ctx);
Long userId = getUserIdSendError(username, ctx);

Set<Integer> admins = bot.admins();
Set<Long> admins = bot.admins();
if (admins.remove(userId)) {
sendMd(ABILITY_DEMOTE_SUCCESS, ctx, escape(username));
} else {
Expand All @@ -400,8 +400,8 @@ public Ability claimCreator() {
.privacy(CREATOR)
.input(0)
.action(ctx -> {
Set<Integer> admins = bot.admins();
int id = bot.creatorId();
Set<Long> admins = bot.admins();
long id = bot.creatorId();

if (admins.contains(id))
send(ABILITY_CLAIM_FAIL, ctx);
Expand All @@ -420,7 +420,7 @@ public Ability claimCreator() {
* @return the user
*/
private User getUser(String username) {
Integer id = bot.userIds().get(username.toLowerCase());
Long id = bot.userIds().get(username.toLowerCase());
if (id == null) {
throw new IllegalStateException(format("Could not find ID corresponding to username [%s]", username));
}
Expand All @@ -434,7 +434,7 @@ private User getUser(String username) {
* @param id the id of the required user
* @return the user
*/
private User getUser(int id) {
private User getUser(long id) {
User user = bot.users().get(id);
if (user == null) {
throw new IllegalStateException(format("Could not find user corresponding to id [%d]", id));
Expand All @@ -450,7 +450,7 @@ private User getUser(int id) {
* @param ctx the message context with the originating user
* @return the id of the user
*/
private int getUserIdSendError(String username, MessageContext ctx) {
private long getUserIdSendError(String username, MessageContext ctx) {
try {
return getUser(username).getId();
} catch (IllegalStateException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public enum Flag implements Predicate<Update> {
PRECHECKOUT_QUERY(Update::hasPreCheckoutQuery),
POLL(Update::hasPoll),
POLL_ANSWER(Update::hasPollAnswer),
MY_CHAT_MEMBER(Update::hasMyChatMember),
CHAT_MEMBER(Update::hasChatMember),


// Message Flags
REPLY(upd -> MESSAGE.test(upd) && upd.getMessage().isReply()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* Helper and utility methods
*/
public final class AbilityUtils {
public static User EMPTY_USER = new User(0, "", false);
public static User EMPTY_USER = new User(0L, "", false);

private AbilityUtils() {

Expand Down Expand Up @@ -82,6 +82,10 @@ private static User getUserElseThrow(Update update) {
return update.getPreCheckoutQuery().getFrom();
} else if (POLL_ANSWER.test(update)) {
return update.getPollAnswer().getUser();
} else if (MY_CHAT_MEMBER.test(update)) {
return update.getMyChatMember().getFrom();
} else if (CHAT_MEMBER.test(update)) {
return update.getChatMember().getFrom();
} else if (POLL.test(update)) {
return EMPTY_USER;
} else {
Expand Down Expand Up @@ -146,23 +150,27 @@ public static Long getChatId(Update update) {
} else if (CALLBACK_QUERY.test(update)) {
return update.getCallbackQuery().getMessage().getChatId();
} else if (INLINE_QUERY.test(update)) {
return (long) update.getInlineQuery().getFrom().getId();
return update.getInlineQuery().getFrom().getId();
} else if (CHANNEL_POST.test(update)) {
return update.getChannelPost().getChatId();
} else if (EDITED_CHANNEL_POST.test(update)) {
return update.getEditedChannelPost().getChatId();
} else if (EDITED_MESSAGE.test(update)) {
return update.getEditedMessage().getChatId();
} else if (CHOSEN_INLINE_QUERY.test(update)) {
return (long) update.getChosenInlineQuery().getFrom().getId();
return update.getChosenInlineQuery().getFrom().getId();
} else if (SHIPPING_QUERY.test(update)) {
return (long) update.getShippingQuery().getFrom().getId();
return update.getShippingQuery().getFrom().getId();
} else if (PRECHECKOUT_QUERY.test(update)) {
return (long) update.getPreCheckoutQuery().getFrom().getId();
return update.getPreCheckoutQuery().getFrom().getId();
} else if (POLL_ANSWER.test(update)) {
return (long) update.getPollAnswer().getUser().getId();
return update.getPollAnswer().getUser().getId();
} else if (POLL.test(update)) {
return (long) EMPTY_USER.getId();
return EMPTY_USER.getId();
} else if (MY_CHAT_MEMBER.test(update)) {
return update.getMyChatMember().getChat().getId();
} else if (CHAT_MEMBER.test(update)) {
return update.getChatMember().getChat().getId();
} else {
throw new IllegalStateException("Could not retrieve originating chat ID from update");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import static org.telegram.abilitybots.api.db.MapDBContext.offlineInstance;

class AbilityBotI18nTest {
private static final User NO_LANGUAGE_USER = new User(1, "first", false, "last", "username", null, false, false, false);
private static final User ITALIAN_USER = new User(2, "first", false, "last", "username", "it-IT", false, false, false);
private static final User NO_LANGUAGE_USER = new User(1L, "first", false, "last", "username", null, false, false, false);
private static final User ITALIAN_USER = new User(2L, "first", false, "last", "username", "it-IT", false, false, false);

private DBContext db;
private NoPublicCommandsBot bot;
Expand Down Expand Up @@ -76,7 +76,7 @@ public static class NoPublicCommandsBot extends AbilityBot {
}

@Override
public int creatorId() {
public long creatorId() {
return 1;
}
}
Expand Down
Loading

0 comments on commit 6653ffe

Please sign in to comment.