#3 change sessions impl
This commit is contained in:
@@ -5,6 +5,8 @@ import org.slf4j.LoggerFactory;
|
||||
import picocli.CommandLine;
|
||||
import ru.penkrat.stbf.api.BotCommandChain;
|
||||
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.impl.pengrad.PengradTelegramBot;
|
||||
import ru.penkrat.stbf.templates.xml.XmlFlowResolver;
|
||||
@@ -12,58 +14,58 @@ import ru.penkrat.stbf.templates.xml.XmlFlowResolver;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
@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 {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(App.class);
|
||||
private static final Logger log = LoggerFactory.getLogger(App.class);
|
||||
|
||||
private static AtomicReference<PengradTelegramBot> botAtomicReference = new AtomicReference<>();
|
||||
private static AtomicReference<PengradTelegramBot> botAtomicReference = new AtomicReference<>();
|
||||
|
||||
@CommandLine.Option(names = {"-f", "--file"}, description = "The file with config.")
|
||||
private String flowFile = "classpath:/flow.xml";
|
||||
@CommandLine.Option(names = {"-f", "--file"}, description = "The file with config.")
|
||||
private String flowFile = "classpath:/flow.xml";
|
||||
|
||||
@CommandLine.Option(names = {"-t", "--token"}, required = true, description = "bot token 1234:abscdf...")
|
||||
private String botToken = "";
|
||||
@CommandLine.Option(names = {"-t", "--token"}, required = true, description = "bot token 1234:abscdf...")
|
||||
private String botToken = "";
|
||||
|
||||
public static void main(String[] args) {
|
||||
new CommandLine(new App()).execute(args);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
new CommandLine(new App()).execute(args);
|
||||
}
|
||||
|
||||
private BotCommandChain getCommandChain(String filename) {
|
||||
XmlFlowResolver flow = new XmlFlowResolver(filename);
|
||||
private CommandChain getCommandChain(String filename) {
|
||||
XmlFlowResolver flow = new XmlFlowResolver(filename);
|
||||
|
||||
BotCommandChain chain = new BotCommandChain();
|
||||
flow.getCommands().forEach(chain::add);
|
||||
BotCommandChain chain = new BotCommandChain();
|
||||
flow.getCommands().forEach(chain::add);
|
||||
|
||||
return chain;
|
||||
}
|
||||
return new ThreadPolledCommandChain(new SessionAwareCommandChain(chain, new InMemBotSessionProvider()));
|
||||
}
|
||||
|
||||
private Runnable start(String token, CommandChain chain) {
|
||||
return () -> botAtomicReference.set(new PengradTelegramBot(token, chain, new InMemBotSessionProvider()));
|
||||
}
|
||||
private Runnable start(String token, CommandChain chain) {
|
||||
return () -> botAtomicReference.set(new PengradTelegramBot(token, chain));
|
||||
}
|
||||
|
||||
private void onShutdown() {
|
||||
try {
|
||||
if (botAtomicReference.get() != null) {
|
||||
botAtomicReference.get().close();
|
||||
log.info("Bot finished.");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Error:", e);
|
||||
}
|
||||
}
|
||||
private void onShutdown() {
|
||||
try {
|
||||
if (botAtomicReference.get() != null) {
|
||||
botAtomicReference.get().close();
|
||||
log.info("Bot finished.");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Error:", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Thread botThread = new Thread(start(botToken, getCommandChain(flowFile)));
|
||||
botThread.setDaemon(false);
|
||||
botThread.setName("stbf-bot-thread");
|
||||
@Override
|
||||
public void run() {
|
||||
Thread botThread = new Thread(start(botToken, getCommandChain(flowFile)));
|
||||
botThread.setDaemon(false);
|
||||
botThread.setName("stbf-bot-thread");
|
||||
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(this::onShutdown));
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(this::onShutdown));
|
||||
|
||||
log.info("Starting bot...");
|
||||
botThread.start();
|
||||
log.info("Bot started.");
|
||||
log.info("Press Ctrl+C to exit.");
|
||||
}
|
||||
log.info("Starting bot...");
|
||||
botThread.start();
|
||||
log.info("Bot started.");
|
||||
log.info("Press Ctrl+C to exit.");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user