org.telegram.telegrambots.meta.bots.AbsSender Java Examples

The following examples show how to use org.telegram.telegrambots.meta.bots.AbsSender. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UnsubCommand.java    From telegram-notifications-plugin with MIT License 7 votes vote down vote up
@Override
public void execute(AbsSender absSender, User user, Chat chat, String[] strings) {
    Subscribers subscribers = Subscribers.getInstance();
    String ans;

    Long id = chat.getId();

    boolean isSubscribed = subscribers.isSubscribed(id);

    if (isSubscribed) {
        subscribers.unsubscribe(id);
        ans = botStrings.get("message.unsub.success");
    } else {
        ans = botStrings.get("message.unsub.alreadyunsub");
    }

    SendMessage answer = new SendMessage();
    answer.setChatId(chat.getId().toString());
    answer.setText(ans);

    try {
        absSender.execute(answer);
    } catch (TelegramApiException e) {
        LOGGER.error(LOG_TAG, e);
    }
}
 
Example #2
Source File: HelpCommand.java    From TelegramBotsExample with GNU General Public License v3.0 6 votes vote down vote up
@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 (IBotCommand 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.execute(helpMessage);
    } catch (TelegramApiException e) {
        BotLogger.error(LOGTAG, e);
    }
}
 
Example #3
Source File: StopCommand.java    From TelegramBotsExample with GNU General Public License v3.0 6 votes vote down vote up
@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.execute(answer);
        } catch (TelegramApiException e) {
            BotLogger.error(LOGTAG, e);
        }
    }
}
 
Example #4
Source File: StartCommand.java    From TelegramBotsExample with GNU General Public License v3.0 6 votes vote down vote up
@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.execute(answer);
    } catch (TelegramApiException e) {
        BotLogger.error(LOGTAG, e);
    }
}
 
Example #5
Source File: CommandRegistry.java    From TelegramBots with MIT License 6 votes vote down vote up
/**
 * Executes a command action if the command is registered.
 *
 * @note If the command is not registered and there is a default consumer,
 * that action will be performed
 *
 * @param absSender absSender
 * @param message input message
 * @return True if a command or default action is executed, false otherwise
 */
public final boolean executeCommand(AbsSender absSender, Message message) {
    if (message.hasText()) {
        String text = message.getText();
        if (text.startsWith(BotCommand.COMMAND_INIT_CHARACTER)) {
            String commandMessage = text.substring(1);
            String[] commandSplit = commandMessage.split(BotCommand.COMMAND_PARAMETER_SEPARATOR_REGEXP);

            String command = removeUsernameFromCommandIfNeeded(commandSplit[0]);

            if (commandRegistryMap.containsKey(command)) {
                String[] parameters = Arrays.copyOfRange(commandSplit, 1, commandSplit.length);
                commandRegistryMap.get(command).processMessage(absSender, message, parameters);
                return true;
            } else if (defaultConsumer != null) {
                defaultConsumer.accept(absSender, message);
                return true;
            }
        }
    }
    return false;
}
 
Example #6
Source File: HelpCommand.java    From telegram-notifications-plugin with MIT License 5 votes vote down vote up
@Override
public void execute(AbsSender absSender, User user, Chat chat, String[] strings) {
    SendMessage answer = new SendMessage();
    answer.setChatId(chat.getId().toString());
    answer.setText(botStrings.get("message.help"));

    try {
        absSender.execute(answer);
    } catch (TelegramApiException e) {
        LOGGER.error(LOG_TAG, e);
    }
}
 
Example #7
Source File: HelloCommand.java    From TelegramBotsExample with GNU General Public License v3.0 5 votes vote down vote up
@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) {
        BotLogger.error(LOGTAG, e);
    }
}
 
Example #8
Source File: SubCommand.java    From telegram-notifications-plugin with MIT License 5 votes vote down vote up
@Override
public void execute(AbsSender absSender, User user, Chat chat, String[] strings) {
    Subscribers subscribers = Subscribers.getInstance();
    String ans;

    Long id = chat.getId();
    String name = chat.isUserChat() ? user.toString() : chat.toString();

    boolean isSubscribed = subscribers.isSubscribed(id);

    if (!isSubscribed) {
        subscribers.subscribe(name, id);
        ans = botStrings.get("message.sub.success");
    } else {
        ans = botStrings.get("message.sub.alreadysub");
    }

    SendMessage answer = new SendMessage();
    answer.setChatId(chat.getId().toString());
    answer.setText(ans);

    try {
        absSender.execute(answer);
    } catch (TelegramApiException e) {
        LOGGER.error(LOG_TAG, e);
    }
}
 
Example #9
Source File: StartCommand.java    From telegram-notifications-plugin with MIT License 5 votes vote down vote up
@Override
public void execute(AbsSender absSender, User user, Chat chat, String[] strings) {
    SendMessage answer = new SendMessage();
    answer.setChatId(chat.getId().toString());
    answer.setText(botStrings.get("message.help"));

    try {
        absSender.execute(answer);
    } catch (TelegramApiException e) {
        LOGGER.error(LOG_TAG, e);
    }
}
 
Example #10
Source File: StatusCommand.java    From telegram-notifications-plugin with MIT License 5 votes vote down vote up
@Override
public void execute(AbsSender absSender, User user, Chat chat, String[] strings) {
    Subscribers subscribers = Subscribers.getInstance();
    String toSend;

    Long id = chat.getId();

    boolean isSubscribed = subscribers.isSubscribed(id);

    if (isSubscribed) {
        boolean isApproved = subscribers.isApproved(id);

        if (CONFIG.getApprovalType() == UserApprover.ApprovalType.ALL) {
            toSend = botStrings.get("message.status.approved");
        } else {
            toSend = isApproved
                    ? botStrings.get("message.status.approved")
                    : botStrings.get("message.status.unapproved");
        }
    } else {
        toSend = botStrings.get("message.status.unsubscribed");
    }

    SendMessage answer = new SendMessage();
    answer.setChatId(chat.getId().toString());
    answer.setText(toSend);

    try {
        absSender.execute(answer);
    } catch (TelegramApiException e) {
        LOGGER.error(LOG_TAG, e);
    }
}
 
Example #11
Source File: CommandRegistry.java    From TelegramBots with MIT License 4 votes vote down vote up
@Override
public void registerDefaultAction(BiConsumer<AbsSender, Message> defaultConsumer) {
    this.defaultConsumer = defaultConsumer;
}
 
Example #12
Source File: DefaultBotCommand.java    From TelegramBots with MIT License 4 votes vote down vote up
@Override
public final void execute(AbsSender absSender, User user, Chat chat, String[] arguments) {
}
 
Example #13
Source File: TelegramLongPollingCommandBot.java    From TelegramBots with MIT License 4 votes vote down vote up
@Override
public void registerDefaultAction(BiConsumer<AbsSender, Message> defaultConsumer) {
    commandRegistry.registerDefaultAction(defaultConsumer);
}
 
Example #14
Source File: ICommandRegistry.java    From TelegramBots with MIT License 2 votes vote down vote up
/**
 * Register a default action when there is no command register that match the message sent
 * @param defaultConsumer Consumer to evaluate the message
 *
 * @note Use this method if you want your bot to execute a default action when the user
 * sends a command that is not registered.
 */
void registerDefaultAction(BiConsumer<AbsSender, Message> defaultConsumer);
 
Example #15
Source File: IBotCommand.java    From TelegramBots with MIT License 2 votes vote down vote up
/**
 * Process the message and execute the command
 *
 * @param absSender absSender to send messages over
 * @param message   the message to process
 */
void processMessage(AbsSender absSender, Message message, String[] arguments);
 
Example #16
Source File: DefaultBotCommand.java    From TelegramBots with MIT License 2 votes vote down vote up
/**
 * Process the message and execute the command
 *
 * @param absSender absSender to send messages over
 * @param message   the message to process
 * @param arguments passed arguments
 */
@Override
public void processMessage(AbsSender absSender, Message message, String[] arguments) {
    execute(absSender, message.getFrom(), message.getChat(), message.getMessageId(), arguments);
}
 
Example #17
Source File: DefaultBotCommand.java    From TelegramBots with MIT License 2 votes vote down vote up
/**
 * 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 messageId message id for interaction
 * @param arguments passed arguments
 */
public abstract void execute(AbsSender absSender, User user, Chat chat, Integer messageId, String[] arguments);
 
Example #18
Source File: BotCommand.java    From TelegramBots with MIT License 2 votes vote down vote up
/**
 * Process the message and execute the command
 *
 * @param absSender absSender to send messages over
 * @param message   the message to process
 * @param arguments passed arguments
 */
public void processMessage(AbsSender absSender, Message message, String[] arguments) {
    execute(absSender, message.getFrom(), message.getChat(), arguments);
}
 
Example #19
Source File: BotCommand.java    From TelegramBots with MIT License 2 votes vote down vote up
/**
 * 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, User user, Chat chat, String[] arguments);