use of com.pengrad.telegrambot.request.GetUpdates in project anton-pavlovich-bot by wyvie.
the class MessageReader method readMessages.
@Scheduled(fixedDelay = 200)
public void readMessages() {
GetUpdates getUpdates = new GetUpdates().limit(telegramProperties.getUpdateLimit()).offset(lastOffset).timeout(0);
GetUpdatesResponse response = telegramBot.execute(getUpdates);
List<Update> updates = response.updates();
updates.forEach(update -> {
lastOffset = update.updateId() + 1;
Message message = update.message();
if (message != null && message.from() != null && bannedUsers.contains(message.from().id())) {
logger.debug("Message ignored from user " + message.from().id());
persistUser(message.from());
return;
}
if (message != null && message.text() != null) {
logger.debug("Got message '" + message.text() + "' from chat_id " + message.chat().id());
persistUser(message.from());
if (validateCommmand(message))
commandProcessor.processCommand(message);
else {
String messageText;
messageText = message.text().trim();
if (messageText.startsWith("+") || messageText.startsWith("-") || emojiHelper.isThumbsUp(messageText) || emojiHelper.isThumbsDown(messageText))
commandProcessor.processKarma(message);
}
lastOffset = update.updateId() + 1;
}
if (message != null && message.sticker() != null) {
String emoji = message.sticker().emoji();
if (emojiHelper.isThumbsDown(emoji) || emojiHelper.isThumbsUp(emoji)) {
commandProcessor.processKarma(message);
}
}
});
}
Aggregations