Support callback query
This commit is contained in:
parent
80d4a65c30
commit
5f64fb90ed
|
@ -6,43 +6,56 @@ import org.apache.shiro.session.UnknownSessionException;
|
||||||
import org.apache.shiro.session.mgt.DefaultSessionManager;
|
import org.apache.shiro.session.mgt.DefaultSessionManager;
|
||||||
import org.apache.shiro.session.mgt.SessionContext;
|
import org.apache.shiro.session.mgt.SessionContext;
|
||||||
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
|
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.api.objects.Update;
|
||||||
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public abstract class TelegramLongPollingSessionBot extends TelegramLongPollingBot {
|
public abstract class TelegramLongPollingSessionBot extends TelegramLongPollingBot {
|
||||||
DefaultSessionManager sessionManager = new DefaultSessionManager();
|
@Setter
|
||||||
|
DefaultSessionManager sessionManager;
|
||||||
|
|
||||||
@Setter
|
@Setter
|
||||||
ChatIdConverter chatIdConverter = new DefaultChatIdConverter();
|
ChatIdConverter chatIdConverter;
|
||||||
|
|
||||||
public TelegramLongPollingSessionBot(){
|
public TelegramLongPollingSessionBot(){
|
||||||
this.setChatIdConverter(new DefaultChatIdConverter());
|
this(new DefaultChatIdConverter());
|
||||||
}
|
}
|
||||||
|
|
||||||
public TelegramLongPollingSessionBot(ChatIdConverter chatIdConverter){
|
public TelegramLongPollingSessionBot(ChatIdConverter chatIdConverter){
|
||||||
|
this.setSessionManager(new DefaultSessionManager());
|
||||||
this.setChatIdConverter(chatIdConverter);
|
this.setChatIdConverter(chatIdConverter);
|
||||||
|
AbstractSessionDAO sessionDAO = (AbstractSessionDAO) sessionManager.getSessionDAO();
|
||||||
|
sessionDAO.setSessionIdGenerator(chatIdConverter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onUpdateReceived(Update update) {
|
public void onUpdateReceived(Update update) {
|
||||||
Optional<Session> chatSession;
|
Optional<Session> chatSession;
|
||||||
|
Message message;
|
||||||
if (update.hasMessage()) {
|
if (update.hasMessage()) {
|
||||||
chatIdConverter.setSessionId(update.getMessage().getChatId());
|
message = update.getMessage();
|
||||||
AbstractSessionDAO sessionDAO = (AbstractSessionDAO) sessionManager.getSessionDAO();
|
} else if (update.hasCallbackQuery()) {
|
||||||
sessionDAO.setSessionIdGenerator(chatIdConverter);
|
message = update.getCallbackQuery().getMessage();
|
||||||
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 {
|
} else {
|
||||||
chatSession = Optional.empty();
|
chatSession = Optional.empty();
|
||||||
|
onUpdateReceived(update, chatSession);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
chatIdConverter.setSessionId(message.getChatId());
|
||||||
|
chatSession = this.getSession(message);
|
||||||
onUpdateReceived(update, chatSession);
|
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);
|
public abstract void onUpdateReceived(Update update, Optional<Session> botSession);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user