From 4991eef1f18b7a36ea7f82ab4c54aaf2b83269a5 Mon Sep 17 00:00:00 2001 From: Abbas Abou Daya Date: Sat, 18 Jul 2020 01:50:16 -0700 Subject: [PATCH] Add wiki and rename no space feature --- TelegramBots.wiki/abilities/Advanced.md | 30 +++++++++++++++++++ .../abilitybots/api/bot/BaseAbilityBot.java | 10 +++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/TelegramBots.wiki/abilities/Advanced.md b/TelegramBots.wiki/abilities/Advanced.md index 45f90300..ec2f4617 100644 --- a/TelegramBots.wiki/abilities/Advanced.md +++ b/TelegramBots.wiki/abilities/Advanced.md @@ -33,5 +33,35 @@ As an example, if you want to restrict the updates to photos only, then you may } ``` +## Custom Command Processing +### Command Prefix +Customizing the command prefix is as simple as overriding the `getCommandPrefix` method as shown below. +```java +@Override +protected String getCommandPrefix() { + return "!"; +} +``` + +### Command Regex Split +The method that the bot uses to capture command tokens is through the regex splitters. By default, it's set to `" "`. However, this can be customized. For example, +if you'd like to split on digits and whitespaces, then you may do the following: +```java +@Override +protected String getCommandRegexSplit() { + return "\\s\\d"; +} +``` +### Commands with Continuous Text +Feeling ambitious? You may allow your bot to process tokens that are technically attached to your command. Imagine you have a command +`/do` and you'd like users to send commands as `/do1` and still trigger the `do` ability. In order to do that, override the `allowContinuousText` function. +```java +@Override +protected boolean allowContinuousText() { + return true; +} +``` +Please note that this may cause ability overlap. If multiple abilities can match the same command, the longest match will be taken. For example, +if you have two abilities `do` and `do1`, the command `/do1` will trigger the `do1` ability. ## Statistics AbilityBot can accrue basic statistics about the usage of your abilities and replies. Simply `enableStats()` on an Ability builder or `enableStats()` on replies to activate this feature. Once activated, you may call `/stats` and the bot will print a basic list of statistics. At the moment, AbilityBot only tracks hits. In the future, this will be enhanced to track more stats. \ No newline at end of file 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 5fbc3ffb..4298a274 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 @@ -241,7 +241,7 @@ public abstract class BaseAbilityBot extends DefaultAbsSender implements Ability return " "; } - protected boolean allowNoSpaceText() { + protected boolean allowContinuousText() { return false; } @@ -507,13 +507,17 @@ public abstract class BaseAbilityBot extends DefaultAbsSender implements Ability return Trio.of(update, abilities.get(DEFAULT), new String[]{}); Ability ability; - String[] tokens = msg.getText().split(getCommandRegexSplit()); - if (allowNoSpaceText()) { + String[] tokens; + if (allowContinuousText()) { String abName = abilities.keySet().stream() .filter(name -> msg.getText().startsWith(format("%s%s", getCommandPrefix(), name))) .findFirst().orElse(DEFAULT); + tokens = msg.getText() + .replaceFirst(getCommandPrefix() + abName, "") + .split(getCommandRegexSplit()); ability = abilities.get(abName); } else { + tokens = msg.getText().split(getCommandRegexSplit()); if (tokens[0].startsWith(getCommandPrefix())) { String abilityToken = stripBotUsername(tokens[0].substring(1)).toLowerCase(); ability = abilities.get(abilityToken);