2018-06-07 19:43:41 +02:00
|
|
|
package org.telegram.telegrambots.session;
|
|
|
|
|
|
|
|
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;
|
2020-08-01 13:49:46 +02:00
|
|
|
import org.telegram.telegrambots.bots.DefaultBotOptions;
|
2018-07-27 00:48:40 +02:00
|
|
|
import org.telegram.telegrambots.meta.api.objects.Message;
|
|
|
|
import org.telegram.telegrambots.meta.api.objects.Update;
|
2018-06-07 19:43:41 +02:00
|
|
|
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
|
|
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
2018-07-27 00:48:40 +02:00
|
|
|
@SuppressWarnings({"WeakerAccess", "OptionalUsedAsFieldOrParameterType", "unused"})
|
2018-06-07 19:43:41 +02:00
|
|
|
public abstract class TelegramLongPollingSessionBot extends TelegramLongPollingBot {
|
2018-06-08 22:12:34 +02:00
|
|
|
DefaultSessionManager sessionManager;
|
2018-06-07 19:43:41 +02:00
|
|
|
|
2018-06-08 22:12:34 +02:00
|
|
|
ChatIdConverter chatIdConverter;
|
2018-06-07 19:43:41 +02:00
|
|
|
|
|
|
|
public TelegramLongPollingSessionBot(){
|
2018-06-08 22:12:34 +02:00
|
|
|
this(new DefaultChatIdConverter());
|
2018-06-07 19:43:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public TelegramLongPollingSessionBot(ChatIdConverter chatIdConverter){
|
2020-11-03 03:57:36 +01:00
|
|
|
this(chatIdConverter, new DefaultBotOptions());
|
2020-08-01 13:49:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public TelegramLongPollingSessionBot(ChatIdConverter chatIdConverter, DefaultBotOptions defaultBotOptions){
|
2018-06-08 22:12:34 +02:00
|
|
|
this.setSessionManager(new DefaultSessionManager());
|
2018-06-07 19:43:41 +02:00
|
|
|
this.setChatIdConverter(chatIdConverter);
|
2018-06-08 22:12:34 +02:00
|
|
|
AbstractSessionDAO sessionDAO = (AbstractSessionDAO) sessionManager.getSessionDAO();
|
|
|
|
sessionDAO.setSessionIdGenerator(chatIdConverter);
|
2018-06-07 19:43:41 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 00:48:40 +02:00
|
|
|
public void setSessionManager(DefaultSessionManager sessionManager) {
|
|
|
|
this.sessionManager = sessionManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setChatIdConverter(ChatIdConverter chatIdConverter) {
|
|
|
|
this.chatIdConverter = chatIdConverter;
|
|
|
|
}
|
|
|
|
|
2018-06-07 19:43:41 +02:00
|
|
|
@Override
|
|
|
|
public void onUpdateReceived(Update update) {
|
|
|
|
Optional<Session> chatSession;
|
2018-06-08 22:12:34 +02:00
|
|
|
Message message;
|
2018-06-07 19:43:41 +02:00
|
|
|
if (update.hasMessage()) {
|
2018-06-08 22:12:34 +02:00
|
|
|
message = update.getMessage();
|
|
|
|
} else if (update.hasCallbackQuery()) {
|
|
|
|
message = update.getCallbackQuery().getMessage();
|
2018-06-07 19:43:41 +02:00
|
|
|
} else {
|
|
|
|
chatSession = Optional.empty();
|
2018-06-08 22:12:34 +02:00
|
|
|
onUpdateReceived(update, chatSession);
|
|
|
|
return;
|
2018-06-07 19:43:41 +02:00
|
|
|
}
|
2018-06-08 22:12:34 +02:00
|
|
|
chatIdConverter.setSessionId(message.getChatId());
|
|
|
|
chatSession = this.getSession(message);
|
2018-06-07 19:43:41 +02:00
|
|
|
onUpdateReceived(update, chatSession);
|
|
|
|
}
|
|
|
|
|
2018-06-08 22:12:34 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-07 19:43:41 +02:00
|
|
|
public abstract void onUpdateReceived(Update update, Optional<Session> botSession);
|
|
|
|
}
|