use of com.pengrad.telegrambot.response.BaseResponse 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);
}
Aggregations