From e6aae3c28283cbe2c024e735e4d92889ec0c06d4 Mon Sep 17 00:00:00 2001 From: Abbas Abou Daya Date: Sat, 18 Jul 2020 01:27:39 -0700 Subject: [PATCH] Allow users to customize command prefix, split regex and NoSpaceText --- .../abilitybots/api/bot/BaseAbilityBot.java | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/bot/BaseAbilityBot.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/bot/BaseAbilityBot.java index ab2dd1b5..5fbc3ffb 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/bot/BaseAbilityBot.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/bot/BaseAbilityBot.java @@ -233,6 +233,18 @@ public abstract class BaseAbilityBot extends DefaultAbsSender implements Ability return true; } + protected String getCommandPrefix() { + return "/"; + } + + protected String getCommandRegexSplit() { + return " "; + } + + protected boolean allowNoSpaceText() { + return false; + } + /** * Registers the declared abilities using method reflection. Also, replies are accumulated using the built abilities and standalone methods that return a Reply. *

@@ -494,17 +506,23 @@ public abstract class BaseAbilityBot extends DefaultAbsSender implements Ability if (!update.hasMessage() || !msg.hasText()) return Trio.of(update, abilities.get(DEFAULT), new String[]{}); - String[] tokens = msg.getText().split(" "); - - if (tokens[0].startsWith("/")) { - String abilityToken = stripBotUsername(tokens[0].substring(1)).toLowerCase(); - Ability ability = abilities.get(abilityToken); - tokens = Arrays.copyOfRange(tokens, 1, tokens.length); - return Trio.of(update, ability, tokens); + Ability ability; + String[] tokens = msg.getText().split(getCommandRegexSplit()); + if (allowNoSpaceText()) { + String abName = abilities.keySet().stream() + .filter(name -> msg.getText().startsWith(format("%s%s", getCommandPrefix(), name))) + .findFirst().orElse(DEFAULT); + ability = abilities.get(abName); } else { - Ability ability = abilities.get(DEFAULT); - return Trio.of(update, ability, tokens); + if (tokens[0].startsWith(getCommandPrefix())) { + String abilityToken = stripBotUsername(tokens[0].substring(1)).toLowerCase(); + ability = abilities.get(abilityToken); + tokens = Arrays.copyOfRange(tokens, 1, tokens.length); + } else { + ability = abilities.get(DEFAULT); + } } + return Trio.of(update, ability, tokens); } private String stripBotUsername(String token) {