From 986608d7adede40c548c0e8dc80436ee24a3a4ce Mon Sep 17 00:00:00 2001 From: tschulz Date: Tue, 31 May 2016 10:54:40 +0200 Subject: [PATCH 01/10] add java doc for CommandRegistry -> class purpose --- .../telegram/telegrambots/bots/commands/CommandRegistry.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java b/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java index 6d2d3eb7..5231c4f5 100644 --- a/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java +++ b/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java @@ -10,6 +10,8 @@ import java.util.Map; import java.util.function.BiConsumer; /** + * This class manages all the commands for a bot. You can register and deregister commands on demand + * * @author tschulz */ public final class CommandRegistry implements ICommandRegistry { From eefeb1e19f3ab33e8565c71d1fda5e081c8af7b6 Mon Sep 17 00:00:00 2001 From: tschulz Date: Tue, 31 May 2016 10:54:58 +0200 Subject: [PATCH 02/10] remove empty constructor --- .../telegram/telegrambots/bots/commands/CommandRegistry.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java b/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java index 5231c4f5..033e297a 100644 --- a/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java +++ b/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java @@ -19,9 +19,6 @@ public final class CommandRegistry implements ICommandRegistry { private final Map commandRegistryMap = new HashMap<>(); private BiConsumer defaultConsumer; - public CommandRegistry() { - } - @Override public void registerDefaultAction(BiConsumer defaultConsumer) { this.defaultConsumer = defaultConsumer; From d8abaec756a1ade9c0df166ff3ad003318437f05 Mon Sep 17 00:00:00 2001 From: tschulz Date: Tue, 31 May 2016 10:56:21 +0200 Subject: [PATCH 03/10] add javadoc (class purpose) for ICommandRegistry --- .../telegram/telegrambots/bots/commands/ICommandRegistry.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/telegram/telegrambots/bots/commands/ICommandRegistry.java b/src/main/java/org/telegram/telegrambots/bots/commands/ICommandRegistry.java index 676e1f8f..29559a0b 100644 --- a/src/main/java/org/telegram/telegrambots/bots/commands/ICommandRegistry.java +++ b/src/main/java/org/telegram/telegrambots/bots/commands/ICommandRegistry.java @@ -8,7 +8,9 @@ import java.util.Map; import java.util.function.BiConsumer; /** + * This Interface represents the gateway for registering and deregistering commands. * + * @author tschulz */ public interface ICommandRegistry { From cc9cda3226249d2061e0127ba05a2588f58f7ddf Mon Sep 17 00:00:00 2001 From: tschulz Date: Tue, 31 May 2016 11:13:28 +0200 Subject: [PATCH 04/10] add example for command bot to readme --- README.md | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d4fd43a1..7fb0a014 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ In order to use Long Polling mode, just create your own bot extending `org.teleg If you like to use Webhook, extend `org.telegram.telegrambots.bots.TelegramWebhookBot` -Once done, you just need to creat a `org.telegram.telegrambots.TelegramBotsApi`and register your bots: +Once done, you just need to create a `org.telegram.telegrambots.TelegramBotsApi`and register your bots: ```java @@ -44,7 +44,64 @@ Once done, you just need to creat a `org.telegram.telegrambots.TelegramBotsApi`a } ``` +## Command Bot +In order to make commands work for your bot by using the `org.telegram.telegrambots.bots.TelegramLongPollingCommandBot` you can +just make your Bot extend this class instead of the `org.telegram.telegrambots.bots.TelegramLongPollingBot`. + +Since this bot inherits from `org.telegram.telegrambots.bots.commands.ICommandRegistry` it is capable of register- and +deregistering commands to your bot. + +Here is an example: + +```java + + public class Main { + + private static final String LOGTAG = "MAIN"; + + public static void main(String[] args) { + TelegramBotsApi telegramBotsApi = new TelegramBotsApi(); + try { + telegramBotsApi.registerBot(new MySuperSpecialBotHandler()); + } catch (TelegramApiException e) { + BotLogger.error(LOGTAG, e); + } + } + } +``` +```java + + public class MySuperSpecialBotHandler extends TelegramLongPollingCommandBot { + + private static final String LOGTAG = "MY_SUPER_SPECIAL_BOT_HANDLER"; + + public MySuperSpecialBotHandler() { + register(new BotCommand("mycommand", "This command just demonstrates the use of commands") { + @Override + public void execute(AbsSender absSender, Chat chat, String[] arguments) { + SendMessage sendMessage = new SendMessage(); + sendMessage.setText("Wow you finally used that command " + chat.getUserName()); + sendMessage.setChatId(chat.getId().toString()); + try { + absSender.sendMessage(sendMessage); + } catch (TelegramApiException e) { + BotLogger.error(LOGTAG, e); + } + } + }); + } + + @Override + public void processNonCommandUpdate(Update update) { + //All non command updated end here + } + + . + . + . + } +``` ## Example bots Open them and send them */help* command to get some information about their capabilities: From 00a8ccfac62d84929618ea6c4e75db5fa8c9b202 Mon Sep 17 00:00:00 2001 From: tschulz Date: Tue, 31 May 2016 11:14:56 +0200 Subject: [PATCH 05/10] fiy typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7fb0a014..be734913 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ Here is an example: @Override public void processNonCommandUpdate(Update update) { - //All non command updated end here + //All non command updates are passed to this method } . From 0ff2e13f9691a92d4db86f4965c6235e5bf7c0b4 Mon Sep 17 00:00:00 2001 From: tschulz Date: Tue, 31 May 2016 11:14:56 +0200 Subject: [PATCH 06/10] fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7fb0a014..be734913 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ Here is an example: @Override public void processNonCommandUpdate(Update update) { - //All non command updated end here + //All non command updates are passed to this method } . From 01f321d917729f86b5953d7cbfbe83552cb0f3dd Mon Sep 17 00:00:00 2001 From: Timo Schulz Date: Tue, 31 May 2016 19:53:06 +0200 Subject: [PATCH 07/10] remove commandbot example from readme --- README.md | 57 ------------------------------------------------------- 1 file changed, 57 deletions(-) diff --git a/README.md b/README.md index be734913..e339dcb9 100644 --- a/README.md +++ b/README.md @@ -44,64 +44,7 @@ Once done, you just need to create a `org.telegram.telegrambots.TelegramBotsApi` } ``` -## Command Bot -In order to make commands work for your bot by using the `org.telegram.telegrambots.bots.TelegramLongPollingCommandBot` you can -just make your Bot extend this class instead of the `org.telegram.telegrambots.bots.TelegramLongPollingBot`. - -Since this bot inherits from `org.telegram.telegrambots.bots.commands.ICommandRegistry` it is capable of register- and -deregistering commands to your bot. - -Here is an example: - -```java - - public class Main { - - private static final String LOGTAG = "MAIN"; - - public static void main(String[] args) { - TelegramBotsApi telegramBotsApi = new TelegramBotsApi(); - try { - telegramBotsApi.registerBot(new MySuperSpecialBotHandler()); - } catch (TelegramApiException e) { - BotLogger.error(LOGTAG, e); - } - } - } -``` -```java - - public class MySuperSpecialBotHandler extends TelegramLongPollingCommandBot { - - private static final String LOGTAG = "MY_SUPER_SPECIAL_BOT_HANDLER"; - - public MySuperSpecialBotHandler() { - register(new BotCommand("mycommand", "This command just demonstrates the use of commands") { - @Override - public void execute(AbsSender absSender, Chat chat, String[] arguments) { - SendMessage sendMessage = new SendMessage(); - sendMessage.setText("Wow you finally used that command " + chat.getUserName()); - sendMessage.setChatId(chat.getId().toString()); - try { - absSender.sendMessage(sendMessage); - } catch (TelegramApiException e) { - BotLogger.error(LOGTAG, e); - } - } - }); - } - - @Override - public void processNonCommandUpdate(Update update) { - //All non command updates are passed to this method - } - - . - . - . - } -``` ## Example bots Open them and send them */help* command to get some information about their capabilities: From 3a41c3fd5e11cd887d28ab2eeca7121587013137 Mon Sep 17 00:00:00 2001 From: Timo Schulz Date: Tue, 31 May 2016 19:57:54 +0200 Subject: [PATCH 08/10] Add sender of the command to the execute method signature, to be able to do sth with it's data (e.g. userId) --- .../org/telegram/telegrambots/bots/commands/BotCommand.java | 4 +++- .../telegram/telegrambots/bots/commands/CommandRegistry.java | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/bots/commands/BotCommand.java b/src/main/java/org/telegram/telegrambots/bots/commands/BotCommand.java index f46e9a10..24c1cd3c 100644 --- a/src/main/java/org/telegram/telegrambots/bots/commands/BotCommand.java +++ b/src/main/java/org/telegram/telegrambots/bots/commands/BotCommand.java @@ -1,6 +1,7 @@ package org.telegram.telegrambots.bots.commands; import org.telegram.telegrambots.api.objects.Chat; +import org.telegram.telegrambots.api.objects.User; import org.telegram.telegrambots.bots.AbsSender; /** @@ -69,8 +70,9 @@ public abstract class BotCommand { * Execute the command * * @param absSender absSender to send messages over + * @param user the user who sent the command * @param chat the chat, to be able to send replies * @param arguments passed arguments */ - public abstract void execute(AbsSender absSender, Chat chat, String[] arguments); + public abstract void execute(AbsSender absSender, User user, Chat chat, String[] arguments); } \ No newline at end of file diff --git a/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java b/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java index 033e297a..816fa1cc 100644 --- a/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java +++ b/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java @@ -86,7 +86,7 @@ public final class CommandRegistry implements ICommandRegistry { if (commandRegistryMap.containsKey(command)) { String[] parameters = Arrays.copyOfRange(commandSplit, 1, commandSplit.length); - commandRegistryMap.get(command).execute(absSender, message.getChat(), parameters); + commandRegistryMap.get(command).execute(absSender, message.getFrom(), message.getChat(), parameters); return true; } else if (defaultConsumer != null) { defaultConsumer.accept(absSender, message); From 7047811eaaa8dd94bb604c70f3847fd579cf29c0 Mon Sep 17 00:00:00 2001 From: Timo Schulz Date: Tue, 31 May 2016 20:01:50 +0200 Subject: [PATCH 09/10] change @author tag to full name + github username for reference --- .../telegrambots/bots/TelegramLongPollingCommandBot.java | 2 +- .../org/telegram/telegrambots/bots/commands/BotCommand.java | 2 +- .../telegram/telegrambots/bots/commands/CommandRegistry.java | 2 +- .../telegram/telegrambots/bots/commands/ICommandRegistry.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java index fba5b986..f9f8091f 100644 --- a/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java +++ b/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingCommandBot.java @@ -14,7 +14,7 @@ import java.util.function.BiConsumer; /** * This class adds command functionality to the TelegramLongPollingBot * - * @author tschulz + * @author Timo Schulz (Mit0x2) */ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingBot implements ICommandRegistry { private final CommandRegistry commandRegistry; diff --git a/src/main/java/org/telegram/telegrambots/bots/commands/BotCommand.java b/src/main/java/org/telegram/telegrambots/bots/commands/BotCommand.java index 24c1cd3c..4b0935fa 100644 --- a/src/main/java/org/telegram/telegrambots/bots/commands/BotCommand.java +++ b/src/main/java/org/telegram/telegrambots/bots/commands/BotCommand.java @@ -7,7 +7,7 @@ import org.telegram.telegrambots.bots.AbsSender; /** * Representation of a command, which can be executed * - * @author tschulz + * @author Timo Schulz (Mit0x2) */ public abstract class BotCommand { public final static String COMMAND_INIT_CHARACTER = "/"; diff --git a/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java b/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java index 816fa1cc..1cbafd7f 100644 --- a/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java +++ b/src/main/java/org/telegram/telegrambots/bots/commands/CommandRegistry.java @@ -12,7 +12,7 @@ import java.util.function.BiConsumer; /** * This class manages all the commands for a bot. You can register and deregister commands on demand * - * @author tschulz + * @author Timo Schulz (Mit0x2) */ public final class CommandRegistry implements ICommandRegistry { diff --git a/src/main/java/org/telegram/telegrambots/bots/commands/ICommandRegistry.java b/src/main/java/org/telegram/telegrambots/bots/commands/ICommandRegistry.java index 29559a0b..d6cd992c 100644 --- a/src/main/java/org/telegram/telegrambots/bots/commands/ICommandRegistry.java +++ b/src/main/java/org/telegram/telegrambots/bots/commands/ICommandRegistry.java @@ -10,7 +10,7 @@ import java.util.function.BiConsumer; /** * This Interface represents the gateway for registering and deregistering commands. * - * @author tschulz + * @author Timo Schulz (Mit0x2) */ public interface ICommandRegistry { From 80ee84a3671e85edae937e15236db64a792a7484 Mon Sep 17 00:00:00 2001 From: Ruben Bermudez Date: Tue, 31 May 2016 22:09:41 +0200 Subject: [PATCH 10/10] Added new webhook sample --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index e339dcb9..51f8c9b7 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,8 @@ https://telegram.me/TGlanguagesbot (**Send files uploding them**) https://telegram.me/RaeBot (**Inline support**) +https://telegram.me/SnowcrashBot (**Webhook support**) + You can see code for those bots at [TelegramBotsExample](https://github.com/rubenlagus/TelegramBotsExample) project. ## Telegram Bot API