Search in sources :

Example 1 with GetUpdates

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);
            }
        }
    });
}
Also used : GetUpdates(com.pengrad.telegrambot.request.GetUpdates) Message(com.pengrad.telegrambot.model.Message) GetUpdatesResponse(com.pengrad.telegrambot.response.GetUpdatesResponse) Update(com.pengrad.telegrambot.model.Update) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Aggregations

Message (com.pengrad.telegrambot.model.Message)1 Update (com.pengrad.telegrambot.model.Update)1 GetUpdates (com.pengrad.telegrambot.request.GetUpdates)1 GetUpdatesResponse (com.pengrad.telegrambot.response.GetUpdatesResponse)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1