#3 change sessions impl
This commit is contained in:
@@ -0,0 +1,20 @@
|
|||||||
|
package ru.penkrat.stbf.common.command;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import ru.penkrat.stbf.api.BotRequest;
|
||||||
|
import ru.penkrat.stbf.api.BotResponse;
|
||||||
|
import ru.penkrat.stbf.api.CommandChain;
|
||||||
|
import ru.penkrat.stbf.api.SessionProvider;
|
||||||
|
import ru.penkrat.stbf.common.session.SessionAwareBotRequest;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class SessionAwareCommandChain implements CommandChain {
|
||||||
|
|
||||||
|
private final CommandChain delegate;
|
||||||
|
private final SessionProvider sessionProvider;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void processCommand(BotRequest botRequest, BotResponse botResponse) {
|
||||||
|
delegate.processCommand(new SessionAwareBotRequest(botRequest, sessionProvider), botResponse);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package ru.penkrat.stbf.common.command;
|
||||||
|
|
||||||
|
import ru.penkrat.stbf.api.BotRequest;
|
||||||
|
import ru.penkrat.stbf.api.BotResponse;
|
||||||
|
import ru.penkrat.stbf.api.CommandChain;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
public class ThreadPolledCommandChain implements CommandChain {
|
||||||
|
|
||||||
|
private final CommandChain delegate;
|
||||||
|
private final ExecutorService executor;
|
||||||
|
|
||||||
|
public ThreadPolledCommandChain(CommandChain delegate) {
|
||||||
|
this(delegate, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ThreadPolledCommandChain(CommandChain delegate, int nThreads) {
|
||||||
|
this.delegate = delegate;
|
||||||
|
this.executor = Executors.newFixedThreadPool(nThreads);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void processCommand(BotRequest botRequest, BotResponse botResponse) {
|
||||||
|
executor.execute(() -> {
|
||||||
|
delegate.processCommand(botRequest, botResponse);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package ru.penkrat.stbf.common.session;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import ru.penkrat.stbf.api.BotRequest;
|
||||||
|
import ru.penkrat.stbf.api.BotSession;
|
||||||
|
import ru.penkrat.stbf.api.SessionProvider;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class SessionAwareBotRequest implements BotRequest {
|
||||||
|
|
||||||
|
private final BotRequest delegate;
|
||||||
|
|
||||||
|
private final SessionProvider sessionProvider;
|
||||||
|
|
||||||
|
private BotSession botSession;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BotSession getSession() {
|
||||||
|
if (botSession == null) {
|
||||||
|
botSession = sessionProvider.get(getChatId());
|
||||||
|
Objects.requireNonNull(botSession, "Session can not be null");
|
||||||
|
}
|
||||||
|
return botSession;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<String> getMessageText() {
|
||||||
|
return this.delegate.getMessageText();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<String> getPhoneNumber() {
|
||||||
|
return this.delegate.getPhoneNumber();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<String> getCallbackData() {
|
||||||
|
return this.delegate.getCallbackData();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<String> getCallbackMessageText() {
|
||||||
|
return this.delegate.getCallbackMessageText();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getChatId() {
|
||||||
|
return this.delegate.getChatId();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -5,6 +5,8 @@ import org.slf4j.LoggerFactory;
|
|||||||
import picocli.CommandLine;
|
import picocli.CommandLine;
|
||||||
import ru.penkrat.stbf.api.BotCommandChain;
|
import ru.penkrat.stbf.api.BotCommandChain;
|
||||||
import ru.penkrat.stbf.api.CommandChain;
|
import ru.penkrat.stbf.api.CommandChain;
|
||||||
|
import ru.penkrat.stbf.common.command.SessionAwareCommandChain;
|
||||||
|
import ru.penkrat.stbf.common.command.ThreadPolledCommandChain;
|
||||||
import ru.penkrat.stbf.common.session.InMemBotSessionProvider;
|
import ru.penkrat.stbf.common.session.InMemBotSessionProvider;
|
||||||
import ru.penkrat.stbf.impl.pengrad.PengradTelegramBot;
|
import ru.penkrat.stbf.impl.pengrad.PengradTelegramBot;
|
||||||
import ru.penkrat.stbf.templates.xml.XmlFlowResolver;
|
import ru.penkrat.stbf.templates.xml.XmlFlowResolver;
|
||||||
@@ -12,7 +14,7 @@ import ru.penkrat.stbf.templates.xml.XmlFlowResolver;
|
|||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
@CommandLine.Command(name = "java -jar stbf-demo.jar", mixinStandardHelpOptions = true, description = "Run bot",
|
@CommandLine.Command(name = "java -jar stbf-demo.jar", mixinStandardHelpOptions = true, description = "Run bot",
|
||||||
version = "Simple Telegram bot framework Demo app v. 0.0.1")
|
version = "Simple Telegram bot framework Demo app v. 0.0.2")
|
||||||
public class App implements Runnable {
|
public class App implements Runnable {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(App.class);
|
private static final Logger log = LoggerFactory.getLogger(App.class);
|
||||||
@@ -29,17 +31,17 @@ public class App implements Runnable {
|
|||||||
new CommandLine(new App()).execute(args);
|
new CommandLine(new App()).execute(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
private BotCommandChain getCommandChain(String filename) {
|
private CommandChain getCommandChain(String filename) {
|
||||||
XmlFlowResolver flow = new XmlFlowResolver(filename);
|
XmlFlowResolver flow = new XmlFlowResolver(filename);
|
||||||
|
|
||||||
BotCommandChain chain = new BotCommandChain();
|
BotCommandChain chain = new BotCommandChain();
|
||||||
flow.getCommands().forEach(chain::add);
|
flow.getCommands().forEach(chain::add);
|
||||||
|
|
||||||
return chain;
|
return new ThreadPolledCommandChain(new SessionAwareCommandChain(chain, new InMemBotSessionProvider()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Runnable start(String token, CommandChain chain) {
|
private Runnable start(String token, CommandChain chain) {
|
||||||
return () -> botAtomicReference.set(new PengradTelegramBot(token, chain, new InMemBotSessionProvider()));
|
return () -> botAtomicReference.set(new PengradTelegramBot(token, chain));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onShutdown() {
|
private void onShutdown() {
|
||||||
|
|||||||
@@ -19,9 +19,6 @@ public class BotRequestImpl implements BotRequest {
|
|||||||
@Getter(PROTECTED)
|
@Getter(PROTECTED)
|
||||||
private final Update update;
|
private final Update update;
|
||||||
|
|
||||||
@Getter
|
|
||||||
private final BotSession session;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<String> getMessageText() {
|
public Optional<String> getMessageText() {
|
||||||
if (update.message() != null) {
|
if (update.message() != null) {
|
||||||
@@ -61,4 +58,9 @@ public class BotRequestImpl implements BotRequest {
|
|||||||
.orElseGet(() -> update.message()).chat().id();
|
.orElseGet(() -> update.message()).chat().id();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BotSession getSession() {
|
||||||
|
throw new UnsupportedOperationException("Session is not supported");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package ru.penkrat.stbf.impl.pengrad;
|
package ru.penkrat.stbf.impl.pengrad;
|
||||||
|
|
||||||
import com.google.gson.internal.reflect.ReflectionAccessor;
|
import com.google.gson.internal.reflect.ReflectionHelper;
|
||||||
import com.pengrad.telegrambot.model.request.InlineKeyboardButton;
|
import com.pengrad.telegrambot.model.request.InlineKeyboardButton;
|
||||||
import com.pengrad.telegrambot.model.request.KeyboardButton;
|
import com.pengrad.telegrambot.model.request.KeyboardButton;
|
||||||
import lombok.Value;
|
import lombok.Value;
|
||||||
@@ -58,7 +58,7 @@ class KeyboardImpl implements Keyboard {
|
|||||||
return text.computeIfAbsent(btn, o -> {
|
return text.computeIfAbsent(btn, o -> {
|
||||||
try {
|
try {
|
||||||
Field text = btn.getClass().getDeclaredField("text");
|
Field text = btn.getClass().getDeclaredField("text");
|
||||||
ReflectionAccessor.getInstance().makeAccessible(text);
|
ReflectionHelper.makeAccessible(text);
|
||||||
return String.valueOf(text.get(btn));
|
return String.valueOf(text.get(btn));
|
||||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||||
return "*error*";
|
return "*error*";
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import com.pengrad.telegrambot.model.CallbackQuery;
|
|||||||
import com.pengrad.telegrambot.model.Update;
|
import com.pengrad.telegrambot.model.Update;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import ru.penkrat.stbf.api.CommandChain;
|
import ru.penkrat.stbf.api.CommandChain;
|
||||||
import ru.penkrat.stbf.api.SessionProvider;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@@ -14,12 +13,6 @@ import java.util.Optional;
|
|||||||
public class PengradTelegramBot extends TelegramBot implements AutoCloseable {
|
public class PengradTelegramBot extends TelegramBot implements AutoCloseable {
|
||||||
|
|
||||||
public PengradTelegramBot(String botToken, CommandChain commandChain) {
|
public PengradTelegramBot(String botToken, CommandChain commandChain) {
|
||||||
this(botToken, commandChain, (id) -> {
|
|
||||||
throw new IllegalArgumentException("'SessionProvider' is not defined");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public PengradTelegramBot(String botToken, CommandChain commandChain, SessionProvider sessionProvider) {
|
|
||||||
super(botToken);
|
super(botToken);
|
||||||
this.setUpdatesListener(updates -> {
|
this.setUpdatesListener(updates -> {
|
||||||
for (Update update : updates) {
|
for (Update update : updates) {
|
||||||
@@ -32,7 +25,7 @@ public class PengradTelegramBot extends TelegramBot implements AutoCloseable {
|
|||||||
log.debug("New message in chat {}", chatId);
|
log.debug("New message in chat {}", chatId);
|
||||||
|
|
||||||
commandChain.processCommand(
|
commandChain.processCommand(
|
||||||
new BotRequestImpl(update, sessionProvider.get(chatId)),
|
new BotRequestImpl(update),
|
||||||
new BotResponseImpl(update, this));
|
new BotResponseImpl(update, this));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Bot Error:", e);
|
log.error("Bot Error:", e);
|
||||||
|
|||||||
@@ -19,9 +19,6 @@ class BotRequestImpl implements BotRequest {
|
|||||||
@Getter(PROTECTED)
|
@Getter(PROTECTED)
|
||||||
private final Update update;
|
private final Update update;
|
||||||
|
|
||||||
@Getter
|
|
||||||
private final BotSession session;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<String> getMessageText() {
|
public Optional<String> getMessageText() {
|
||||||
if (update.hasMessage()) {
|
if (update.hasMessage()) {
|
||||||
@@ -58,4 +55,9 @@ class BotRequestImpl implements BotRequest {
|
|||||||
return Utils.getChatId(update);
|
return Utils.getChatId(update);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BotSession getSession() {
|
||||||
|
throw new UnsupportedOperationException("Session is not supported");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import org.telegram.telegrambots.meta.api.objects.Update;
|
|||||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||||
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
|
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
|
||||||
import ru.penkrat.stbf.api.BotCommandChain;
|
import ru.penkrat.stbf.api.BotCommandChain;
|
||||||
import ru.penkrat.stbf.api.SessionProvider;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class RubenlagusTelegramBot extends TelegramLongPollingBot {
|
public class RubenlagusTelegramBot extends TelegramLongPollingBot {
|
||||||
@@ -15,19 +14,11 @@ public class RubenlagusTelegramBot extends TelegramLongPollingBot {
|
|||||||
private final String botUsername;
|
private final String botUsername;
|
||||||
private final String botToken;
|
private final String botToken;
|
||||||
private final BotCommandChain commandChain;
|
private final BotCommandChain commandChain;
|
||||||
private final SessionProvider sessionProvider;
|
|
||||||
|
|
||||||
public RubenlagusTelegramBot(String botUsername, String botToken, BotCommandChain botCommandChain) {
|
public RubenlagusTelegramBot(String botUsername, String botToken, BotCommandChain botCommandChain) {
|
||||||
this(botUsername, botToken, botCommandChain, (id) -> {
|
|
||||||
throw new IllegalArgumentException("'SessionProvider' is not defined");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public RubenlagusTelegramBot(String botUsername, String botToken, BotCommandChain botCommandChain, SessionProvider sessionProvider) {
|
|
||||||
this.botUsername = botUsername;
|
this.botUsername = botUsername;
|
||||||
this.botToken = botToken;
|
this.botToken = botToken;
|
||||||
this.commandChain = botCommandChain;
|
this.commandChain = botCommandChain;
|
||||||
this.sessionProvider = sessionProvider;
|
|
||||||
|
|
||||||
TelegramBotsApi telegramBotsApi;
|
TelegramBotsApi telegramBotsApi;
|
||||||
try {
|
try {
|
||||||
@@ -42,7 +33,7 @@ public class RubenlagusTelegramBot extends TelegramLongPollingBot {
|
|||||||
public void onUpdateReceived(Update update) {
|
public void onUpdateReceived(Update update) {
|
||||||
try {
|
try {
|
||||||
commandChain.processCommand(
|
commandChain.processCommand(
|
||||||
new BotRequestImpl(update, sessionProvider.get(Utils.getChatId(update))),
|
new BotRequestImpl(update),
|
||||||
new BotResponseImpl(update, this));
|
new BotResponseImpl(update, this));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Bot Error:", e);
|
log.error("Bot Error:", e);
|
||||||
|
|||||||
Reference in New Issue
Block a user