JavaRush /Java Blog /Random EN /Telegram Ability Bot: a bot that can conduct a dialogue
Chundrik
Level 35
Санкт-Петербург

Telegram Ability Bot: a bot that can conduct a dialogue

Published in the Random EN group
Bots are not only interesting, but also useful. While working on them, you will be able to get a simple but working project for automating any functions and, along the way, get acquainted with lambdas, Git and Heroku. When I made the bot, I didn't find instructions on how to make anything even a little more complex than a simple echo bot. I wanted something more reasonable: for example, a channel that could conduct a real dialogue, the development of which would change depending on the chosen answer. I will talk about my project, which took me two weeks, and I will pay special attention to deployment on Heroku, since I encountered great difficulties at the final stage. Telegram Ability Bot: a bot that can conduct a dialogue - 1

Registration

To get started, you need to register the bot, receiving a name and a token, which we will need later. Fortunately, this is not difficult: just open Telegram, find @BotFather and enter /start. In response, you will receive a list of commands - we need /newbot. We come up with a name, and then a username, which should end in -bot, for example, Consequences1Bot. The username must be original, so you will have to use your imagination here. In response, BotFather will send a token, which is best saved immediately.

Start

Telegram Ability Bot: a bot that can conduct a dialogue - 2To get started, we will need a Maven project, Java 8 and Intelliji Idea Ultimate. Insert pom.xml: Telegram Ability Bot: a bot that can conduct a dialogue - 3Don't forget to click the Load Maven changes button, which will appear in the upper right corner. Also make sure you are using Java 8 or higher, this can be found in File --> Project settings --> Project. In the folder src.main.java, create a subfolder with the name of your bot, for me it’s a package example.TelegramBot. First we create a class TelegramBotthat inherits AbilityBot. If Idea underlines it in red (and it does), click Import class. AbilityBot has one method that needs to be implemented - creatorId().
@Override
    public int creatorId() {
        return Constants.CREATOR_ID;
    }
We also need to create two constructors: one without arguments, and using the second we call the superclass constructor:
public TelegramBot() {
        this(Constants.BOT_TOKEN, Constants.BOT_USERNAME);
    }
    private TelegramBot(String botToken, String botUsername) {
        super(botToken, botUsername);
    }
Constansnaturally turns red - this interface has to be created. It will store the strings and the ID number. Of course, you don’t have to bother and leave everything in the main document, but it will be much clearer this way. So, let's create an interface Constans. The first variables will be String BOT_TOKEN(the token that BotFather sent), String BOT_USERNAMEand int CREATOR_ID. The creator ID is needed for additional security - you can find it a little later.

Launch

Create a class Applicationthat will contain a method main()with the code necessary to initialize the bot.Telegram Ability Bot: a bot that can conduct a dialogue - 4
public class Application {

    public static void main(String[] args) {
        // Initializes dependencies necessary for the base bot
        ApiContextInitializer.init();

        // Create the TelegramBotsApi object to register your bots
        TelegramBotsApi botsApi = new TelegramBotsApi();

        try {
            // Register your newly created AbilityBot
            FitnessBot bot = new FitnessBot();
            botsApi.registerBot(bot);

        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }
}
Now you can launch the bot from the main method, but it will not perform any actions. Only silence will be your answer. In the next part I will describe how to change this. PART 2
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION