Skip to content

Commit

Permalink
1.0.10: Non-inline Telegram buttons. VK Inline buttons fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hevav committed Jan 27, 2023
1 parent 6413891 commit 356d4d5
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 14 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.9
1.0.10
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

setGroup("net.elytrium")
setVersion("1.0.9")
setVersion("1.0.10")

compileJava {
getOptions().setEncoding("UTF-8")
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/elytrium/limboauth/socialaddon/Addon.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ private void load() {
)
);

this.socialManager.registerKeyboard(this.keyboard);

this.socialManager.addMessageEvent((dbField, id, message) -> {
String lowercaseMessage = message.toLowerCase(Locale.ROOT);
if (Settings.IMP.MAIN.START_MESSAGES.contains(lowercaseMessage)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class SocialManager {
private final LinkedList<AbstractSocial> socialList;
private final LinkedList<SocialMessageListenerAdapter> messageEvents = new LinkedList<>();
private final HashMap<String, SocialButtonListenerAdapter> buttonEvents = new HashMap<>();
private final HashMap<String, String> buttonIdMap = new HashMap<>();

public SocialManager(AbstractSocial.Constructor... socialList) {
this.socialList = new LinkedList<>();
Expand All @@ -47,6 +48,11 @@ public SocialManager(AbstractSocial.Constructor... socialList) {
}

private void onMessageReceived(String dbField, Long id, String message) {
String buttonId = this.buttonIdMap.get(message);
if (buttonId != null) {
this.onButtonClicked(dbField, id, buttonId);
}

this.messageEvents.forEach(event -> {
try {
event.accept(dbField, id, message);
Expand All @@ -60,9 +66,10 @@ private void onMessageReceived(String dbField, Long id, String message) {
}

private void onButtonClicked(String dbField, Long id, String buttonId) {
if (this.buttonEvents.containsKey(buttonId)) {
SocialButtonListenerAdapter buttonListenerAdapter = this.buttonEvents.get(buttonId);
if (buttonListenerAdapter != null) {
try {
this.buttonEvents.get(buttonId).accept(dbField, id);
buttonListenerAdapter.accept(dbField, id);
} catch (Exception e) {
this.broadcastMessage(dbField, id, Settings.IMP.MAIN.STRINGS.SOCIAL_EXCEPTION_CAUGHT);
if (Settings.IMP.MAIN.DEBUG) {
Expand Down Expand Up @@ -117,12 +124,24 @@ public void registerHook(String dbField, Long id) {
.forEach(e -> e.onPlayerAdded(id));
}

public void registerKeyboard(List<List<AbstractSocial.ButtonItem>> keyboard) {
for (List<AbstractSocial.ButtonItem> items : keyboard) {
for (AbstractSocial.ButtonItem item : items) {
this.buttonIdMap.put(item.getValue(), item.getId());
}
}
}

public void registerButton(AbstractSocial.ButtonItem item) {
this.buttonIdMap.put(item.getValue(), item.getId());
}

public void broadcastMessage(SocialPlayer player, String message, List<List<AbstractSocial.ButtonItem>> item) {
this.broadcastMessage(player, message, item, AbstractSocial.ButtonVisibility.DEFAULT);
}

public void broadcastMessage(SocialPlayer player, String message,
List<List<AbstractSocial.ButtonItem>> item, AbstractSocial.ButtonVisibility visibility) {
List<List<AbstractSocial.ButtonItem>> item, AbstractSocial.ButtonVisibility visibility) {
this.socialList.stream()
.filter(e -> e.canSend(player))
.forEach(e -> e.sendMessage(player, message, item, visibility));
Expand All @@ -140,7 +159,7 @@ public void broadcastMessage(String dbField, Long id, String message, List<List<


public void broadcastMessage(String dbField, Long id, String message,
List<List<AbstractSocial.ButtonItem>> item, AbstractSocial.ButtonVisibility visibility) {
List<List<AbstractSocial.ButtonItem>> item, AbstractSocial.ButtonVisibility visibility) {
this.socialList.stream()
.filter(e -> e.getDbField().equals(dbField))
.forEach(e -> e.sendMessage(id, message, item, visibility));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public LimboAuthListener(Addon addon, LimboAuth plugin, Dao<SocialPlayer, String
);
}

this.socialManager.registerKeyboard(this.yesNoButtons);
this.socialManager.removeButtonEvent(ASK_NO_BTN);
this.socialManager.removeButtonEvent(ASK_YES_BTN);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboard;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardButton;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.generics.BotSession;
Expand Down Expand Up @@ -90,14 +93,27 @@ public void onPlayerRemoved(SocialPlayer player) {

@Override
public void sendMessage(Long id, String content, List<List<ButtonItem>> buttons, ButtonVisibility visibility) {
ReplyKeyboard keyboard = new InlineKeyboardMarkup(buttons.stream().map(row -> row.stream().map(e -> {

InlineKeyboardButton button = new InlineKeyboardButton();
button.setText(e.getValue());
button.setCallbackData(e.getId());

return button;
}).collect(Collectors.toList())).collect(Collectors.toList()));
ReplyKeyboard keyboard;
switch (visibility) {
case PREFER_INLINE: {
keyboard = new InlineKeyboardMarkup(buttons.stream().map(row -> row.stream().map(e -> {
InlineKeyboardButton button = new InlineKeyboardButton();
button.setText(e.getValue());
button.setCallbackData(e.getId());

return button;
}).collect(Collectors.toList())).collect(Collectors.toList()));
break;
}
default:
case PREFER_KEYBOARD: {
keyboard = new ReplyKeyboardMarkup(buttons.stream().map(row ->
new KeyboardRow(row.stream().map(e ->
new KeyboardButton(e.getValue())).collect(Collectors.toList())))
.collect(Collectors.toList()));
break;
}
}

this.bot.sendMessage(id, content, keyboard);
}
Expand Down

0 comments on commit 356d4d5

Please sign in to comment.