diff --git a/pom.xml b/pom.xml index a95a39c..1f1df0b 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,7 @@ stbf-common stbf-rubenlagus stbf-templates + stbf-demo diff --git a/stbf-demo/pom.xml b/stbf-demo/pom.xml new file mode 100644 index 0000000..98b7513 --- /dev/null +++ b/stbf-demo/pom.xml @@ -0,0 +1,127 @@ + + + 4.0.0 + + stbf-parent + ru.penkrat.stbf + 0.0.1-SNAPSHOT + + + ru.penkrat.stbf + stbf-demo + stbf-demo + + + UTF-8 + 1.8 + 1.8 + 3.0.2 + 3.8.1 + 4.12 + 3.9.0 + 2.13.0 + 1.14 + + + + + stbf-templates + ru.penkrat.stbf + ${project.version} + + + stbf-pengrad + ru.penkrat.stbf + ${project.version} + + + info.picocli + picocli + 4.6.1 + + + com.samskivert + jmustache + ${jmustache.version} + + + ch.qos.logback + logback-core + 1.2.5 + + + ch.qos.logback + logback-classic + 1.2.5 + + + junit + junit + ${junit.version} + test + + + org.assertj + assertj-core + ${assertj.version} + test + + + org.mockito + mockito-core + ${mockito.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${maven-compiler-plugin-version} + + + + info.picocli + picocli-codegen + 4.6.1 + + + + -Aproject=${project.groupId}/${project.artifactId} + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + ru.penkrat.stbf.demo.App + + + + + junit:junit + + + + + + + + + diff --git a/stbf-demo/src/main/java/ru/penkrat/stbf/demo/App.java b/stbf-demo/src/main/java/ru/penkrat/stbf/demo/App.java new file mode 100644 index 0000000..410b519 --- /dev/null +++ b/stbf-demo/src/main/java/ru/penkrat/stbf/demo/App.java @@ -0,0 +1,68 @@ +package ru.penkrat.stbf.demo; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import picocli.CommandLine; +import ru.penkrat.stbf.api.BotCommandChain; +import ru.penkrat.stbf.api.CommandChain; +import ru.penkrat.stbf.impl.pengrad.PengradTelegramBot; +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") +public class App implements Runnable { + + private static final Logger log = LoggerFactory.getLogger(App.class); + + private static AtomicReference botAtomicReference = new AtomicReference<>(); + + @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 = ""; + + public static void main(String[] args) { + new CommandLine(new App()).execute(args); + } + + private BotCommandChain getCommandChain(String filename) { + XmlFlowResolver flow = new XmlFlowResolver(filename); + + BotCommandChain chain = new BotCommandChain(); + flow.getCommands().forEach(chain::add); + + return chain; + } + + 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); + } + } + + @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)); + + log.info("Starting bot..."); + botThread.start(); + log.info("Bot started."); + log.info("Press Ctrl+C to exit."); + } +} diff --git a/stbf-demo/src/main/resources/flow.xml b/stbf-demo/src/main/resources/flow.xml new file mode 100644 index 0000000..fa3f643 --- /dev/null +++ b/stbf-demo/src/main/resources/flow.xml @@ -0,0 +1,28 @@ + + + Start + Help + + + + This is demo bot + + + + + + + + This is demo help + + + + + + + + + + + + \ No newline at end of file diff --git a/stbf-demo/src/main/resources/logback.xml b/stbf-demo/src/main/resources/logback.xml new file mode 100644 index 0000000..6eea430 --- /dev/null +++ b/stbf-demo/src/main/resources/logback.xml @@ -0,0 +1,11 @@ + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + \ No newline at end of file