Saturday, 21 October 2017

Where do I learn to code bots?

If you are well versed with API and has some hands-on experience with server-side programming ilke nodejs or php or python , then you could refer Messenger platform guide to build messenger bots. If you are newbie to programming, then using a bot engines would be a better option. Once you are familiar with messenger bot, you will be able to build bots on other platforms like telegram, slack etc.

I have listed few bot engines which makes it easy for you to build a bot.

wit.ai

gupshup bot builder

Chatfuel

Flowxo

API.ai

Botsify

Smooch

Botkit

I have built a few bots in messenger. Let me share them if that might help.

e Bhasha Dictionary bot

This is an English-Hindi dictionary bot. Enter any English word and it gives you the corresponding Hindi meaning.

e Bhasha News bot

This bot gives you the daily news updates, you can also subscribe to daily news alerts. This is available in 4 Indian languages(Hindi, Urdu, Punjabi, Telugu).

e Bhasha Quiz bot

This is a quiz bot that shoots general knowledge questions and is available in 3 Indian Languages(English, Telugu, Hindi ).

e Bhasha Translation bot

This is a translation bot that helps you translate any words or sentences in regional languages. Currently, this bot supports Hindi-Punjabi, Hindi-Urdu, English-Hindi language pairs.

We look forward to building many more bots as such for regional language support to help people eliminate the language barrier.

Happy Learning!

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.

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.



3.  Click on "/newbot" option and enter your bot's name. I am naming my bot as "echo bot".



 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.

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 Bot\n"
        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 ;)