/telegram-bot-template

A template for creating a telegram bot. Spring boot, AWS, Long Polling Bot

Primary LanguageJava

Telegram bot template

  • Allows you to create a multilingual bot
  • Easy addition of new commands and their processing
  • Easy addition of inline buttons and callback processing
  • Send/delete/edit messages from the bot
  • Adding a new language to enum rey.bos.telegram_bot_template.shared.dto.LanguageCode, LanguageCode.value - language codes that can be found here
  • Adding a new language to the mapping rey.bos.telegram_bot_template.shared.mapper.UserDtoMapper, method LanguageCode map(String code)
  • In the rey.bos.telegram_bot_template.dictionary.impl package, we add a new implementation of the interface rey.bos.telegram_bot_template.dictionary.Dictionary, in which you will need to enable support for the new language in the Dictionary.isSuitable method and translate all the keys rey.bos.telegram_bot_template.dictionary.DictionaryKey in the Dictionary.get method
  • Add a new value to the enum rey.bos.telegram_bot_template.bot.handler.impl.command.MenuCommand, if you want the command to be shown in the menu, then add it to the MenuCommand.getCommandsForMenu method
  • Add a BotHandler implementation to the rey.bos.telegram_bot_template.bot.handler.impl.command package, which will process this command

When sending a message to a user, you can add buttons to it, when clicked you can perform some action. For example, changing the language.

When you click on any button, a callback event will be sent to the server, with the data that you previously added to it.
  • Add a new value for callback to the enum rey.bos.telegram_bot_template.bot.handler.impl.callback.CallBackCommand, which can be added to the button and made a handler for the click event
  • When sending a message to a user, you can add buttons to it and data with the created callback, using the MessageUtil.buildSendMessageWithButtons method
  • In the package rey.bos.telegram_bot_template.bot.handler.impl.callback we add an implementation of the BotHandler interface to handle the added callback event

How to do this can be found on the Internet. Here is a link to the telegram documentation. The token that you receive when creating the bot will later be used when interacting with the telegram api

Your bot can be launched locally for debugging, without deployment to the server.

  • Download the project to your local computer git clone git@github.com:reybos/telegram-bot-template.git
  • Go to the folder with the project cd telegram-bot-template
  • In the project properties you need to specify the token of your bot received when registering in telegram. telegram.token in src/main/resources/application.properties
  • Run the commands mvn clean install and mvn spring-boot:run

This template is ready to work and deploy to AWS, I had a number of problems with saving application logs, but now it works.

Create an AWS account

During the first year after registration, free resources for testing ideas are available, which I used.

Assemble the jar archive with the program

Run the command in the root of the application >mvn clean install

As a result, a file will be created target/telegram_bot_template-0.0.1.jar

Now you can upload it to the server

Deployment to AWS

I use Elastic Beanstalk for deployment. Go to this service in your chosen region, I have it us-east-1

Let's create a new application. I will name my telegram-bot-template

Now let's create a new environment

In the environment creation form, I will change only some fields, leave the rest unchanged and will not write about them.

I chose the language and version

Select the jar archive to download

And finally, in the fifth step, you need to set the environment variable with the bot token.

Now everything is ready, you can deploy the bot. It will take a few minutes, once everything is ready you will see a green bar

Now you can check the work of the bot. Find your bot in telegram and click the "start" button or send him any message