Category: messenger bot API

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 a 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!

How to build a basic facebook messenger bot using Nodejs ?

To build a facebook bot, you may choose any server side technology like php, nodejs, python, ruby. I am choosing Nodejs, as it is simple and has built in libraries that would ease in building a bot.

To start building a bot, you need to install Nodejs on your computer. You may install nodejs from link below:

https://nodejs.org/en/download/

Let’s start of  with building that would say hello on your input.

step 1:

The front end API is provided by facebook which can be accessed through a facebook page. To accomplish this you’ll have to create a facebook page.

step 2:

In order to have your bot communicate with the page, facebook provides a unique token which is to be included in your code. To generate a token, you will have to create a facebook app here. Once you are done creating the app, click on “Messenger” and select your page from the drop down which generates a random token. Have this token saved for later use in the code. Then click on “Add Products” and select webhooks.

step 3:

Now that we have token ready, lets write the code say file name is server.js. Note that the above generated token needs to be replaced in the below code 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    const http = require('http')
    const Bot = require('messenger-bot')
    var bot = new Bot({
     token: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
     verify: 'VERIFY_TOKEN'
    })
    bot.on('error', (err) => {
     console.log(err.message)
    })
    bot.on('message', (payload, reply) => {
     var text = payload.message.text;
     text = text.toLowerCase();
     text = text.replace(/[.,/#!$%^&*;:{}=-_`~()?'"@|+<>[]]/g,"");
     text = text.replace(/s{2,}/g," ");
     console.log(text);
     if(typeof text === "undefined" || text == "")
     {
     text = "Oops! we couldn't find this word in our database. Try other.";
     console.log("Sorry! we couldn't find this word in our database. Try other.");
     }
    else if(text == "hi" || text == "hello"){
     text= "Hi. I am hello bot.";
     reply({ text }, (err, info) => {
     });
    else if(text == "bye"){
     text= "Bye. See ya!.";
     reply({ text }, (err, info) => {
     });
     else if(text == "How are you?"){
     text= "Am fine thanks.";
     reply({ text }, (err, info) => {
     });
     bot.getProfile(payload.sender.id, (err, profile) => {
      if (err) throw err
      reply({ text }, (err) => {
       if (err) throw err
       console.log(`Echoed back to ${profile.first_name} ${profile.last_name}: ${text}`)
      })
     })
    });
    http.createServer(bot.middleware()).listen(3000);
    console.log("chat bot up and running..");
step 4:

Now that we are done with the code, its time to run our bot. To run the server, type node server.js.

step 5:

Since, the facebook API communicates through secure tunnel, we need a secure server to establish the connection between facebook and our node server. Ngrok comes to the rescue in this case. Ngrok provides a secure tunnel which allows our server to connect to facebook API.  Install and run Ngrok which generates a secure link. Copy the generated secure link(looks like https://localhost:XXX).

step 6:

In the facebook Developers page, click on “Webhooks” and paste the above secure link in callback url field and enter the verify token from code in “Verify Token” section, click “Verify and save”. To activate the tunnel, click on “Messenger”  and select your page and click on “Subscribe” to subscribe it.

step 7:

Finally, we are done with setup and our bot is ready to talk. In the created facebook page, click on Message and type “Hi” and boom you’ll get the response.

This is a basic chat bot which simply sends text message. Similarly, you can use different templates available in Messenger platform to make your own bot.