-
Notifications
You must be signed in to change notification settings - Fork 4
Add telegram bot creation template #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * MIT License | ||
| * | ||
| * Copyright (c) 2024-2026. Artem Getmanskii | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in all | ||
| * copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| * SOFTWARE. | ||
| * | ||
| */ | ||
|
|
||
| package io.github.artemget.teleroute.bot; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Creates connection to the bot platform. | ||
| * | ||
| * @since 2.0.0 | ||
| */ | ||
| public interface Connection extends AutoCloseable { | ||
| /** | ||
| * Connects to the bot platform. | ||
| */ | ||
| void open() throws Exception; | ||
|
|
||
| void close() throws IOException; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
telegrambots/src/io/github/artemget/teleroute/telegrambots/bot/BotTg.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* | ||
| * MIT License | ||
| * | ||
| * Copyright (c) 2024-2026. Artem Getmanskii | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in all | ||
| * copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| * SOFTWARE. | ||
| * | ||
| */ | ||
|
|
||
| package io.github.artemget.teleroute.telegrambots.bot; | ||
|
|
||
| import io.github.artemget.teleroute.bot.BotEnvelope; | ||
| import io.github.artemget.teleroute.route.Route; | ||
| import io.github.artemget.teleroute.route.RouteDfs; | ||
| import io.github.artemget.teleroute.telegrambots.update.TgBotWrap; | ||
| import io.github.artemget.teleroute.update.Wrap; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.cactoos.Proc; | ||
| import org.telegram.telegrambots.client.okhttp.OkHttpTelegramClient; | ||
| import org.telegram.telegrambots.longpolling.util.LongPollingSingleThreadUpdateConsumer; | ||
| import org.telegram.telegrambots.meta.api.objects.Update; | ||
| import org.telegram.telegrambots.meta.generics.TelegramClient; | ||
|
|
||
| /** | ||
| * Telegram consumer template. | ||
| * | ||
| * @since 2.0.0 | ||
| */ | ||
| public final class BotTg implements LongPollingSingleThreadUpdateConsumer { | ||
| /** | ||
| * Logger. | ||
| */ | ||
| private static final Logger LOGGER = LogManager.getLogger(); | ||
|
|
||
| /** | ||
| * Procedure for telegram update handling. | ||
| */ | ||
| private final Proc<Wrap<Update>> bot; | ||
|
|
||
| @SafeVarargs | ||
| public BotTg(final String token, final Route<Update, TelegramClient>... routes) { | ||
| this(new OkHttpTelegramClient(token), new RouteDfs<>(routes)); | ||
| } | ||
|
|
||
| public BotTg(final String token, final Route<Update, TelegramClient> route) { | ||
| this(new OkHttpTelegramClient(token), route); | ||
| } | ||
|
|
||
| @SafeVarargs | ||
| public BotTg(final TelegramClient client, final Route<Update, TelegramClient>... routes) { | ||
| this(new BotEnvelope<>(client, new RouteDfs<>(routes))); | ||
| } | ||
|
|
||
| public BotTg(final TelegramClient client, final Route<Update, TelegramClient> route) { | ||
| this(new BotEnvelope<>(client, route)); | ||
| } | ||
|
|
||
| public BotTg(final Proc<Wrap<Update>> bot) { | ||
| this.bot = bot; | ||
| } | ||
|
|
||
| //@checkstyle IllegalCatchCheck (8 lines) | ||
| @SuppressWarnings("PMD.AvoidCatchingGenericException") | ||
| @Override | ||
| public void consume(final Update update) { | ||
| try { | ||
| this.bot.exec(new TgBotWrap(update)); | ||
| } catch (final Exception exception) { | ||
| LOGGER.error("Error occurred while processing update {}", update, exception); | ||
| } | ||
| } | ||
| } |
101 changes: 101 additions & 0 deletions
101
telegrambots/src/io/github/artemget/teleroute/telegrambots/bot/ConnectionTg.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /* | ||
| * MIT License | ||
| * | ||
| * Copyright (c) 2024-2026. Artem Getmanskii | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in all | ||
| * copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| * SOFTWARE. | ||
| * | ||
| */ | ||
|
|
||
| package io.github.artemget.teleroute.telegrambots.bot; | ||
|
|
||
| import io.github.artemget.teleroute.bot.Connection; | ||
| import io.github.artemget.teleroute.route.Route; | ||
| import io.github.artemget.teleroute.route.RouteDfs; | ||
| import java.io.IOException; | ||
| import org.cactoos.proc.CheckedProc; | ||
| import org.telegram.telegrambots.longpolling.TelegramBotsLongPollingApplication; | ||
| import org.telegram.telegrambots.longpolling.interfaces.LongPollingUpdateConsumer; | ||
| import org.telegram.telegrambots.meta.api.objects.Update; | ||
| import org.telegram.telegrambots.meta.generics.TelegramClient; | ||
|
|
||
| /** | ||
| * Telegram Bot Connection. | ||
| * | ||
| * @since 2.0.0 | ||
| * @todo #41:30min After release jdk 26 change IOException to Exception | ||
| * for AutoCloseable interface, see | ||
| * <a href="https://bugs.openjdk.org/browse/JDK-8155591">SDK bug</a> | ||
| */ | ||
| public final class ConnectionTg implements Connection { | ||
| /** | ||
| * Bot token. | ||
| */ | ||
| private final String token; | ||
|
|
||
| /** | ||
| * Consumer. | ||
| */ | ||
| private final LongPollingUpdateConsumer consumer; | ||
|
|
||
| /** | ||
| * Application. | ||
| */ | ||
| private final TelegramBotsLongPollingApplication application; | ||
|
|
||
| @SafeVarargs | ||
| public ConnectionTg(final String token, final Route<Update, TelegramClient>... routes) { | ||
| this(token, new RouteDfs<>(routes)); | ||
| } | ||
|
|
||
| public ConnectionTg(final String token, final Route<Update, TelegramClient> route) { | ||
| this(token, new TelegramBotsLongPollingApplication(), new BotTg(token, route)); | ||
| } | ||
|
|
||
| public ConnectionTg( | ||
| final String token, | ||
| final TelegramBotsLongPollingApplication application, | ||
| final Route<Update, TelegramClient> route | ||
| ) { | ||
| this(token, application, new BotTg(token, route)); | ||
| } | ||
|
|
||
| public ConnectionTg( | ||
| final String token, | ||
| final TelegramBotsLongPollingApplication application, | ||
| final LongPollingUpdateConsumer consumer | ||
| ) { | ||
| this.token = token; | ||
| this.application = application; | ||
| this.consumer = consumer; | ||
| } | ||
|
|
||
| @Override | ||
| public void open() throws Exception { | ||
| this.application.registerBot(this.token, this.consumer); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
ArtemGet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| new CheckedProc<>( | ||
| TelegramBotsLongPollingApplication::close, | ||
| ex -> new IOException("Failed to close Telegram Bot Connection", ex) | ||
| ).exec(this.application); | ||
| } | ||
| } | ||
26 changes: 26 additions & 0 deletions
26
telegrambots/src/io/github/artemget/teleroute/telegrambots/bot/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * MIT License | ||
| * | ||
| * Copyright (c) 2024-2026. Artem Getmanskii | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in all | ||
| * copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| * SOFTWARE. | ||
| * | ||
| */ | ||
|
|
||
| package io.github.artemget.teleroute.telegrambots.bot; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.