DEV Community

Cover image for Integrating Telegram Notifications with Spring Boot Admin: A Practical Guide
Nuwan Jayarathna
Nuwan Jayarathna

Posted on

Integrating Telegram Notifications with Spring Boot Admin: A Practical Guide

Background

In modern microservice architectures, observability and monitoring are essential. Tools like Spring Boot Admin (SBA) provide real-time dashboards to monitor registered Spring Boot applications, track their health, and receive alerts when something goes wrong.

Telegram, being a lightweight, fast, and widely adopted messaging platform, serves as an excellent channel for real-time notifications, especially for developers and ops teams who are already active on the platform.

The Problem

How can we build a clean, reliable Telegram notifier that integrates with SBA 3.4.5 and sends alerts like service DOWN, OFFLINE, or UP changes?

The Solution

We’ll solve this by:

  1. Creating a Telegram bot.
  2. Fetching the chat ID where messages should be delivered.
  3. Implementing a custom notifier class in Spring Boot Admin that reacts to application status events.
  4. Configuring the notifier as a Spring Bean.

This solution uses Telegram Bot API and standard Java components (RestTemplate, Reactor, etc.) to ensure a light and scalable setup.

Implementation

Step 1: Create a Telegram Bot

  • Open Telegram and search for @botfather.
  • Type /newbot and follow the prompts to name and create your bot.
  • Save the bot token provided by BotFather.

Step 2: Get Your Chat ID

  • Start a chat with your new bot and send a message like "Hello Bot".
  • Open the following URL (replace YOUR_BOT_TOKEN): https://api.telegram.org/botYOUR_BOT_TOKEN/getUpdates
  • Look at the JSON response for the chat.id field. That is your chatId.

Step 3: Add SBA and Dependencies

Include Spring Boot Admin server starter in your Maven pom.xml:
Optional: Add lombok and spring-boot-starter-actuator if not already present

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Step 4: Create the Telegram Notifier Class

@Slf4j
public class TelegramNotifier extends AbstractEventNotifier {

    private final String botToken;
    private final String chatId;
    private final RestTemplate restTemplate;

    public TelegramNotifier(String botToken, String chatId, InstanceRepository repository) {
        super(repository);
        this.botToken = botToken;
        this.chatId = chatId;
        this.restTemplate = new RestTemplate();
    }

    @Override
    protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
            String message = String.format(
                    "*Status Change* - Service: %s >> %s - %s",
                    instance.getRegistration().getName(),
                    event.getType(), instance.getStatusInfo().getStatus()
            );
        sendTelegramMessage(message);
        return Mono.empty();
    }

    private void sendTelegramMessage(String message) {
        String url = String.format("https://api.telegram.org/bot%s/sendMessage", botToken);
        String payload = String.format("chat_id=%s&text=%s&parse_mode=Markdown", chatId, message);

            try {
                    restTemplate.postForObject(url + "?" + payload, null, String.class);
            } catch (Exception e) {
                    log.error("Failed to send Telegram notification", e);
            }
    }

}
Enter fullscreen mode Exit fullscreen mode

Optional: Filter Event Types

Due to Telegram rate limit, notify only DOWN or OFFLINE events:

if (event instanceof InstanceStatusChangedEvent statusChangedEvent) {
    String status = statusChangedEvent.getType();
    if ("DOWN".equals(status) || "OFFLINE".equals(status)) {
        sendTelegramMessage("Service down: " + instance.getStatusInfo().getStatus());
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Register the Notifier as a Spring Bean

@Configuration
public class NotifierConfig {

    @Autowired
    InstanceRepository repository;

    @Value("${telegram.bot.token}")
    private String botToken;

    @Value("${telegram.chat.id}")
    private String chatId;

    @Bean
    public TelegramNotifier telegramNotifier() {
        return new TelegramNotifier(botToken, chatId, repository);
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 6: Add to application.properties

telegram.bot.token=YOUR_BOT_TOKEN
telegram.chat.id=TELEGRAM_CHAT_ID
Enter fullscreen mode Exit fullscreen mode

Running the Application

  • Start your SBA server application.
  • Register Spring Boot clients (using spring-boot-admin-starter-client).
  • Simulate a failure (e.g., shut down one service).
  • Telegram message should appear within a second or two.

Example notification: Status Change - Service: order-service >> STATUS_CHANGED - UP

Pros and Cons

Pros:

  • Fast and real-time: Telegram notifications are almost instant.
  • Mobile-friendly: Easy to monitor on the go.
  • Lightweight: No extra messaging infrastructure like Kafka or RabbitMQ required.
  • Customizable: Easily expand to include uptime, memory, or custom actuator data.

Cons:

  • Not officially supported: Requires maintaining your own notifier class.
  • No retry/backoff: If Telegram API fails, notification may be lost.
  • Limited formatting: Telegram’s Markdown support is basic.
  • Security: Must protect your bot token and restrict bot access properly.

Final Thoughts

Adding Telegram notification support to Spring Boot Admin 3.4.5 is straightforward, yet powerful. It bridges a gap in observability by enabling real-time alerts to your most used messaging tool. Whether you're an SRE, developer, or DevOps engineer, this simple integration can improve your reaction time to production issues and keep your microservices in check.

With minimal code and no third-party infrastructure, you gain fast, actionable insights to your pocket.

👋 See you... next time!

Top comments (0)