Search in sources :

Example 1 with PinChatMessage

use of com.pengrad.telegrambot.request.PinChatMessage in project anton-pavlovich-bot by wyvie.

the class RemindCommand method handle.

@Override
public void handle(Message message, String args) {
    logger.debug("chat: " + message.chat().toString() + " from user");
    Chat chat = message.chat();
    if (!userCanPinEdit(botUser, chat)) {
        sendMessage(chat, MAKE_BOT_ADMIN, ParseMode.HTML);
        return;
    }
    User user = message.from();
    List<String> tokenizedArgs = parseCommandArgs(args);
    logger.debug("*** Tokenized arguments are: " + tokenizedArgs);
    if (tokenizedArgs.isEmpty()) {
        this.sendMessage(chat, USAGE, null);
        return;
    }
    // list all notifications for this user in this chat
    if (tokenizedArgs.get(0).equalsIgnoreCase("list")) {
        sendMessage(user, getRemindersList(user, chat), ParseMode.HTML);
        return;
    }
    // cancel task by id or cancel all tasks
    if (tokenizedArgs.get(0).equalsIgnoreCase("cancel")) {
        String id = tokenizedArgs.size() > 1 ? tokenizedArgs.get(1) : null;
        cancelReminder(user, chat, id);
        sendMessage(user, getRemindersList(user, chat), ParseMode.HTML);
        return;
    }
    // continue with setting up reminder
    long when;
    try {
        when = parseDateTimeToEpoch(tokenizedArgs.get(0));
    } catch (DateTimeParseException e) {
        sendMessage(chat, DATE_FORMAT_ERROR, ParseMode.HTML);
        return;
    }
    String reminderText = tokenizedArgs.size() < 2 ? DEFAULT_REMINDER_TEXT : tokenizedArgs.stream().skip(1).collect(Collectors.joining(" "));
    ScheduledFuture scheduledReminder = scheduleReminder(() -> {
        long messageChatId = chat.id();
        SendResponse response = sendMessage(chat, reminderText, null);
        logger.debug("<<< Send Response: " + response.toString());
        int messageToPinId = response.message().messageId();
        PinChatMessage pinChatMessage = new PinChatMessage(messageChatId, messageToPinId);
        BaseResponse pin = telegramBot.execute(pinChatMessage);
        logger.debug("<<< Pin Response: " + pin.toString());
    }, when);
    // keep scheduled task reference for list or cancel
    Reminder reminder = new Reminder(user, chat, tokenizedArgs.get(0), reminderText, scheduledReminder);
    String key = sha256(args + scheduledReminder.toString());
    scheduledNotifications.put(key, reminder);
    sendMessage(chat, "Reminder created.", null);
    logger.debug("*** Reminders list size: " + scheduledNotifications.size() + ", last one id: " + key + " set by user: " + user);
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) BaseResponse(com.pengrad.telegrambot.response.BaseResponse) User(com.pengrad.telegrambot.model.User) PinChatMessage(com.pengrad.telegrambot.request.PinChatMessage) SendResponse(com.pengrad.telegrambot.response.SendResponse) Chat(com.pengrad.telegrambot.model.Chat) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Aggregations

Chat (com.pengrad.telegrambot.model.Chat)1 User (com.pengrad.telegrambot.model.User)1 PinChatMessage (com.pengrad.telegrambot.request.PinChatMessage)1 BaseResponse (com.pengrad.telegrambot.response.BaseResponse)1 SendResponse (com.pengrad.telegrambot.response.SendResponse)1 DateTimeParseException (java.time.format.DateTimeParseException)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1