add demo app
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-09-05 12:20:05 +03:00
parent e5a9c82ff3
commit d25eac9366
5 changed files with 235 additions and 0 deletions

View File

@@ -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<PengradTelegramBot> 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.");
}
}

View File

@@ -0,0 +1,28 @@
<flow>
<actions>
<action id="10001" name="start-action" command="/start">Start</action>
<action id="10002" name="help-action" command="/help">Help</action>
</actions>
<screens>
<screen id="20001" name="on-start-screen">
<text>This is demo bot</text>
<keyboard>
<row>
<button actionRef="help-action">Action.name</button>
</row>
</keyboard>
</screen>
<screen id="20001" name="on-help-screen">
<text>This is demo help</text>
<keyboard>
<row>
<button actionRef="help-action">Action.name</button>
</row>
</keyboard>
</screen>
</screens>
<commands>
<command actionRef="start-action" screenRef="on-start-screen" id="30001" name="startCommand"/>
<command actionRef="help-action" screenRef="on-help-screen" id="30002" name="helpCommand"/>
</commands>
</flow>

View File

@@ -0,0 +1,11 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>