undo
Go Beyond the Code
arrow_forward_ios

Integrating a Telegram Bot in Java for Automated Security Alerts

June 19, 2024

In the realm of automated security applications, the need for real-time alerts is paramount. In this context, implementing Telegram messaging to notify users of specific situations is a practical solution since the integration setup is very quick and simple.

This article provides a comprehensive guide to integrating a Telegram bot into an existing Java application based on a real use case we have in one of our products. It will cover the process from setting up the bot to sending messages and ensuring proper documentation and logging.

This article will cover:

  1. Setting up a Telegram bot.
  2. Integrating the bot with a Java application.
  3. Sending messages through the bot.
  4. Adding the bot as an admin and ensuring proper logging.


Setting Up a Telegram Bot

  1. Create a Telegram Bot:some text
    • Open Telegram and search for the “BotFather” bot.
    • Start a chat with BotFather and use the /newbot command to create a new bot.
    • Follow the prompts to name the bot and obtain a unique bot token. This token is essential for authenticating API requests.
    • Optional. Use /setcommands – to change the list of commands supported by your bot. 
  2. Obtain the Chat ID:some text
    • Start a chat with the new bot.
    • Use the Telegram API to get the chat ID. Send a message to the bot and then visit https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates to view the message details, which include the chat ID.
  3. Add the Bot as an Admin:some text
    • Ensure the bot has administrative privileges in the group where it will send messages. This step is crucial for the bot to function correctly within group chats

Integrating the Telegram Bot with a Java Application

Step 1: Dependencies

Add the Telegrambots library dependency to your project - in this case, we use Maven so we need to add it to the pom.xml file:

Step 2: Create a Bot Class:

Implement a class that extends TelegramLongPollingBot and override necessary methods. Ensure proper logging is implemented using a Logger instance.

Step 3: Initialize the Bot:

Set up the bot in the main application class, ensuring proper initialization and logging. Here we show a very simplified example about how we can do it.

At this stage, when a message is sent to the bot on Telegram, new updates will be observed in the console and you will receive a message from the bot to your chat. By now, a Telegram bot has been successfully created.

Now let's dig a bit more into our concrete use case. In our case we had to create an /invite command to set up the bot and link it to a user. An improved version of this implementation could be to implement a Strategy pattern and make use of Java's polymorphism, but in this case since we only have one command to process, it would be an overengineered solution.

After we receive the message, we process it accordingly.

With the bot properly set-up, now can take care of the core use case here: sending messages with the security alerts. In this case we use the ability to attach photos to messages to include a snapshot of the camera that triggered the alert. This way, for instance we can see a picture of the person that was detected in a particular camera.

And the rest would be just linking the actual event processing to call the sendPhotoMessage method with the proper parameters.

Conclusion

In this article we've shown how to integrate a Telegram bot into a Java application. By following the steps outlined, you can seamlessly integrate Telegram bots into your apps, providing a robust solution for real-time notifications including media content like photos or even videos.

To learn more about this topic, click here.
Jonatan Piedra
Software Engineer & Solver

Start Your Digital Journey Now!

Which capabilities are you interested in?
You may select more than one.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.