Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions core/src/io/github/artemget/teleroute/bot/Connection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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;

/**
* Creates connection to the bot platform.
*
* @since 2.0.0
*/
public interface Connection {
/**
* Connects to the bot platform.
*/
void connect() throws Exception;
}
21 changes: 18 additions & 3 deletions telegrambots/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>
Expand All @@ -12,19 +12,34 @@
<name>eleroute-telegrambots</name>
<properties>
<logging.version>2.22.1</logging.version>
<telegrambots.version>9.1.0</telegrambots.version>
<telegrambots.version>9.2.0</telegrambots.version>
</properties>
<dependencies>
<dependency>
<groupId>io.github.artemget.eleroute</groupId>
<artifactId>core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.cactoos</groupId>
<artifactId>cactoos</artifactId>
<version>0.57.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-longpolling</artifactId>
<version>${telegrambots.version}</version>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-meta</artifactId>
<version>${telegrambots.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-client</artifactId>
<version>${telegrambots.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.cactoos.Proc;
import org.cactoos.proc.UncheckedProc;
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 {
/**
* 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;
}

@Override
public void consume(final Update update) {
new UncheckedProc<>((Update u) -> this.bot.exec(new TgBotWrap(u))).exec(update);
}
}
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, AutoCloseable {
/**
* 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 connect() throws Exception {
this.application.registerBot(this.token, this.consumer);
}

@Override
public void close() throws IOException {
new CheckedProc<>(
TelegramBotsLongPollingApplication::close,
ex -> new IOException("Failed to close Telegram Bot Connection", ex)
).exec(this.application);
}
}
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;