Category: echo bot

Developing a basic responsive Telegram bot that echoes your input

Well it’s not quite easy for a beginner to develop telegram bot in a short time by exploring the telegram bot API. I’ll just brief you the procedure by developing a basic telegram bot and I’m sure this tutorial would help you kick start with developing a telegram bot of your own with in a short time.

Once you get familiar with basic bot, you could explore the official telegram bot API for customizing your bot.

Getting Started:

Let’s get started with developing a basic telegram bot.

1. Firstly, you would need to create or register your bot with Telegram. A Telegram’s bot called “BotFather” will help you do this.

Login into telegram either through web or app and search for “BotFather” in search box.

2. Select BotFather and you’ll find options to create a new bot in botfather chat.

Bot’s Name:

3.  Click on “/newbot” option and enter your bot’s name. I am naming my bot as “echo bot”.

Token:

4. Once you are done with creating your bot, you’ll be issued a token. Save this token for later use.

5. Now your bot is ready to show up on telegram, you may search your bot using it’s user name in search box.

6. Now that the bot is ready, let’s add some life to it. For this, we’ll have to develop a server for our bot to help it handle the actions(echo user input in our case). To get our server communicated with this bot, we use the token(in step 3) issued by BotFather.

Script:

7. Let’s develop a script which upon sending an input from our bot echoes it back to bot. I am using nodejs as server side language, it can also be developed in python. Since, node has pretty good number of modules that would make it easy for developing server-side, I’m using it. Here, I’m using a node module called node-telegram-bot-api which has pre-defined packages required for telegram bot API.

Server code for bot with filename echobot.js:

const http = require('http');

const https = require('https');

var TelegramBot = require('node-telegram-bot-api');

var token = 'XXXX'; //Token issued by BotFather

var bot = new TelegramBot(token, {

    polling: true

});


//getMe() returns the user name of the bot


bot.getMe().then(function(me) {

    console.log('Hi my name is %s!', me.username); //prints on console  when you run the server.

});


//on entering any text, we're converting the character case to lower


bot.onText(/.*/, function(msg, match) {

    var text = msg.text;

    text = text.toLowerCase();


    //remove special characters in the input


    text = text.replace(/[.,/#!$%^&*;:{}=-_`~()?'"@|+<>[]]/g, "");

    text = text.replace(/s{2,}/g, " ");


    //just to add AI, on input hi/hello, it prints this message


    var fromId = msg.from.id; // get the id, of who is sending the message

    if (text == "hi" || text == "hello") {

        //  console.log("hi d d gdg d"+msg.text);

        var from = msg.from.first_name; // get the id, of who is sending the message

        var message = "Hi " + from + " Welcome to your eng-hin Botn"

        message += "Type any english word and get the respective hindi meaning.";

        bot.sendMessage(fromId, message);

    } else {

        message = text;

        bot.sendMessage(fromId, message); //echoes the message

    }


});

});

8. Now that our server is ready, let’s run the server using the following command:

$ node echobot.js

9. Try the echo bot in telegram by sending the messages.

That’s it, your bot is ready to talk to you now 🙂 Hope you are aware of customizing your own telegram bot now.

Happy coding 😉