Compare commits
10 Commits
02c5ef32fa
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| a06f9940ff | |||
| 8453ba42f8 | |||
| 56a4d4957b | |||
| c62de38f17 | |||
| 05af2f05a4 | |||
| 7270a34e97 | |||
| a318034d77 | |||
| 5880739849 | |||
| f2320ff2af | |||
| 1765abbdfd |
202
LICENSE.txt
Normal file
202
LICENSE.txt
Normal file
@@ -0,0 +1,202 @@
|
|||||||
|
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright (c) 2021 Ruslan Penkrat <penkrat@gmail.com>
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
135
pom.xml
135
pom.xml
@@ -1,70 +1,89 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
<groupId>ru.penkrat.stbf</groupId>
|
<groupId>ru.penkrat.stbf</groupId>
|
||||||
<artifactId>stbf-parent</artifactId>
|
<artifactId>stbf-parent</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.2-SNAPSHOT</version>
|
||||||
<name>stbf-parent</name>
|
<name>stbf-parent</name>
|
||||||
|
|
||||||
<description>Simple Telegram Bot Facade</description>
|
<description>Simple Telegram Bot Facade</description>
|
||||||
<properties>
|
|
||||||
<java.version>1.8</java.version>
|
|
||||||
<lombok.version>1.18.6</lombok.version>
|
|
||||||
<slf4j.version>1.7.25</slf4j.version>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<modules>
|
<scm>
|
||||||
<module>stbf-api</module>
|
<connection>scm:git:https://git.penkrat.ru/ruslan/stbf.git</connection>
|
||||||
<module>stbf-pengrad</module>
|
<developerConnection>scm:git:https://git.penkrat.ru/ruslan/stbf.git</developerConnection>
|
||||||
<module>stbf-test</module>
|
<tag>HEAD</tag>
|
||||||
<module>stbf-common</module>
|
</scm>
|
||||||
<module>stbf-rubenlagus</module>
|
|
||||||
<module>stbf-templates</module>
|
|
||||||
<module>stbf-demo</module>
|
|
||||||
</modules>
|
|
||||||
|
|
||||||
<dependencies>
|
<licenses>
|
||||||
<dependency>
|
<license>
|
||||||
<groupId>org.slf4j</groupId>
|
<name>Apache License, Version 2.0</name>
|
||||||
<artifactId>slf4j-api</artifactId>
|
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
|
||||||
<version>${slf4j.version}</version>
|
</license>
|
||||||
<scope>compile</scope>
|
</licenses>
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.projectlombok</groupId>
|
|
||||||
<artifactId>lombok</artifactId>
|
|
||||||
<version>${lombok.version}</version>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
<build>
|
<properties>
|
||||||
<plugins>
|
<java.version>1.8</java.version>
|
||||||
<plugin>
|
<lombok.version>1.18.6</lombok.version>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<slf4j.version>1.7.25</slf4j.version>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
</properties>
|
||||||
<configuration>
|
|
||||||
<source>${java.version}</source>
|
|
||||||
<target>${java.version}</target>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
|
|
||||||
<distributionManagement>
|
<modules>
|
||||||
<repository>
|
<module>stbf-api</module>
|
||||||
<id>${deployment.releases.id}</id>
|
<module>stbf-pengrad</module>
|
||||||
<name>Releases</name>
|
<module>stbf-test</module>
|
||||||
<url>${deployment.releases.url}</url>
|
<module>stbf-common</module>
|
||||||
</repository>
|
<module>stbf-rubenlagus</module>
|
||||||
<snapshotRepository>
|
<module>stbf-templates</module>
|
||||||
<id>${deployment.snapshots.id}</id>
|
<module>stbf-demo</module>
|
||||||
<name>Snapshots</name>
|
</modules>
|
||||||
<url>${deployment.snapshots.url}</url>
|
|
||||||
</snapshotRepository>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
<version>${slf4j.version}</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>${java.version}</source>
|
||||||
|
<target>${java.version}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-release-plugin</artifactId>
|
||||||
|
<version>3.0.0-M4</version>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<distributionManagement>
|
||||||
|
<repository>
|
||||||
|
<id>${deployment.releases.id}</id>
|
||||||
|
<name>Releases</name>
|
||||||
|
<url>${deployment.releases.url}</url>
|
||||||
|
</repository>
|
||||||
|
<snapshotRepository>
|
||||||
|
<id>${deployment.snapshots.id}</id>
|
||||||
|
<name>Snapshots</name>
|
||||||
|
<url>${deployment.snapshots.url}</url>
|
||||||
|
</snapshotRepository>
|
||||||
</distributionManagement>
|
</distributionManagement>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>ru.penkrat.stbf</groupId>
|
<groupId>ru.penkrat.stbf</groupId>
|
||||||
<artifactId>stbf-parent</artifactId>
|
<artifactId>stbf-parent</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.2-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>stbf-api</artifactId>
|
<artifactId>stbf-api</artifactId>
|
||||||
|
|||||||
@@ -14,4 +14,5 @@ public interface BotRequest {
|
|||||||
|
|
||||||
Long getChatId();
|
Long getChatId();
|
||||||
|
|
||||||
|
BotSession getSession();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package ru.penkrat.stbf.api;
|
||||||
|
|
||||||
|
public interface BotSession {
|
||||||
|
|
||||||
|
Object getAttribute(String name);
|
||||||
|
|
||||||
|
void setAttribute(String name, Object value);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package ru.penkrat.stbf.api;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface SessionProvider {
|
||||||
|
|
||||||
|
BotSession get(Long chatId);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,12 +1,10 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<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">
|
||||||
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>stbf-parent</artifactId>
|
<artifactId>stbf-parent</artifactId>
|
||||||
<groupId>ru.penkrat.stbf</groupId>
|
<groupId>ru.penkrat.stbf</groupId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.2-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>stbf-common</artifactId>
|
<artifactId>stbf-common</artifactId>
|
||||||
@@ -16,7 +14,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>ru.penkrat.stbf</groupId>
|
<groupId>ru.penkrat.stbf</groupId>
|
||||||
<artifactId>stbf-api</artifactId>
|
<artifactId>stbf-api</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.2-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
|
|||||||
@@ -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,18 @@
|
|||||||
|
package ru.penkrat.stbf.common.session;
|
||||||
|
|
||||||
|
import ru.penkrat.stbf.api.BotSession;
|
||||||
|
import ru.penkrat.stbf.api.SessionProvider;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
public class InMemBotSessionProvider implements SessionProvider {
|
||||||
|
|
||||||
|
private static final Map<Long, BotSession> MAP = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BotSession get(Long chatId) {
|
||||||
|
return MAP.computeIfAbsent(chatId, k -> new MapBackedBotSession(new ConcurrentHashMap<>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package ru.penkrat.stbf.common.session;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import ru.penkrat.stbf.api.BotSession;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
class MapBackedBotSession implements BotSession {
|
||||||
|
|
||||||
|
private final Map<String, Object> map;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getAttribute(String name) {
|
||||||
|
return map.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setAttribute(String name, Object value) {
|
||||||
|
map.put(name, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,11 +1,10 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?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"
|
<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">
|
||||||
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>stbf-parent</artifactId>
|
<artifactId>stbf-parent</artifactId>
|
||||||
<groupId>ru.penkrat.stbf</groupId>
|
<groupId>ru.penkrat.stbf</groupId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.2-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<groupId>ru.penkrat.stbf</groupId>
|
<groupId>ru.penkrat.stbf</groupId>
|
||||||
@@ -35,6 +34,11 @@
|
|||||||
<groupId>ru.penkrat.stbf</groupId>
|
<groupId>ru.penkrat.stbf</groupId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<artifactId>stbf-common</artifactId>
|
||||||
|
<groupId>ru.penkrat.stbf</groupId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>info.picocli</groupId>
|
<groupId>info.picocli</groupId>
|
||||||
<artifactId>picocli</artifactId>
|
<artifactId>picocli</artifactId>
|
||||||
@@ -108,8 +112,7 @@
|
|||||||
</goals>
|
</goals>
|
||||||
<configuration>
|
<configuration>
|
||||||
<transformers>
|
<transformers>
|
||||||
<transformer
|
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
|
||||||
<mainClass>ru.penkrat.stbf.demo.App</mainClass>
|
<mainClass>ru.penkrat.stbf.demo.App</mainClass>
|
||||||
</transformer>
|
</transformer>
|
||||||
</transformers>
|
</transformers>
|
||||||
|
|||||||
@@ -5,64 +5,67 @@ 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.impl.pengrad.PengradTelegramBot;
|
import ru.penkrat.stbf.impl.pengrad.PengradTelegramBot;
|
||||||
import ru.penkrat.stbf.templates.xml.XmlFlowResolver;
|
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);
|
||||||
|
|
||||||
private static AtomicReference<PengradTelegramBot> botAtomicReference = new AtomicReference<>();
|
private static AtomicReference<PengradTelegramBot> botAtomicReference = new AtomicReference<>();
|
||||||
|
|
||||||
@CommandLine.Option(names = {"-f", "--file"}, description = "The file with config.")
|
@CommandLine.Option(names = {"-f", "--file"}, description = "The file with config.")
|
||||||
private String flowFile = "classpath:/flow.xml";
|
private String flowFile = "classpath:/flow.xml";
|
||||||
|
|
||||||
@CommandLine.Option(names = {"-t", "--token"}, required = true, description = "bot token 1234:abscdf...")
|
@CommandLine.Option(names = {"-t", "--token"}, required = true, description = "bot token 1234:abscdf...")
|
||||||
private String botToken = "";
|
private String botToken = "";
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
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));
|
return () -> botAtomicReference.set(new PengradTelegramBot(token, chain));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onShutdown() {
|
private void onShutdown() {
|
||||||
try {
|
try {
|
||||||
if (botAtomicReference.get() != null) {
|
if (botAtomicReference.get() != null) {
|
||||||
botAtomicReference.get().close();
|
botAtomicReference.get().close();
|
||||||
log.info("Bot finished.");
|
log.info("Bot finished.");
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Error:", e);
|
log.error("Error:", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Thread botThread = new Thread(start(botToken, getCommandChain(flowFile)));
|
Thread botThread = new Thread(start(botToken, getCommandChain(flowFile)));
|
||||||
botThread.setDaemon(false);
|
botThread.setDaemon(false);
|
||||||
botThread.setName("stbf-bot-thread");
|
botThread.setName("stbf-bot-thread");
|
||||||
|
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(this::onShutdown));
|
Runtime.getRuntime().addShutdownHook(new Thread(this::onShutdown));
|
||||||
|
|
||||||
log.info("Starting bot...");
|
log.info("Starting bot...");
|
||||||
botThread.start();
|
botThread.start();
|
||||||
log.info("Bot started.");
|
log.info("Bot started.");
|
||||||
log.info("Press Ctrl+C to exit.");
|
log.info("Press Ctrl+C to exit.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,7 +68,8 @@
|
|||||||
</keyboard>
|
</keyboard>
|
||||||
</screen>
|
</screen>
|
||||||
<screen id="20005" name="inline-test-2-screen">
|
<screen id="20005" name="inline-test-2-screen">
|
||||||
<text>Inline Screen #2</text>
|
<text>Inline Screen #2
|
||||||
|
================</text>
|
||||||
<keyboard>
|
<keyboard>
|
||||||
<row>
|
<row>
|
||||||
<button actionRef="inline1-action"></button>
|
<button actionRef="inline1-action"></button>
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>ru.penkrat.stbf</groupId>
|
<groupId>ru.penkrat.stbf</groupId>
|
||||||
<artifactId>stbf-parent</artifactId>
|
<artifactId>stbf-parent</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.2-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>stbf-pengrad</artifactId>
|
<artifactId>stbf-pengrad</artifactId>
|
||||||
@@ -18,12 +16,12 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>ru.penkrat.stbf</groupId>
|
<groupId>ru.penkrat.stbf</groupId>
|
||||||
<artifactId>stbf-api</artifactId>
|
<artifactId>stbf-api</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.2-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.pengrad</groupId>
|
<groupId>com.github.pengrad</groupId>
|
||||||
<artifactId>java-telegram-bot-api</artifactId>
|
<artifactId>java-telegram-bot-api</artifactId>
|
||||||
<version>[4.9.0,5.2.0]</version>
|
<version>6.2.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
package ru.penkrat.stbf.impl.pengrad;
|
package ru.penkrat.stbf.impl.pengrad;
|
||||||
|
|
||||||
import static lombok.AccessLevel.PROTECTED;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import com.pengrad.telegrambot.model.CallbackQuery;
|
import com.pengrad.telegrambot.model.CallbackQuery;
|
||||||
import com.pengrad.telegrambot.model.Contact;
|
import com.pengrad.telegrambot.model.Contact;
|
||||||
import com.pengrad.telegrambot.model.Message;
|
import com.pengrad.telegrambot.model.Message;
|
||||||
import com.pengrad.telegrambot.model.Update;
|
import com.pengrad.telegrambot.model.Update;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import ru.penkrat.stbf.api.BotRequest;
|
import ru.penkrat.stbf.api.BotRequest;
|
||||||
|
import ru.penkrat.stbf.api.BotSession;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static lombok.AccessLevel.PROTECTED;
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class BotRequestImpl implements BotRequest {
|
public class BotRequestImpl implements BotRequest {
|
||||||
@@ -58,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");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import com.pengrad.telegrambot.request.AbstractSendRequest;
|
|||||||
import com.pengrad.telegrambot.request.DeleteMessage;
|
import com.pengrad.telegrambot.request.DeleteMessage;
|
||||||
import com.pengrad.telegrambot.request.EditMessageText;
|
import com.pengrad.telegrambot.request.EditMessageText;
|
||||||
import com.pengrad.telegrambot.request.SendDocument;
|
import com.pengrad.telegrambot.request.SendDocument;
|
||||||
|
import com.pengrad.telegrambot.response.BaseResponse;
|
||||||
import com.pengrad.telegrambot.response.SendResponse;
|
import com.pengrad.telegrambot.response.SendResponse;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -27,7 +28,7 @@ public class BotResponseImpl implements BotResponse {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void send(Screen screen) {
|
public void send(Screen screen) {
|
||||||
log.debug("Send message: \n============\n{}\n============", screen.getText().trim());
|
log.trace("Send trace: \n============\n{}\n============", screen.getText().trim());
|
||||||
|
|
||||||
AbstractSendRequest<? extends AbstractSendRequest> sendMessage = SendMethodUtils.createFromScreen(chatId(), screen);
|
AbstractSendRequest<? extends AbstractSendRequest> sendMessage = SendMethodUtils.createFromScreen(chatId(), screen);
|
||||||
|
|
||||||
@@ -36,8 +37,8 @@ public class BotResponseImpl implements BotResponse {
|
|||||||
KeyboardButton[][] keyboard = kk.getKeyboard();
|
KeyboardButton[][] keyboard = kk.getKeyboard();
|
||||||
InlineKeyboardButton[][] inlineKeyboard = kk.getInlineKeyboard();
|
InlineKeyboardButton[][] inlineKeyboard = kk.getInlineKeyboard();
|
||||||
|
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isTraceEnabled()) {
|
||||||
log.debug("{}", kk.toFriendlyString());
|
log.trace("{}", kk.toFriendlyString());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inlineKeyboard != null && inlineKeyboard.length > 0) {
|
if (inlineKeyboard != null && inlineKeyboard.length > 0) {
|
||||||
@@ -45,12 +46,14 @@ public class BotResponseImpl implements BotResponse {
|
|||||||
} else if (keyboard != null && keyboard.length > 0) {
|
} else if (keyboard != null && keyboard.length > 0) {
|
||||||
sendMessage = sendMessage.replyMarkup(new ReplyKeyboardMarkup(keyboard));
|
sendMessage = sendMessage.replyMarkup(new ReplyKeyboardMarkup(keyboard));
|
||||||
} else {
|
} else {
|
||||||
log.debug("No keyboard");
|
log.trace("No keyboard");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final SendResponse sendResponse = telegramBot.execute(sendMessage);
|
final SendResponse sendResponse = telegramBot.execute(sendMessage);
|
||||||
SendMethodUtils.processResponse(screen, sendResponse);
|
SendMethodUtils.processResponse(screen, sendResponse);
|
||||||
|
|
||||||
|
log.debug("Response message = {}", sendResponse.message().messageId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -76,18 +79,23 @@ public class BotResponseImpl implements BotResponse {
|
|||||||
KeyboardImpl kk = (KeyboardImpl) screen.getKeyboard();
|
KeyboardImpl kk = (KeyboardImpl) screen.getKeyboard();
|
||||||
InlineKeyboardButton[][] inlineKeyboard = kk.getInlineKeyboard();
|
InlineKeyboardButton[][] inlineKeyboard = kk.getInlineKeyboard();
|
||||||
|
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isTraceEnabled()) {
|
||||||
log.debug("{}", kk.toFriendlyString());
|
log.trace("{}", kk.toFriendlyString());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inlineKeyboard != null && inlineKeyboard.length > 0) {
|
if (inlineKeyboard != null && inlineKeyboard.length > 0) {
|
||||||
editMessage = editMessage.replyMarkup(new InlineKeyboardMarkup(inlineKeyboard));
|
editMessage = editMessage.replyMarkup(new InlineKeyboardMarkup(inlineKeyboard));
|
||||||
} else {
|
} else {
|
||||||
log.debug("No keyboard");
|
log.trace("No keyboard");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
telegramBot.execute(editMessage);
|
final BaseResponse response = telegramBot.execute(editMessage);
|
||||||
|
if (response.isOk()) {
|
||||||
|
log.debug("Response ok, edit message = {}", messageId());
|
||||||
|
} else {
|
||||||
|
log.debug("Response error code {}", response.errorCode());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -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*";
|
||||||
|
|||||||
@@ -2,11 +2,13 @@ package ru.penkrat.stbf.impl.pengrad;
|
|||||||
|
|
||||||
import com.pengrad.telegrambot.TelegramBot;
|
import com.pengrad.telegrambot.TelegramBot;
|
||||||
import com.pengrad.telegrambot.UpdatesListener;
|
import com.pengrad.telegrambot.UpdatesListener;
|
||||||
|
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 java.util.Optional;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class PengradTelegramBot extends TelegramBot implements AutoCloseable {
|
public class PengradTelegramBot extends TelegramBot implements AutoCloseable {
|
||||||
|
|
||||||
@@ -15,6 +17,13 @@ public class PengradTelegramBot extends TelegramBot implements AutoCloseable {
|
|||||||
this.setUpdatesListener(updates -> {
|
this.setUpdatesListener(updates -> {
|
||||||
for (Update update : updates) {
|
for (Update update : updates) {
|
||||||
try {
|
try {
|
||||||
|
final Long chatId = Optional.of(update)
|
||||||
|
.map(Update::callbackQuery)
|
||||||
|
.map(CallbackQuery::message)
|
||||||
|
.orElseGet(() -> update.message()).chat().id();
|
||||||
|
|
||||||
|
log.debug("New message in chat {}", chatId);
|
||||||
|
|
||||||
commandChain.processCommand(
|
commandChain.processCommand(
|
||||||
new BotRequestImpl(update),
|
new BotRequestImpl(update),
|
||||||
new BotResponseImpl(update, this));
|
new BotResponseImpl(update, this));
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<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">
|
||||||
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>stbf-parent</artifactId>
|
<artifactId>stbf-parent</artifactId>
|
||||||
<groupId>ru.penkrat.stbf</groupId>
|
<groupId>ru.penkrat.stbf</groupId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.2-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>stbf-rubenlagus</artifactId>
|
<artifactId>stbf-rubenlagus</artifactId>
|
||||||
@@ -17,7 +15,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>ru.penkrat.stbf</groupId>
|
<groupId>ru.penkrat.stbf</groupId>
|
||||||
<artifactId>stbf-api</artifactId>
|
<artifactId>stbf-api</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.2-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.telegram</groupId>
|
<groupId>org.telegram</groupId>
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
package ru.penkrat.stbf.impl.rubenlagus;
|
package ru.penkrat.stbf.impl.rubenlagus;
|
||||||
|
|
||||||
import static lombok.AccessLevel.PROTECTED;
|
import lombok.Getter;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
|
import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
|
||||||
import org.telegram.telegrambots.meta.api.objects.Contact;
|
import org.telegram.telegrambots.meta.api.objects.Contact;
|
||||||
import org.telegram.telegrambots.meta.api.objects.Message;
|
import org.telegram.telegrambots.meta.api.objects.Message;
|
||||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import ru.penkrat.stbf.api.BotRequest;
|
import ru.penkrat.stbf.api.BotRequest;
|
||||||
|
import ru.penkrat.stbf.api.BotSession;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static lombok.AccessLevel.PROTECTED;
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
class BotRequestImpl implements BotRequest {
|
class BotRequestImpl implements BotRequest {
|
||||||
@@ -52,7 +52,12 @@ class BotRequestImpl implements BotRequest {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Long getChatId() {
|
public Long getChatId() {
|
||||||
return update.getMessage().getChatId();
|
return Utils.getChatId(update);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BotSession getSession() {
|
||||||
|
throw new UnsupportedOperationException("Session is not supported");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
package ru.penkrat.stbf.impl.rubenlagus;
|
package ru.penkrat.stbf.impl.rubenlagus;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
||||||
import org.telegram.telegrambots.meta.TelegramBotsApi;
|
import org.telegram.telegrambots.meta.TelegramBotsApi;
|
||||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
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 lombok.extern.slf4j.Slf4j;
|
|
||||||
import ru.penkrat.stbf.api.BotCommandChain;
|
import ru.penkrat.stbf.api.BotCommandChain;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package ru.penkrat.stbf.impl.rubenlagus;
|
||||||
|
|
||||||
|
import lombok.experimental.UtilityClass;
|
||||||
|
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||||
|
|
||||||
|
@UtilityClass
|
||||||
|
class Utils {
|
||||||
|
|
||||||
|
Long getChatId(Update update) {
|
||||||
|
if (update.getMessage() != null) {
|
||||||
|
return update.getMessage().getChatId();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (update.getCallbackQuery() != null) {
|
||||||
|
update.getCallbackQuery().getMessage().getChatId();
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IllegalStateException("Can't extract chatId from 'Update'");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,12 +1,10 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<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/maven-v4_0_0.xsd">
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>stbf-parent</artifactId>
|
<artifactId>stbf-parent</artifactId>
|
||||||
<groupId>ru.penkrat.stbf</groupId>
|
<groupId>ru.penkrat.stbf</groupId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.2-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>stbf-templates</artifactId>
|
<artifactId>stbf-templates</artifactId>
|
||||||
<properties>
|
<properties>
|
||||||
|
|||||||
@@ -2,9 +2,14 @@ package ru.penkrat.stbf.templates.utils;
|
|||||||
|
|
||||||
import lombok.experimental.UtilityClass;
|
import lombok.experimental.UtilityClass;
|
||||||
|
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public class StringUtils {
|
public class StringUtils {
|
||||||
|
|
||||||
|
public static final String NL_DELIMITER = "\n";
|
||||||
|
|
||||||
public boolean isEmpty(String string) {
|
public boolean isEmpty(String string) {
|
||||||
return string == null || string.isEmpty();
|
return string == null || string.isEmpty();
|
||||||
}
|
}
|
||||||
@@ -12,4 +17,13 @@ public class StringUtils {
|
|||||||
public boolean isNotEmpty(String string) {
|
public boolean isNotEmpty(String string) {
|
||||||
return !isEmpty(string);
|
return !isEmpty(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String ltrim(String text) {
|
||||||
|
if (text == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return Stream.of(text.split(NL_DELIMITER))
|
||||||
|
.map(string -> string.trim())
|
||||||
|
.collect(Collectors.joining(NL_DELIMITER));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,27 +44,35 @@ class FlowScreenResolverDelegate implements ScreenResolver {
|
|||||||
public Screen getScreen(String name) {
|
public Screen getScreen(String name) {
|
||||||
ScreenItem item = resolver.get(name);
|
ScreenItem item = resolver.get(name);
|
||||||
|
|
||||||
|
String text = item.getText();
|
||||||
|
if (!item.isPreserve()) {
|
||||||
|
text = StringUtils.ltrim(text);
|
||||||
|
}
|
||||||
if (StringUtils.isNotEmpty(item.getMediaRef())) {
|
if (StringUtils.isNotEmpty(item.getMediaRef())) {
|
||||||
final Media media = mediaResolver.getMedia(item.getMediaRef());
|
final Media media = mediaResolver.getMedia(item.getMediaRef());
|
||||||
final MediaScreen mediaScreen = new MediaScreen(item.getText(), media, buildKeyboard(item.getKeyboard(), null));
|
final MediaScreen mediaScreen = new MediaScreen(text, media, buildKeyboard(item.getKeyboard(), null));
|
||||||
return new RamFileIdStorageMediaScreen(mediaScreen);
|
return new RamFileIdStorageMediaScreen(mediaScreen);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new TextScreen(item.getText(), buildKeyboard(item.getKeyboard(), null));
|
return new TextScreen(text, buildKeyboard(item.getKeyboard(), null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Screen getScreen(String name, Object context) {
|
public Screen getScreen(String name, Object context) {
|
||||||
ScreenItem item = resolver.get(name);
|
ScreenItem item = resolver.get(name);
|
||||||
|
|
||||||
|
String text = item.getText();
|
||||||
|
if (!item.isPreserve()) {
|
||||||
|
text = StringUtils.ltrim(text);
|
||||||
|
}
|
||||||
if (StringUtils.isNotEmpty(item.getMediaRef())) {
|
if (StringUtils.isNotEmpty(item.getMediaRef())) {
|
||||||
final Media media = mediaResolver.getMedia(item.getMediaRef());
|
final Media media = mediaResolver.getMedia(item.getMediaRef());
|
||||||
final MediaScreen mediaScreen = new MediaScreen(templateRenderer.render(item.getText(), context), media,
|
final MediaScreen mediaScreen = new MediaScreen(templateRenderer.render(text, context), media,
|
||||||
resolveKeyboard(item.getKeyboard(), context));
|
resolveKeyboard(item.getKeyboard(), context));
|
||||||
return new RamFileIdStorageMediaScreen(mediaScreen);
|
return new RamFileIdStorageMediaScreen(mediaScreen);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new TextScreen(templateRenderer.render(item.getText(), context),
|
return new TextScreen(templateRenderer.render(text, context),
|
||||||
resolveKeyboard(item.getKeyboard(), context));
|
resolveKeyboard(item.getKeyboard(), context));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,4 +15,7 @@ class ScreenItem extends NamedItem {
|
|||||||
|
|
||||||
private KeyboardWrapper keyboard = new KeyboardWrapper();
|
private KeyboardWrapper keyboard = new KeyboardWrapper();
|
||||||
|
|
||||||
|
@JacksonXmlProperty(isAttribute = true)
|
||||||
|
private boolean preserve;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<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">
|
||||||
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>stbf-parent</artifactId>
|
<artifactId>stbf-parent</artifactId>
|
||||||
<groupId>ru.penkrat.stbf</groupId>
|
<groupId>ru.penkrat.stbf</groupId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.2-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>stbf-test</artifactId>
|
<artifactId>stbf-test</artifactId>
|
||||||
@@ -16,12 +14,12 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>ru.penkrat.stbf</groupId>
|
<groupId>ru.penkrat.stbf</groupId>
|
||||||
<artifactId>stbf-api</artifactId>
|
<artifactId>stbf-api</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.2-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>ru.penkrat.stbf</groupId>
|
<groupId>ru.penkrat.stbf</groupId>
|
||||||
<artifactId>stbf-common</artifactId>
|
<artifactId>stbf-common</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.2-SNAPSHOT</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import java.util.Random;
|
|||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import ru.penkrat.stbf.api.BotRequest;
|
import ru.penkrat.stbf.api.BotRequest;
|
||||||
|
import ru.penkrat.stbf.api.BotSession;
|
||||||
|
|
||||||
@Builder
|
@Builder
|
||||||
class BotRequestImpl implements BotRequest {
|
class BotRequestImpl implements BotRequest {
|
||||||
@@ -14,6 +15,7 @@ class BotRequestImpl implements BotRequest {
|
|||||||
private String phoneNumber;
|
private String phoneNumber;
|
||||||
private String callbackData;
|
private String callbackData;
|
||||||
private String callbackMessageText;
|
private String callbackMessageText;
|
||||||
|
private BotSession session;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Builder.Default
|
@Builder.Default
|
||||||
@@ -39,4 +41,9 @@ class BotRequestImpl implements BotRequest {
|
|||||||
return Optional.ofNullable(callbackMessageText);
|
return Optional.ofNullable(callbackMessageText);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BotSession getSession() {
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user