A lightweight, developer-friendly Spring Boot starter for building Telegram bots using Java 17 and Spring Boot 3.
Supports long polling, webhooks, routing, interceptors, and pluggable HTTP clients.
- Plug & Play β launch a Telegram bot easily.
- Long Polling & Webhook β two modes out of the box.
- Smart Routing β commands, text messages, update types.
- Interceptors β logging, filtering, metrics.
- Flexible HTTP Client β OkHttp or Spring RestClient.
- Auto Configuration β minimal boilerplate.
- Extensible Architecture β customize any component.
- Examples Included β ready-to-run sample bots.
<dependency>
<groupId>io.github.ksilisk</groupId>
<artifactId>telegram-bot-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>telegram:
bot:
token: ${BOT_TOKEN}@Component
public class StartCommandHandler implements CommandUpdateHandler {
private final TelegramBotExecutor telegramBotExecutor;
public StartCommandHandler(TelegramBotExecutor telegramBotExecutor) {
this.telegramBotExecutor = telegramBotExecutor;
}
@Override
public void handle(Update update) {
SendMessage sendMessage = new SendMessage(update.message().from().id(), "Simple Hello!");
telegramBotExecutor.execute(sendMessage);
}
@Override
public Set<String> commands() {
return Set.of("/start", "/help");
}
}Thatβs it β your bot is live β¨
Telegram β Ingress β Delivery -> Interceptors β Dispatcher β Router -> Your Handlers
- Update arrives (long polling or webhook)
- Passes through optional interceptors
- Dispatcher selects the right handler
- Fallback strategies handle no-match and exceptions
You write only handlers.
| Property | Description |
|---|---|
telegram.bot.token |
Bot token (required) |
telegram.bot.mode |
LONG_POLLING (default) or WEBHOOK |
telegram.bot.client.implementation |
AUTO / OKHTTP / SPRING |
- Webhook mode requires
telegram-bot-webhook. - AUTO prefers OkHttp if present β otherwise uses Spring
RestClient.
@Component
public class TestCallbackHandler implements CallbackUpdateHandler {
private final TelegramBotExecutor telegramBotExecutor;
public TestCallbackHandler(TelegramBotExecutor telegramBotExecutor) {
this.telegramBotExecutor = telegramBotExecutor;
}
@Override
public void handle(Update update) {
Long chatId = update.callbackQuery().maybeInaccessibleMessage().chat().id();
SendMessage sendMessage = new SendMessage(chatId, "Successfully handled callback 'test'");
telegramBotExecutor.execute(sendMessage);
}
@Override
public Set<String> callbacks() {
return Set.of("test");
}
}@Component
public class AnyMessageHandler implements MessageUpdateHandler {
private final TelegramBotExecutor telegramBotExecutor;
public AnyMessageHandler(TelegramBotExecutor telegramBotExecutor) {
this.telegramBotExecutor = telegramBotExecutor;
}
@Override
public void handle(Update update) {
Long chatId = update.message().chat().id();
SendMessage sendMessage = new SendMessage(chatId, "Your message is successfully handled!");
telegramBotExecutor.execute(sendMessage);
}
}
@Component
public class AnyMessageRule implements MessageUpdateRule {
private final AnyMessageUpdateHandler anyMessageHandler;
public AnyMessageRule(AnyMessageUpdateHandler anyMessageHandler) {
this.anyMessageHandler = anyMessageHandler;
}
@Override
public Matcher<Message> matcher() {
return update -> true; // handle any message, but you can add your custom rule
}
@Override
public UpdateHandler handler() {
return anyMessageHandler;
}
}@Component
public class LoggingUpdateInterceptor implements UpdateInterceptor {
private static final Logger log = LoggerFactory.getLogger(LoggingUpdateInterceptor.class);
@Override
public Update intercept(Update input) {
log.info("Handled an update: {}", input);
return input;
}
}- Interceptors must be thread-safe
- They run before dispatcher
- Return
nullto drop an update
| Mode | Behavior |
|---|---|
AUTO |
Prefer OkHttp β fallback to RestClient |
OKHTTP |
Fail if OkHttp not on classpath |
SPRING |
Fail if Spring Web not on classpath |
Examples are located in the /examples directory:
- telegram-bot-sample-long-polling β minimal bot example
Run:
cd examples/telegram-bot-sample-long-polling
mvn spring-boot:runPRs are welcome!
See CONTRIBUTING.md for details.
Security guidelines are available in SECURITY.md.
MIT β see LICENSE file.