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);
}

@@ -328,16 +328,70 @@
 ionDAO;%0A
+import org.telegram.telegrambots.api.objects.Message;%0A
 import o
@@ -611,16 +611,28 @@
 ngBot %7B%0A
+    @Setter%0A
     Defa
@@ -667,38 +667,8 @@
 ager
- = new DefaultSessionManager()
 ;%0A%0A 
@@ -717,39 +717,8 @@
 rter
- = new DefaultChatIdConverter()
 ;%0A%0A 
@@ -772,35 +772,16 @@
     this
-.setChatIdConverter
 (new Def
@@ -902,30 +902,243 @@
 this.set
-ChatIdConverte
+SessionManager(new DefaultSessionManager());%0A        this.setChatIdConverter(chatIdConverter);%0A        AbstractSessionDAO sessionDAO = (AbstractSessionDAO) sessionManager.getSessionDAO();%0A        sessionDAO.setSessionIdGenerato
 r(chatId
@@ -1259,16 +1259,41 @@
 ession;%0A
+        Message message;%0A
         
@@ -1335,152 +1335,316 @@
     
-chatIdConverter.setSessionId(update.getMessage().getChatId());%0A            AbstractSessionDAO sessionDAO = (AbstractSessionDAO) sessionManager.g
+message = update.getMessage();%0A        %7D else if (update.hasCallbackQuery()) %7B%0A            message = update.getCallbackQuery().getMessage();%0A        %7D else %7B%0A            chatSession = Optional.empty();%0A            onUpdateReceived(update, chatSession);%0A            return;%0A        %7D%0A        chatIdConverter.s
 etSe
@@ -1640,36 +1640,54 @@
 erter.setSession
-DAO(
+Id(message.getChatId()
 );%0A            s
@@ -1685,79 +1685,158 @@
     
-    s
+chatS
 ession
-DAO.setSessionIdGenerator(chatIdConverter
+ = this.getSession(message);%0A        onUpdateReceived(update, chatSession
 );%0A    
-  
+%7D%0A%0A
     
-  try 
+public Optional%3CSession%3E getSession(Message message)
 %7B%0A  
@@ -1837,32 +1837,38 @@
 e)%7B%0A        
+try %7B%0A
         
 chatSession 
@@ -1851,37 +1851,34 @@
 y %7B%0A        
-chatSession =
+    return
  Optional.of
@@ -1931,20 +1931,16 @@
         
-    
 %7D catch 
@@ -1981,20 +1981,16 @@
         
-    
 SessionC
@@ -2039,35 +2039,23 @@
 Context(
-update.getM
+m
 essage
-()
 .getChat
@@ -2060,35 +2060,23 @@
 atId(), 
-update.getM
+m
 essage
-()
 .getFrom
@@ -2106,33 +2106,22 @@
         
-    chatSession =
+return
  Optiona
@@ -2172,131 +2172,9 @@
     
-    %7D%0A        %7D else %7B%0A            chatSession = Optional.empty();%0A        %7D%0A        onUpdateReceived(update, chatSession);
+%7D
 %0A   

Support callback query