package org.telegram.telegrambots.session;

import lombok.Setter;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.session.mgt.SessionContext;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;

import java.util.Optional;

public abstract class TelegramLongPollingSessionBot extends TelegramLongPollingBot {
    DefaultSessionManager sessionManager = new DefaultSessionManager();

    @Setter
    ChatIdConverter chatIdConverter = new DefaultChatIdConverter();

    public TelegramLongPollingSessionBot(){
        this.setChatIdConverter(new DefaultChatIdConverter());
    }

    public TelegramLongPollingSessionBot(ChatIdConverter chatIdConverter){
        this.setChatIdConverter(chatIdConverter);
    }

    @Override
    public void onUpdateReceived(Update update) {
        Optional<Session> chatSession;
        if (update.hasMessage()) {
            chatIdConverter.setSessionId(update.getMessage().getChatId());
            AbstractSessionDAO sessionDAO = (AbstractSessionDAO) sessionManager.getSessionDAO();
            sessionDAO.setSessionIdGenerator(chatIdConverter);
            try {
                chatSession = Optional.of(sessionManager.getSession(chatIdConverter));
            } catch (UnknownSessionException e) {
                SessionContext botSession = new DefaultChatSessionContext(update.getMessage().getChatId(), update.getMessage().getFrom().getUserName());
                chatSession = Optional.of(sessionManager.start(botSession));
            }
        } else {
            chatSession = Optional.empty();
        }
        onUpdateReceived(update, chatSession);
    }

    public abstract void onUpdateReceived(Update update, Optional<Session> botSession);
}

package org.telegram.telegrambots.session;

import lombok.Setter;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.session.mgt.SessionContext;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
import org.telegram.telegrambots.api.objects.Message;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;

import java.util.Optional;

public abstract class TelegramLongPollingSessionBot extends TelegramLongPollingBot {
    @Setter
    DefaultSessionManager sessionManager;

    @Setter
    ChatIdConverter chatIdConverter;

    public TelegramLongPollingSessionBot(){
        this(new DefaultChatIdConverter());
    }

    public TelegramLongPollingSessionBot(ChatIdConverter chatIdConverter){
        this.setSessionManager(new DefaultSessionManager());
        this.setChatIdConverter(chatIdConverter);
        AbstractSessionDAO sessionDAO = (AbstractSessionDAO) sessionManager.getSessionDAO();
        sessionDAO.setSessionIdGenerator(chatIdConverter);
    }

    @Override
    public void onUpdateReceived(Update update) {
        Optional<Session> chatSession;
        Message message;
        if (update.hasMessage()) {
            message = update.getMessage();
        } else if (update.hasCallbackQuery()) {
            message = update.getCallbackQuery().getMessage();
        } else {
            chatSession = Optional.empty();
            onUpdateReceived(update, chatSession);
            return;
        }
        chatIdConverter.setSessionId(message.getChatId());
        chatSession = this.getSession(message);
        onUpdateReceived(update, chatSession);
    }

    public Optional<Session> getSession(Message message){
        try {
            return Optional.of(sessionManager.getSession(chatIdConverter));
        } catch (UnknownSessionException e) {
            SessionContext botSession = new DefaultChatSessionContext(message.getChatId(), message.getFrom().getUserName());
            return Optional.of(sessionManager.start(botSession));
        }
    }

    public abstract void onUpdateReceived(Update update, Optional<Session> botSession);
}

Support callback query