Develop a Telegram Bot: from A to Z for beginners


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

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!”.

 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

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

        // Register our bot
        try {
            botsApi.registerBot(new MyAmazingBot());
        } catch (TelegramApiException e) {
 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 {
    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
            try {
                execute(message); // Sending our message object to user
            } catch (TelegramApiException e) {

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

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


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).



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

Be the first to comment

Leave a Reply

Your email address will not be published.