telegrambots-extensions java 9 supported

This commit is contained in:
chmilevfa 2018-07-08 01:46:26 +02:00
parent db9ae4f586
commit c0b903e689
10 changed files with 187 additions and 192 deletions

View File

@ -1,4 +1,4 @@
package org.telegram.telegrambots.bots.commandbot; package org.telegram.telegrambots.extensions.bots.commandbot;
import org.telegram.telegrambots.meta.ApiContext; import org.telegram.telegrambots.meta.ApiContext;
@ -7,10 +7,9 @@ import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.bots.AbsSender; import org.telegram.telegrambots.meta.bots.AbsSender;
import org.telegram.telegrambots.bots.DefaultBotOptions; import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.bots.TelegramLongPollingBot; import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.bots.commandbot.commands.BotCommand; import org.telegram.telegrambots.extensions.bots.commandbot.commands.CommandRegistry;
import org.telegram.telegrambots.bots.commandbot.commands.CommandRegistry; import org.telegram.telegrambots.extensions.bots.commandbot.commands.IBotCommand;
import org.telegram.telegrambots.bots.commandbot.commands.IBotCommand; import org.telegram.telegrambots.extensions.bots.commandbot.commands.ICommandRegistry;
import org.telegram.telegrambots.bots.commandbot.commands.ICommandRegistry;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;

View File

@ -1,4 +1,4 @@
package org.telegram.telegrambots.bots.commandbot.commands; package org.telegram.telegrambots.extensions.bots.commandbot.commands;
import org.telegram.telegrambots.meta.api.objects.Chat; import org.telegram.telegrambots.meta.api.objects.Chat;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;

View File

@ -1,4 +1,4 @@
package org.telegram.telegrambots.bots.commandbot.commands; package org.telegram.telegrambots.extensions.bots.commandbot.commands;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.bots.AbsSender; import org.telegram.telegrambots.meta.bots.AbsSender;

View File

@ -1,4 +1,4 @@
package org.telegram.telegrambots.bots.commandbot.commands; package org.telegram.telegrambots.extensions.bots.commandbot.commands;
import org.telegram.telegrambots.meta.api.objects.Chat; import org.telegram.telegrambots.meta.api.objects.Chat;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;

View File

@ -1,12 +1,8 @@
package org.telegram.telegrambots.bots.commandbot.commands; package org.telegram.telegrambots.extensions.bots.commandbot.commands;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.bots.AbsSender; import org.telegram.telegrambots.meta.bots.AbsSender;
import java.util.Collection;
import java.util.Map;
import java.util.function.BiConsumer;
/** /**
* This Interface represents the a Command that can be executed * This Interface represents the a Command that can be executed
* *

View File

@ -1,4 +1,4 @@
package org.telegram.telegrambots.bots.commandbot.commands; package org.telegram.telegrambots.extensions.bots.commandbot.commands;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.bots.AbsSender; import org.telegram.telegrambots.meta.bots.AbsSender;

View File

@ -1,116 +1,116 @@
package org.telegram.telegrambots.bots.commandbot.commands.helpCommand; package org.telegram.telegrambots.extensions.bots.commandbot.commands.helpCommand;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage; import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Chat; import org.telegram.telegrambots.meta.api.objects.Chat;
import org.telegram.telegrambots.meta.api.objects.User; import org.telegram.telegrambots.meta.api.objects.User;
import org.telegram.telegrambots.meta.bots.AbsSender; import org.telegram.telegrambots.meta.bots.AbsSender;
import org.telegram.telegrambots.bots.commandbot.commands.IBotCommand; import org.telegram.telegrambots.extensions.bots.commandbot.commands.IBotCommand;
import org.telegram.telegrambots.bots.commandbot.commands.ICommandRegistry; import org.telegram.telegrambots.extensions.bots.commandbot.commands.ICommandRegistry;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import java.util.Collection; import java.util.Collection;
/** /**
* A special bot command used for printing help messages similiar to the Linux man command. * A special bot command used for printing help messages similiar to the Linux man command.
* The commands printed by this command should implement the {@link IManCommand} interface to provide an extended description. * The commands printed by this command should implement the {@link IManCommand} interface to provide an extended description.
* @author Lukas Prediger(Chase) * @author Lukas Prediger(Chase)
* @version 1.0.0 * @version 1.0.0
* *
*/ */
public class HelpCommand extends ManCommand { public class HelpCommand extends ManCommand {
private static final String COMMAND_IDENTIFIER = "help"; private static final String COMMAND_IDENTIFIER = "help";
private static final String COMMAND_DESCRIPTION = "shows all commands. Use /help [command] for more info"; private static final String COMMAND_DESCRIPTION = "shows all commands. Use /help [command] for more info";
private static final String EXTENDED_DESCRIPTION = "This command displays all commands the bot has to offer.\n /help [command] can display deeper information"; private static final String EXTENDED_DESCRIPTION = "This command displays all commands the bot has to offer.\n /help [command] can display deeper information";
/** /**
* Returns the command and description of all supplied commands as a formatted String * Returns the command and description of all supplied commands as a formatted String
* @param botCommands the Commands that should be included in the String * @param botCommands the Commands that should be included in the String
* @return a formatted String containing command and description for all supplied commands * @return a formatted String containing command and description for all supplied commands
*/ */
public static String getHelpText(IBotCommand...botCommands) { public static String getHelpText(IBotCommand...botCommands) {
StringBuilder reply = new StringBuilder(); StringBuilder reply = new StringBuilder();
for (IBotCommand com : botCommands) { for (IBotCommand com : botCommands) {
reply.append(com.toString()).append(System.lineSeparator()).append(System.lineSeparator()); reply.append(com.toString()).append(System.lineSeparator()).append(System.lineSeparator());
} }
return reply.toString(); return reply.toString();
} }
/** /**
* Returns the command and description of all supplied commands as a formatted String * Returns the command and description of all supplied commands as a formatted String
* @param botCommands a collection of commands that should be included in the String * @param botCommands a collection of commands that should be included in the String
* @return a formatted String containing command and description for all supplied commands * @return a formatted String containing command and description for all supplied commands
*/ */
public static String getHelpText(Collection<IBotCommand> botCommands) { public static String getHelpText(Collection<IBotCommand> botCommands) {
return getHelpText(botCommands.toArray(new IBotCommand[botCommands.size()])); return getHelpText(botCommands.toArray(new IBotCommand[botCommands.size()]));
} }
/** /**
* Returns the command and description of all supplied commands as a formatted String * Returns the command and description of all supplied commands as a formatted String
* @param registry a commandRegistry which commands are formatted into the String * @param registry a commandRegistry which commands are formatted into the String
* @return a formatted String containing command and description for all supplied commands * @return a formatted String containing command and description for all supplied commands
*/ */
public static String getHelpText(ICommandRegistry registry) { public static String getHelpText(ICommandRegistry registry) {
return getHelpText(registry.getRegisteredCommands()); return getHelpText(registry.getRegisteredCommands());
} }
/** /**
* Reads the extended Description from a BotCommand. If the Command is not of Type {@link IManCommand}, it calls toString(); * Reads the extended Description from a BotCommand. If the Command is not of Type {@link IManCommand}, it calls toString();
* @param command a command the extended Descriptions is read from * @param command a command the extended Descriptions is read from
* @return the extended Description or the toString() if IManCommand is not implemented * @return the extended Description or the toString() if IManCommand is not implemented
*/ */
public static String getManText(IBotCommand command) { public static String getManText(IBotCommand command) {
return IManCommand.class.isInstance(command) ? getManText((IManCommand) command) : command.toString(); return IManCommand.class.isInstance(command) ? getManText((IManCommand) command) : command.toString();
} }
/** /**
* Reads the extended Description from a BotCommand; * Reads the extended Description from a BotCommand;
* @param command a command the extended Descriptions is read from * @param command a command the extended Descriptions is read from
* @return the extended Description * @return the extended Description
*/ */
public static String getManText(IManCommand command) { public static String getManText(IManCommand command) {
return command.toMan(); return command.toMan();
} }
/** /**
* Create a Help command with the standard Arguments. * Create a Help command with the standard Arguments.
*/ */
public HelpCommand() { public HelpCommand() {
super(COMMAND_IDENTIFIER, COMMAND_DESCRIPTION, EXTENDED_DESCRIPTION); super(COMMAND_IDENTIFIER, COMMAND_DESCRIPTION, EXTENDED_DESCRIPTION);
} }
/** /**
* Creates a Help Command with custom identifier, description and extended Description * Creates a Help Command with custom identifier, description and extended Description
* @param commandIdentifier the unique identifier of this command (e.g. the command string to enter into chat) * @param commandIdentifier the unique identifier of this command (e.g. the command string to enter into chat)
* @param description the description of this command * @param description the description of this command
* @param extendedDescription The extended Description for the Command, should provide detailed information about arguments and possible options * @param extendedDescription The extended Description for the Command, should provide detailed information about arguments and possible options
*/ */
public HelpCommand(String commandIdentifier, String description, String extendedDescription) { public HelpCommand(String commandIdentifier, String description, String extendedDescription) {
super(commandIdentifier, description, extendedDescription); super(commandIdentifier, description, extendedDescription);
} }
@Override @Override
public void execute(AbsSender absSender, User user, Chat chat, String[] arguments) { public void execute(AbsSender absSender, User user, Chat chat, String[] arguments) {
if (ICommandRegistry.class.isInstance(absSender)) { if (ICommandRegistry.class.isInstance(absSender)) {
ICommandRegistry registry = (ICommandRegistry) absSender; ICommandRegistry registry = (ICommandRegistry) absSender;
if (arguments.length > 0) { if (arguments.length > 0) {
IBotCommand command = registry.getRegisteredCommand(arguments[0]); IBotCommand command = registry.getRegisteredCommand(arguments[0]);
String reply = getManText(command); String reply = getManText(command);
try { try {
absSender.execute(new SendMessage(chat.getId(), reply).setParseMode("HTML")); absSender.execute(new SendMessage(chat.getId(), reply).setParseMode("HTML"));
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
e.printStackTrace(); e.printStackTrace();
} }
} else { } else {
String reply = getHelpText(registry); String reply = getHelpText(registry);
try { try {
absSender.execute(new SendMessage(chat.getId(), reply).setParseMode("HTML")); absSender.execute(new SendMessage(chat.getId(), reply).setParseMode("HTML"));
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
} }
} }

View File

@ -1,21 +1,21 @@
package org.telegram.telegrambots.bots.commandbot.commands.helpCommand; package org.telegram.telegrambots.extensions.bots.commandbot.commands.helpCommand;
/** /**
* Represents a Command that, aside the normal command description provides an extended Description similar to the output of the Linux <i>man</i> command * Represents a Command that, aside the normal command description provides an extended Description similar to the output of the Linux <i>man</i> command
* @author Lukas Prediger(Chase) * @author Lukas Prediger(Chase)
* @version 1.0.0 * @version 1.0.0
*/ */
public interface IManCommand { public interface IManCommand {
/** /**
* Returns the extended Description of this command * Returns the extended Description of this command
* @return the extendedDescription * @return the extendedDescription
*/ */
String getExtendedDescription(); String getExtendedDescription();
/** /**
* Returns a String Representations of the Command and it's extended Description. * Returns a String Representations of the Command and it's extended Description.
* @return a String representing the Command and it's extended Description * @return a String representing the Command and it's extended Description
*/ */
String toMan(); String toMan();
} }

View File

@ -1,41 +1,41 @@
package org.telegram.telegrambots.bots.commandbot.commands.helpCommand; package org.telegram.telegrambots.extensions.bots.commandbot.commands.helpCommand;
import org.telegram.telegrambots.bots.commandbot.commands.BotCommand; import org.telegram.telegrambots.extensions.bots.commandbot.commands.BotCommand;
public abstract class ManCommand extends BotCommand implements IManCommand { public abstract class ManCommand extends BotCommand implements IManCommand {
private final String extendedDescription; private final String extendedDescription;
/** /**
* Create a new ManCommand providing a commandIdentifier, a short description and the extended description * Create a new ManCommand providing a commandIdentifier, a short description and the extended description
* @param commandIdentifier the unique identifier of this command (e.g. the command string to enter into chat) * @param commandIdentifier the unique identifier of this command (e.g. the command string to enter into chat)
* @param description the description of this command * @param description the description of this command
* @param extendedDescription The extended Description for the Command, should provide detailed information about arguments and possible options * @param extendedDescription The extended Description for the Command, should provide detailed information about arguments and possible options
*/ */
public ManCommand(String commandIdentifier, String description, String extendedDescription) { public ManCommand(String commandIdentifier, String description, String extendedDescription) {
super(commandIdentifier, description); super(commandIdentifier, description);
this.extendedDescription = extendedDescription; this.extendedDescription = extendedDescription;
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.telegram.telegrambots.bots.commandbot.commands.helpCommand.IManCommand#getExtendedDescription() * @see org.telegram.telegrambots.extensions.bots.commandbot.commands.helpCommand.IManCommand#getExtendedDescription()
*/ */
@Override @Override
public String getExtendedDescription() { public String getExtendedDescription() {
return extendedDescription; return extendedDescription;
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.telegram.telegrambots.bots.commandbot.commands.helpCommand.IManCommand#toMan() * @see org.telegram.telegrambots.extensions.bots.commandbot.commands.helpCommand.IManCommand#toMan()
*/ */
@Override @Override
public String toMan() { public String toMan() {
StringBuilder sb = new StringBuilder(toString()); StringBuilder sb = new StringBuilder(toString());
sb.append(System.lineSeparator()) sb.append(System.lineSeparator())
.append("-----------------") .append("-----------------")
.append(System.lineSeparator()); .append(System.lineSeparator());
if (getExtendedDescription() != null) sb.append(getExtendedDescription()); if (getExtendedDescription() != null) sb.append(getExtendedDescription());
return sb.toString(); return sb.toString();
} }
} }

View File

@ -1,4 +1,4 @@
package org.telegram.telegrambots.bots.timedbot; package org.telegram.telegrambots.extensions.bots.timedbot;
import org.telegram.telegrambots.bots.TelegramLongPollingBot; import org.telegram.telegrambots.bots.TelegramLongPollingBot;