Creating a Telegram Bot With Micronaut
Learn how to create a Telegram bot with some basic commands for fun using Micronaut.
Join the DZone community and get the full member experience.Join For Free
Telegram is a well known instant messaging application used worldwide for millions of users, so no presentation is needed here. Today, on behalf of the Apiumhub team, we are going to focus on a special feature: bots, third-parties applications that run inside Telegram. Users can interact with bots via messages and commands. These applications can have multiple usages: getting some information, purchasing items, chat support for ecommerce, you name it. And today’s article is about how to create a Telegram bot with Micronaut.
Creating a Telegram Bot With Micronaut
To see how they work, we are going to create a Telegram bot with some basic commands for fun. The project will be a bot that returns information about a requested population. We are getting this data from an open third party API. The service will be created using the Micronaut framework to see if it is suitable for our needs.
To have our bot up and running we need 2 pieces:
- A telegram account which will be our bot
- A webservice that will interact with the bot
Let’s see the steps to create the whole project.
Create the Bot
Telegram offers a bot that creates the new accounts: the botfather.
Interacting with the botfather via your telegram account, typing some commands (/newbot to start), it will create your bot.
The botfather will create an access token which will be needed to interact with the telegram API.
Configure the Webservice
To receive the requests from our bot, Telegram offers two options:
- Long polling: Our webservice will be connected to Telegram servers all the time just waiting for a request
- Webhooks: Our service has to provide an API where Telegram will call every time there is a new message.
We are going to use webhooks, since it’s simpler and consumes less resources (our webservice is idle just waiting for new connections, instead of having one open connection the whole time).
To configure the url a simple call to telegram api is needed:
The url must be with https
The port can only be one of these: 443, 80, 88 or 8443
For security reasons, it may be a good idea to allow only requests from telegram networks.
Our bot is already configured and any message that we send to it will be received to our webservice.
How to Reply to the Requests
To reply our bot with the response, Telegram also gives us two options:
1. Reply the response in the same request.
2. Reply the request with a HTTP 200 and send a new request to the API.
Since our webservice will interact with third providers and we can’t know how long it will be their response, we’ll choose the second option: we’ll send an immediate OK and we’ll send a POST to the API with the response once we have the data.
Create the Webservice
To create the webservice, we are going to use Micronaut. It’s a JVM framework (it can be written in Java, Kotlin, or Groovy) that promises low memory consumption and offers a non-blocking http server, which seems very convenient for our use case: We need a light framework that replies the OK immediately while it processes the request. This asynchronous call will be handled via RxJava since Micronaut supports reactive streams.
With their tool Micronaut Launch, it’s very easy to obtain a skeleton for our application. It allows us to choose language, build tools, test framework and extras if needed.
With the framework up and running, we create the controller that will handle the requests:
To manage the telegram protocol, we are going to use one open source library that has already implemented all the requests and responses for the Telegram API . Many of them written in different languages are listed by Telegram.
Implement the Logic of the Bot
For our testing purposes, we are going to use the Open Data offered by the Statistical Institute of Catalonia to get some information about cities, towns and villages of Catalonia. Using their Population search API and The municipality in figures API we will be able to request data about a specific city and show it to the user.
The steps would be that first the user searches for a name with /town XXX and the bot will respond with a list of possible city names with a link next to it. If the user clicks on any link confirming the name of the place, the population information will be shown.
So far, we already need to handle 2 possible requests. Let’s write them.
1. Get the list of cities
The /town XX command will cause a request to the external API to get the list of cities. RxHttpClient will create a Flowable that eventually will return the response from the external call. We’ll capture that event because we have subscribed to it (body.subscribe). Doing so, the process won’t wait for its response and won’t block the whole request.
The async response is sent via the Telegram library, which only needs as a configuration the access token that the botFather created for us.
2. Get the information of a given city.
/XXXX where XXX is the base64 coded name will cause a request to the external API to get information for that specific city.
Again we create a flowable and subscribe to it to get the results async and send them back to Telegram.
Bot Is Working
Here we can see a screenshot of the bot, searching information about Barcelona. As a bonus track, any unexisting command will return the string reversed.
That was a fun project to know how Telegram bot with Micronaut works and discover that there are endless possibilities for this kind of bot. The usage of the Micronaut framework suited very well for this specific case, but it can also be a tool to have in mind for other cases, especially when asynchronous processes need to be handled.
Published at DZone with permission of Oriol Sr. See the original article here.
Opinions expressed by DZone contributors are their own.