org.telegram.telegrambots.meta.api.objects.Chat Java Examples

The following examples show how to use org.telegram.telegrambots.meta.api.objects.Chat. 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 6 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: TelegramBot.java    From telegram-notifications-plugin with MIT License 5 votes vote down vote up
@Override
public void processNonCommandUpdate(Update update) {
    if (update == null) {
        LOG.log(Level.WARNING, "Update is null");
        return;
    }

    final String nonCommandMessage = CONFIG.getBotStrings()
            .get("message.noncommand");

    final Message message = update.getMessage();
    final Chat chat = message.getChat();

    if (chat.isUserChat()) {
        sendMessage(chat.getId(), nonCommandMessage);
        return;
    }

    final String text = message.getText();

    try {
        // Skip not direct messages in chats
        if (text.length() < 1 || text.charAt(0) != '@') return;
        final String[] tmp = text.split(" ");
        if (tmp.length < 2 || !CONFIG.getBotName().equals(tmp[0].substring(1, tmp[0].length()))) return;
    } catch (Exception e) {
        LOG.log(Level.SEVERE, "Something bad happened while message processing", e);
        return;
    }

    sendMessage(chat.getId(), nonCommandMessage);
}
 
Example #10
Source File: GetChat.java    From TelegramBots with MIT License 5 votes vote down vote up
@Override
public Chat deserializeResponse(String answer) throws TelegramApiRequestException {
    try {
        ApiResponse<Chat> result = OBJECT_MAPPER.readValue(answer,
                new TypeReference<ApiResponse<Chat>>(){});
        if (result.getOk()) {
            return result.getResult();
        } else {
            throw new TelegramApiRequestException("Error getting chat", result);
        }
    } catch (IOException e) {
        throw new TelegramApiRequestException("Unable to deserialize response", e);
    }
}
 
Example #11
Source File: TestDeserialization.java    From TelegramBots with MIT License 5 votes vote down vote up
private void assertPrivateChat(Chat chat) {
    assertNotNull(chat);
    assertEquals(Long.valueOf(1111111), chat.getId());
    assertTrue(chat.isUserChat());
    assertEquals("Test Lastname", chat.getLastName());
    assertEquals("Test Firstname", chat.getFirstName());
    assertEquals("Testusername", chat.getUserName());
}
 
Example #12
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 #13
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 #14
Source File: TestDeserialization.java    From TelegramBots with MIT License 4 votes vote down vote up
private void assertChannelChat(Chat chat) {
    assertNotNull(chat);
    assertEquals(Long.valueOf(-10000000000L), chat.getId());
    assertTrue(chat.isChannelChat());
    assertEquals("Test channel", chat.getTitle());
}
 
Example #15
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 #16
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);