This commit is contained in:
127
stbf-demo/pom.xml
Normal file
127
stbf-demo/pom.xml
Normal file
@@ -0,0 +1,127 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<artifactId>stbf-parent</artifactId>
|
||||
<groupId>ru.penkrat.stbf</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.penkrat.stbf</groupId>
|
||||
<artifactId>stbf-demo</artifactId>
|
||||
<name>stbf-demo</name>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.jar.plugin.version>3.0.2</maven.jar.plugin.version>
|
||||
<maven-compiler-plugin-version>3.8.1</maven-compiler-plugin-version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<assertj.version>3.9.0</assertj.version>
|
||||
<mockito.version>2.13.0</mockito.version>
|
||||
<jmustache.version>1.14</jmustache.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<artifactId>stbf-templates</artifactId>
|
||||
<groupId>ru.penkrat.stbf</groupId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<artifactId>stbf-pengrad</artifactId>
|
||||
<groupId>ru.penkrat.stbf</groupId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>info.picocli</groupId>
|
||||
<artifactId>picocli</artifactId>
|
||||
<version>4.6.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.samskivert</groupId>
|
||||
<artifactId>jmustache</artifactId>
|
||||
<version>${jmustache.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-core</artifactId>
|
||||
<version>1.2.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.2.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<!-- annotationProcessorPaths requires maven-compiler-plugin version 3.5 or higher -->
|
||||
<version>${maven-compiler-plugin-version}</version>
|
||||
<configuration>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>info.picocli</groupId>
|
||||
<artifactId>picocli-codegen</artifactId>
|
||||
<version>4.6.1</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
<compilerArgs>
|
||||
<arg>-Aproject=${project.groupId}/${project.artifactId}</arg>
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.2.4</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<transformers>
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>ru.penkrat.stbf.demo.App</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
<artifactSet>
|
||||
<excludes>
|
||||
<exclude>junit:junit</exclude>
|
||||
</excludes>
|
||||
</artifactSet>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
68
stbf-demo/src/main/java/ru/penkrat/stbf/demo/App.java
Normal file
68
stbf-demo/src/main/java/ru/penkrat/stbf/demo/App.java
Normal 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.");
|
||||
}
|
||||
}
|
||||
28
stbf-demo/src/main/resources/flow.xml
Normal file
28
stbf-demo/src/main/resources/flow.xml
Normal 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>
|
||||
11
stbf-demo/src/main/resources/logback.xml
Normal file
11
stbf-demo/src/main/resources/logback.xml
Normal 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>
|
||||
Reference in New Issue
Block a user