Java Code Examples for org.telegram.telegrambots.meta.bots.AbsSender#execute()

The following examples show how to use org.telegram.telegrambots.meta.bots.AbsSender#execute() . 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: 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 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: 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 5
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 6
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 7
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 8
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 9
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);
    }
}