Browse Source
* remove IDE files from tracking * teach git how to ignore IDE files * remove .DS_Store files from tracking * Update version * bring code to new version * Reorganize configuration entries and add command bot entries * change config variable names * update database stuff to contain data for commands bot * implement hello command * implement help command * implement start command * implement stop command * register Commandshandler * rename WebHookExampleHandlers * rename webhook handlermaster
committed by
Ruben Bermudez
18 changed files with 375 additions and 62 deletions
@ -1,13 +0,0 @@ |
|||
<component name="libraryTable"> |
|||
<library name="Maven: com.github.rubenlagus:TelegramBots:v2.3.3.2"> |
|||
<CLASSES> |
|||
<root url="jar://$MAVEN_REPOSITORY$/com/github/rubenlagus/TelegramBots/v2.3.3.2/TelegramBots-v2.3.3.2.jar!/" /> |
|||
</CLASSES> |
|||
<JAVADOC> |
|||
<root url="jar://$MAVEN_REPOSITORY$/com/github/rubenlagus/TelegramBots/v2.3.3.2/TelegramBots-v2.3.3.2-javadoc.jar!/" /> |
|||
</JAVADOC> |
|||
<SOURCES> |
|||
<root url="jar://$MAVEN_REPOSITORY$/com/github/rubenlagus/TelegramBots/v2.3.3.2/TelegramBots-v2.3.3.2-sources.jar!/" /> |
|||
</SOURCES> |
|||
</library> |
|||
</component> |
|||
@ -0,0 +1,55 @@ |
|||
package org.telegram.commands; |
|||
|
|||
import org.telegram.database.DatabaseManager; |
|||
import org.telegram.telegrambots.TelegramApiException; |
|||
import org.telegram.telegrambots.api.methods.send.SendMessage; |
|||
import org.telegram.telegrambots.api.objects.Chat; |
|||
import org.telegram.telegrambots.api.objects.User; |
|||
import org.telegram.telegrambots.bots.AbsSender; |
|||
import org.telegram.telegrambots.bots.commands.BotCommand; |
|||
import org.telegram.telegrambots.logging.BotLogger; |
|||
|
|||
/** |
|||
* This command simply replies with a hello to the users command and |
|||
* sends them the 'kind' words back, which they send via command parameters |
|||
* |
|||
* @author Timo Schulz (Mit0x2) |
|||
*/ |
|||
public class HelloCommand extends BotCommand { |
|||
|
|||
private static final String LOGTAG = "HELLOCOMMAND"; |
|||
|
|||
public HelloCommand() { |
|||
super("hello", "Say hallo to this bot"); |
|||
} |
|||
|
|||
@Override |
|||
public void execute(AbsSender absSender, User user, Chat chat, String[] arguments) { |
|||
|
|||
if (!DatabaseManager.getInstance().getUserStateForCommandsBot(user.getId())) { |
|||
return; |
|||
} |
|||
|
|||
String userName = chat.getUserName(); |
|||
if (userName == null || userName.isEmpty()) { |
|||
userName = user.getFirstName() + " " + user.getLastName(); |
|||
} |
|||
|
|||
StringBuilder messageTextBuilder = new StringBuilder("Hello ").append(userName); |
|||
if (arguments != null && arguments.length > 0) { |
|||
messageTextBuilder.append("\n"); |
|||
messageTextBuilder.append("Thank you so much for your kind words:\n"); |
|||
messageTextBuilder.append(String.join(" ", arguments)); |
|||
} |
|||
|
|||
SendMessage answer = new SendMessage(); |
|||
answer.setChatId(chat.getId().toString()); |
|||
answer.setText(messageTextBuilder.toString()); |
|||
|
|||
try { |
|||
absSender.sendMessage(answer); |
|||
} catch (TelegramApiException e) { |
|||
BotLogger.error(LOGTAG, e); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,54 @@ |
|||
package org.telegram.commands; |
|||
|
|||
import org.telegram.database.DatabaseManager; |
|||
import org.telegram.telegrambots.TelegramApiException; |
|||
import org.telegram.telegrambots.api.methods.send.SendMessage; |
|||
import org.telegram.telegrambots.api.objects.Chat; |
|||
import org.telegram.telegrambots.api.objects.User; |
|||
import org.telegram.telegrambots.bots.AbsSender; |
|||
import org.telegram.telegrambots.bots.commands.BotCommand; |
|||
import org.telegram.telegrambots.bots.commands.ICommandRegistry; |
|||
import org.telegram.telegrambots.logging.BotLogger; |
|||
|
|||
/** |
|||
* This command helps the user to find the command they need |
|||
* |
|||
* @author Timo Schulz (Mit0x2) |
|||
*/ |
|||
public class HelpCommand extends BotCommand { |
|||
|
|||
private static final String LOGTAG = "HELPCOMMAND"; |
|||
|
|||
private final ICommandRegistry commandRegistry; |
|||
|
|||
public HelpCommand(ICommandRegistry commandRegistry) { |
|||
super("help", "Get all the commands this bot provides"); |
|||
this.commandRegistry = commandRegistry; |
|||
} |
|||
|
|||
@Override |
|||
public void execute(AbsSender absSender, User user, Chat chat, String[] strings) { |
|||
|
|||
if (!DatabaseManager.getInstance().getUserStateForCommandsBot(user.getId())) { |
|||
return; |
|||
} |
|||
|
|||
StringBuilder helpMessageBuilder = new StringBuilder("<b>Help</b>\n"); |
|||
helpMessageBuilder.append("These are the registered commands for this Bot:\n\n"); |
|||
|
|||
for (BotCommand botCommand : commandRegistry.getRegisteredCommands()) { |
|||
helpMessageBuilder.append(botCommand.toString()).append("\n\n"); |
|||
} |
|||
|
|||
SendMessage helpMessage = new SendMessage(); |
|||
helpMessage.setChatId(chat.getId().toString()); |
|||
helpMessage.enableHtml(true); |
|||
helpMessage.setText(helpMessageBuilder.toString()); |
|||
|
|||
try { |
|||
absSender.sendMessage(helpMessage); |
|||
} catch (TelegramApiException e) { |
|||
BotLogger.error(LOGTAG, e); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,51 @@ |
|||
package org.telegram.commands; |
|||
|
|||
import org.telegram.database.DatabaseManager; |
|||
import org.telegram.telegrambots.TelegramApiException; |
|||
import org.telegram.telegrambots.api.methods.send.SendMessage; |
|||
import org.telegram.telegrambots.api.objects.Chat; |
|||
import org.telegram.telegrambots.api.objects.User; |
|||
import org.telegram.telegrambots.bots.AbsSender; |
|||
import org.telegram.telegrambots.bots.commands.BotCommand; |
|||
import org.telegram.telegrambots.logging.BotLogger; |
|||
|
|||
/** |
|||
* This commands starts the conversation with the bot |
|||
* |
|||
* @author Timo Schulz (Mit0x2) |
|||
*/ |
|||
public class StartCommand extends BotCommand { |
|||
|
|||
public static final String LOGTAG = "STARTCOMMAND"; |
|||
|
|||
public StartCommand() { |
|||
super("start", "With this command you can start the Bot"); |
|||
} |
|||
|
|||
@Override |
|||
public void execute(AbsSender absSender, User user, Chat chat, String[] strings) { |
|||
DatabaseManager databseManager = DatabaseManager.getInstance(); |
|||
StringBuilder messageBuilder = new StringBuilder(); |
|||
|
|||
String userName = user.getFirstName() + " " + user.getLastName(); |
|||
|
|||
if (databseManager.getUserStateForCommandsBot(user.getId())) { |
|||
messageBuilder.append("Hi ").append(userName).append("\n"); |
|||
messageBuilder.append("i think we know each other already!"); |
|||
} else { |
|||
databseManager.setUserStateForCommandsBot(user.getId(), true); |
|||
messageBuilder.append("Welcome ").append(userName).append("\n"); |
|||
messageBuilder.append("this bot will demonstrate you the command feature of the Java TelegramBots API!"); |
|||
} |
|||
|
|||
SendMessage answer = new SendMessage(); |
|||
answer.setChatId(chat.getId().toString()); |
|||
answer.setText(messageBuilder.toString()); |
|||
|
|||
try { |
|||
absSender.sendMessage(answer); |
|||
} catch (TelegramApiException e) { |
|||
BotLogger.error(LOGTAG, e); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,48 @@ |
|||
package org.telegram.commands; |
|||
|
|||
import org.telegram.database.DatabaseManager; |
|||
import org.telegram.telegrambots.TelegramApiException; |
|||
import org.telegram.telegrambots.api.methods.send.SendMessage; |
|||
import org.telegram.telegrambots.api.objects.Chat; |
|||
import org.telegram.telegrambots.api.objects.User; |
|||
import org.telegram.telegrambots.bots.AbsSender; |
|||
import org.telegram.telegrambots.bots.commands.BotCommand; |
|||
import org.telegram.telegrambots.logging.BotLogger; |
|||
|
|||
/** |
|||
* This commands stops the conversation with the bot. |
|||
* Bot won't respond to user until he sends a start command |
|||
* |
|||
* @author Timo Schulz (Mit0x2) |
|||
*/ |
|||
public class StopCommand extends BotCommand { |
|||
|
|||
public static final String LOGTAG = "STOPCOMMAND"; |
|||
|
|||
/** |
|||
* Construct |
|||
*/ |
|||
public StopCommand() { |
|||
super("stop", "With this command you can stop the Bot"); |
|||
} |
|||
|
|||
@Override |
|||
public void execute(AbsSender absSender, User user, Chat chat, String[] arguments) { |
|||
DatabaseManager dbManager = DatabaseManager.getInstance(); |
|||
|
|||
if (dbManager.getUserStateForCommandsBot(user.getId())) { |
|||
dbManager.setUserStateForCommandsBot(user.getId(), false); |
|||
String userName = user.getFirstName() + " " + user.getLastName(); |
|||
|
|||
SendMessage answer = new SendMessage(); |
|||
answer.setChatId(chat.getId().toString()); |
|||
answer.setText("Good bye " + userName + "\n" + "Hope to see you soon!"); |
|||
|
|||
try { |
|||
absSender.sendMessage(answer); |
|||
} catch (TelegramApiException e) { |
|||
BotLogger.error(LOGTAG, e); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,82 @@ |
|||
package org.telegram.updateshandlers; |
|||
|
|||
import org.telegram.BotConfig; |
|||
import org.telegram.commands.HelloCommand; |
|||
import org.telegram.commands.HelpCommand; |
|||
import org.telegram.commands.StartCommand; |
|||
import org.telegram.commands.StopCommand; |
|||
import org.telegram.database.DatabaseManager; |
|||
import org.telegram.services.Emoji; |
|||
import org.telegram.telegrambots.TelegramApiException; |
|||
import org.telegram.telegrambots.api.methods.send.SendMessage; |
|||
import org.telegram.telegrambots.api.objects.Message; |
|||
import org.telegram.telegrambots.api.objects.Update; |
|||
import org.telegram.telegrambots.bots.TelegramLongPollingCommandBot; |
|||
import org.telegram.telegrambots.logging.BotLogger; |
|||
|
|||
/** |
|||
* This handler mainly works with commands to demonstrate the Commands feature of the API |
|||
* |
|||
* @author Timo Schulz (Mit0x2) |
|||
*/ |
|||
public class CommandsHandler extends TelegramLongPollingCommandBot { |
|||
|
|||
public static final String LOGTAG = "COMMANDSHANDLER"; |
|||
|
|||
/** |
|||
* Constructor. |
|||
*/ |
|||
public CommandsHandler() { |
|||
register(new HelloCommand()); |
|||
register(new StartCommand()); |
|||
register(new StopCommand()); |
|||
HelpCommand helpCommand = new HelpCommand(this); |
|||
register(helpCommand); |
|||
|
|||
registerDefaultAction((absSender, message) -> { |
|||
SendMessage commandUnknownMessage = new SendMessage(); |
|||
commandUnknownMessage.setChatId(message.getChatId().toString()); |
|||
commandUnknownMessage.setText("The command '" + message.getText() + "' is not known by this bot. Here comes some help " + Emoji.AMBULANCE); |
|||
try { |
|||
absSender.sendMessage(commandUnknownMessage); |
|||
} catch (TelegramApiException e) { |
|||
BotLogger.error(LOGTAG, e); |
|||
} |
|||
helpCommand.execute(absSender, message.getFrom(), message.getChat(), new String[] {}); |
|||
}); |
|||
} |
|||
|
|||
@Override |
|||
public void processNonCommandUpdate(Update update) { |
|||
|
|||
if (update.hasMessage()) { |
|||
Message message = update.getMessage(); |
|||
|
|||
if (!DatabaseManager.getInstance().getUserStateForCommandsBot(message.getFrom().getId())) { |
|||
return; |
|||
} |
|||
|
|||
if (message.hasText()) { |
|||
SendMessage echoMessage = new SendMessage(); |
|||
echoMessage.setChatId(message.getChatId().toString()); |
|||
echoMessage.setText("Hey heres your message:\n" + message.getText()); |
|||
|
|||
try { |
|||
sendMessage(echoMessage); |
|||
} catch (TelegramApiException e) { |
|||
BotLogger.error(LOGTAG, e); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public String getBotUsername() { |
|||
return BotConfig.COMMANDS_USER; |
|||
} |
|||
|
|||
@Override |
|||
public String getBotToken() { |
|||
return BotConfig.COMMANDS_TOKEN; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue