Develop a Telegram Bot: from A to Z for beginners

Introduction

Browsing the net I found lots of websites explaining how to write a Telegram bot….but everything around it? How to deploy it? What to do after the code?

First of all you have to know that to create a Telegram bot you have to know at least a programming language that could be PHP  or Python or Java etc…

In this case we will use Java

The second thing to know is that a bot needs to run somewhere in the net (obviously).

In our case the bot will run on Heroku, a “hobby free” cloud application platform, a server where our bot can run and stay active (microservice platform like hook.io).

At the end, we are going to use Gradle,a build tool that gives us the possibility to deploy everywhere our bot…in particular on Heroku.

Writing first bot

Now open your IDE (personally I use IntelliJ IDEA) and let’s code.

As we can find  on every website, we will create a simple “Echo bot”.

An Echo bot is like this cool blue parrot: if we write “Hello world!” it will answer “Hello world!”.

src/Main.java
 import org.telegram.telegrambots.ApiContextInitializer;
 import org.telegram.telegrambots.TelegramBotsApi;
 import org.telegram.telegrambots.exceptions.TelegramApiException;
  public class Main {
    public static void main(String[] args) {
        // Initialize Api Context
        ApiContextInitializer.init();

        // Instantiate Telegram Bots API
        TelegramBotsApi botsApi = new TelegramBotsApi();

        // Register our bot
        try {
            botsApi.registerBot(new MyAmazingBot());
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }
}
 
src/MyAmazingBot.java
 import org.telegram.telegrambots.api.methods.send.SendMessage;
 import org.telegram.telegrambots.api.objects.Update;
 import org.telegram.telegrambots.bots.TelegramLongPollingBot;
 import org.telegram.telegrambots.exceptions.TelegramApiException;

 public class MyAmazingBot extends TelegramLongPollingBot {
    @Override
    public void onUpdateReceived(Update update) {

        // We check if the update has a message and the message has text
        if (update.hasMessage() && update.getMessage().hasText()) {
            // Set variables
            String message_text = update.getMessage().getText();
            long chat_id = update.getMessage().getChatId();

            SendMessage message = new SendMessage() // Create a message object object
                .setChatId(chat_id)
                .setText(message_text);
            try {
                execute(message); // Sending our message object to user
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public String getBotUsername() {
        // Return bot username
        // If bot username is @MyAmazingBot, it must return 'MyAmazingBot'
        return "MyAmazingBot";
    }

    @Override
    public String getBotToken() {
        // Return bot token from BotFather
        return "12345:qwertyuiopASDGFHKMK";
    }
}

Gradle

Now, I don’t want to explain what is an automation build tool ( may Wikipedia be with you ). Just understand that Gradle allows you to develop the bot (and software in general) without worrying about dependecies (libraries etc…) and making it runnable on every platform.

Gradle uses “tasks”, that are something like boxes containing instructions on how to build our bot.

For the next step, remember that the task “stage” is the most important to run our bot on Heroku (we will see it later).

 

Heroku

Our best friend is Heroku that is a platform where we’re going to launch our bot.

First of all let’s create a new account from here and then login.

Heroku has a beautiful Command Line Interface that will simplify our life, so let’s install it. You can follow this guide if you want…or if you are Debian based users you can use this commands:

$ sudo snap install heroku --classic

 

Useful links

https://devcenter.heroku.com/articles/heroku-cli

https://devcenter.heroku.com/articles/deploying-gradle-apps-on-heroku

Be the first to comment

Leave a Reply

Your email address will not be published.


*