You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
2.0 KiB

package org.telegram.commands;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.telegram.database.DatabaseManager;
import org.telegram.telegrambots.extensions.bots.commandbot.commands.BotCommand;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Chat;
import org.telegram.telegrambots.meta.api.objects.User;
import org.telegram.telegrambots.meta.bots.AbsSender;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
/**
* 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 Logger log = LogManager.getLogger(HelloCommand.class);
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.execute(answer);
} catch (TelegramApiException e) {
log.error(e.getLocalizedMessage(), e);
}
}
}