How to create a Telegram Chatbot with Kotlin - Part 1

Automation in customer service is no longer a new concept. In the fast-paced world dominated by innovation and technology, customers seek easier ways to communicate with a company, with more than 50% expecting a business to be available 24/7. As a result, new tools like chatbots or virtual agents emerged to simplify the interaction between a customer and a company.

A chatbot is a programmed chat interface a platform user can interact with. Bots are customizable and can automate parts of customer service such as processing customer inquiries, responding to information requests, and collecting customer data. AI-based sophisticated bots can also handle complex communications and create personalized experiences for customers.

Telegram is one of the most popular IM platforms with more than 500 million active monthly users. Chatbots in Telegram offer wide functionality and can be created following a couple of simple steps.


What value can chatbots in Telegram create for your company?

  • Increased cost savings: bots can handle complex inquiries 24/7.

  • Increased customer satisfaction: instant interaction and response.

  • Increased sales: personalized product recommendations.

  • Increased efficiency: no more repetitive tasks handled by personnel.

  • Increased customer engagement: create games, quizzes, and entertainment content.


How to create a Telegram Chatbot

To use Telegram, you will need an account. Visit https://telegram.org/ and register an account.

Once your account is up and running, we can start creating a bot. Find @BotFather. BotFather is a Telegram-provided channel for creating and managing your bots. Using @BotFather you can configure, delete and modify your bot’s metadata such as picture, description, about, commands.


Start building the bot by pressing start or type /start command. You will see the same message as below.

Select the command /newbot or type /newbot in the chat. You will see the following message:

Note: Slash in front of the text (e.g. /newbot) is a command to the bot.

Note: Store token highlighted in red in a secure place. You will use this token to make bot communicate to the back-end.

Great! You just created your first searchable bot in Telegram. Now let's customize it and make it smarter.


How to configure a Telegram bot

Below you will see what can be changed. You can insert any information you need.

Description

Type your description in the display box. Description is the first message you see before starting to communicate with the bot.

About

Create your “about” section. You can also use emojis.

Commands

There are 2 types of commands. First, we select /setcommands. This section is visible to a user using the bot without typing in the right bottom corner as the “Slash” button.

There are few other options which I will not cover in this post, but you can always reach out to me for more details.

/setuserpic, /setname, /setdomain


How to create a Telegram Bot in Kotlin

Now you have your bot configured and API token available. It is the time to create your first bot.

Create a new Maven project or fork using our bot GitHub as a reference project.

We have a Maven project but no code there, yet.

Below is an example of the main class. We need the main function and TelegramBotsApi, to distinguish between sessions, we pass inside DefaultBotSession::class.java and register our bot.

See the code below:

In our case GbBot implements TelegramLongPollingBot interface. Since we want to keep the bot alive as long as possible, we will use indefinite while loop and try-catch blocks as we don’t have time to maintain the bot. There are alternatives to our approach which we will discuss later in the serverless possibilities section.

The main method is onUpdateReceived as it receives messages from the chat and responds to it. In our case, it passes logic further to GbProcessor that processes incoming messages and responds to the best of its ability. Ideally, there should be no logic in GbBot, but we decided to keep it simple and working. GbProcessor is a big chunk of if-else statements that decides how to react to messages.

See implementation below:

It stores chatId to respond to a correct thread and respond or leave “hint” to the bot, on how to respond. See the full implementation on GitHub.


General discussion and requirements for the deployment

As we implement TelegramLongPollingBot our bot should be running 24/7. The natural requirement is to have VPS running constantly. The cheapest we know is 5$/month. Choose the one that corresponds to your requirements.

Serverless possibilities

In this article, we saw bots basic functionality and abilities with deployment on a traditional VPS.

There are alternatives to the current setup. You can implement the bot back-end as Function as a Service. The bot will be a web-server or function that accepts http requests. In this case, we would need to configure the webhook in Telegram, so every message will fire to the function/web service. This solution is more cost efficient but wouldn’t necessarily lead to shorter development time and reduced maintenance cost.

Can I try out the bot?

Yes, search for @gb-demo-bot or follow this link.

Why do we use Kotlin?

Kotlin is an expressive language JVM-based. We use Kotlin in most projects, but we also love Java, Python and JavaScript. The reason we decided to create this bot in Kotlin is mainly due to us feeling there was a lack of materials in this area and many of our own projects are using Kotlin. However, we recognize that most implementations are based on Python and JavaScript.

Author

Dmytro Chasovskyi is a professional software developer with expertise in Back-end development and integrations building. Dmytro admires GCP and AWS clouds and is all about tech and innovation. He has a Master's degree in Software Engineering from Estonia and a Bachelor's degree in Applied Mathematics from Ukraine. Working in IT since 2014, providing consulting, product development and IT services.

At Greenbird, Dmytro is a Back-end & Cloud developer responsible for in-house consultancy and internal development tools.

You can reach out to Dmytro using the following email: dmytro.chasovskyi@greenbird.com

Related stories