Showing Url, Location and Carousel Responses in Viber Bot

SUSI Webchat is capable of sending Url, location and carousel responses. In this blog, we will learn about implementing these same responses in SUSI viber bot. We can send text response instead of carousels but it doesn’t attract users and cause difficulties in understanding. In SUSI webchat for query “news” it sends a URL of an article from BBC news which looks like this: If we send a query “Where is Singapore” it sends location using map response and it looks like this: For web search SUSI sends carousels which look like this: To implement these responses in SUSI Viber bot we will send a post request with specific types to the send_message endpoint of Viber chat API https://chatapi.viber.com/pa/send_message. Url, location and carousel responses will be implemented in following way: Url: For receiving URL in Viber bot we will post a request with type:'url' and media:'url to be sent here'. A request will look like code given below. var options = {                 method: 'POST',                 url: 'https://chatapi.viber.com/pa/send_message',                 headers: headerBody,                 body: {                     receiver: req.body.sender.id,                     min_api_version: 1,                     sender: {                         name: 'Name of your public Account here',                         avatar: 'Avatar url for your public account here'                     },                     tracking_data: 'tracking data',                     type: 'url',                     Media: 'http://www.website.com/go_here'                 },                 json: true             }; request(options, function(err, res, body) {                    if (err) throw new Error(err);                    console.log(body);                }); Location: For receiving location with a map on Viber bot we will post a request with type: 'location' and location: { lat: latitude, lon: longitude }. In location response, we have to send latitude and longitude values of location to be shown on the map. A request will look like code given below: var options = {                 method: 'POST',                 url: 'https://chatapi.viber.com/pa/send_message',                 headers: headerBody,                 body: {                     receiver: req.body.sender.id,                     min_api_version: 1,                     sender: {                         name: 'Name of your public Account here',                         avatar: 'Avatar url for your public account here'                     },                     tracking_data: 'tracking data',                     type: 'location',                     location: {                            lat: latitude,                            lon: longitude                        }                 },                 json: true             }; request(options, function(err, res, body) {                    if (err) throw new Error(err);                    console.log(body);                }); Location response in Viber bot will look like this: Carousel: Carousels are horizontal tiles which can be scrolled to view content on them. To receive carousels on Viber bot we will post a request with type: 'rich_media' and rich_media: {                    Type: "rich_media",                    ButtonsGroupColumns: 6,                    ButtonsGroupRows: 6,                    BgColor: "#FFFFFF",                    Buttons: buttons        } In rich media “ButtonsGroupColumns” is the number of columns per carousel content block,  “ButtonsGroupRows” is the number of rows per carousel content block and buttons is an array of buttons which contains content to be shown in carousels. A request will look like code given below. var options = {                 method: 'POST',                 url: 'https://chatapi.viber.com/pa/send_message',                 headers: headerBody,                 body: {                     receiver: req.body.sender.id,                     min_api_version: 1,                     sender: {                         name: 'Name of your public Account here',                         avatar: 'Avatar url for your public account here'                     },                     tracking_data: 'tracking data',                     type: 'rich_media',                        rich_media: {                            Type: "rich_media",                            ButtonsGroupColumns: 6,                            ButtonsGroupRows: 6,                            BgColor: "#FFFFFF",                            Buttons:{                                Columns: 6,                                Rows: 3,                                Text: 'text to be shown in carousel',                                "ActionType": "open-url",…

Continue ReadingShowing Url, Location and Carousel Responses in Viber Bot

How to Make SUSI Bot for Google Hangouts

Google Hangouts is a platform for communication provided by Google. We can also make bots for Google Hangouts. In fact, in this blog, we will make SUSI bot for Google Hangouts. In order to make it we will follow these steps: Install Node.js from the link below on your computer if you haven’t installed it already. https://nodejs.org/en/ Create a folder with any name and open a shell and change your current directory to the new folder you created. Type npm init in the shell and enter details like name, version and entry point. Create a file with the same name that you wrote in entry point in an above-given step. i.app.js and it should be in the same folder you created. Type following commands in command line  npm install --save hangouts-bot. After hangouts-bot is installed, type npm install --save express.  On success, type npm install --save request. When all the modules are installed check your package.json file; therein you’ll see the modules in the dependencies portion. Your package.json file should look like this. {  "name": "susi_hangout",  "version": "1.0.0",  "description": "SUSI AI bot for Google Hangouts",  "main": "app.js",  "scripts": {    "start": "node app.js"  },  "author": "",  "license": "",  "dependencies": {    "express": "^4.15.3",    "hangouts-bot": "^0.2.1",    "request": "^2.81.0"  } } Copy following code into file you created i.e index.js var hangoutsBot = require("hangouts-bot"); var bot = new hangoutsBot("your-email-here", process.env.password|| config.password); var request = require('request'); var express = require('express'); var app = express(); app.set('port', 8080); bot.on('online', function() {    console.log('online'); }); bot.on('message', function(from, message) {    console.log(from + ">> " + message);        var options1 = {        method: 'GET',        url: 'http://api.susi.ai/susi/chat.json',        qs: {            timezoneOffset: '-330',            q: message        }    };    request(options1, function(error, response, body) {            if (error) throw new Error(error);            // answer fetched from susi            var type = (JSON.parse(body)).answers[0].actions;            var answer, columns, data, key, i, count;            if (type.length == 1 && type[0].type == "answer") {                answer = (JSON.parse(body)).answers[0].actions[0].expression;                bot.sendMessage(from, answer);            } else if (type.length == 1 && type[0].type == "table") {                data = JSON.parse(body).answers[0].data;                columns = type[0].columns;                key = Object.keys(columns);                count = JSON.parse(body).answers[0].metadata.count;                console.log(key);                for (i = 0; i < count; i++) {                    answer = "";                    answer =key[0].toUpperCase() + ": " + data[i][key[0]] + "\n" + key[1].toUpperCase() + ": " + data[i][key[1]] + "\n" + key[2].toUpperCase() + ": " + data[i][key[2]] + "\n\n";                    bot.sendMessage(from, answer);                }            } else if (type.length == 2 && type[1].type == "rss"){              data = (JSON.parse(body)).answers[0].data;              columns = type[1];              key = Object.keys(columns);              for(i = 0; i< 4; i++){                  if(i == 0){                      answer = (JSON.parse(body)).answers[0].actions[0].expression;                      bot.sendMessage(from, answer);                  } else {                      answer = "";                      answer = key[1].toUpperCase() + ": " + data[i][key[1]] + "\n" + key[2].toUpperCase() + ": " + data[i][key[2]] + "\n" + key[3].toUpperCase() + ": " + data[i][key[3]] + "\n\n";                      bot.sendMessage(from, answer);                  }              }            } else {                answer = "Oops looks like SUSI is taking break try to contact later.";                bot.sendMessage(from, answer);            }    }); }); app.listen(app.get('port')); Enter your email in above code.Now, we have to deploy the above-written code onto Heroku, but first, we will make a Github repository and push the files to Github…

Continue ReadingHow to Make SUSI Bot for Google Hangouts

How to integrate SUSI AI in Google Assistant

Google Assistant is a personal assistant by Google. Google Assistant can interact with other applications. In fact, we can integrate our own applications with Google Assistant using Actions on Google. In order to integrate SUSI action with Google Assistant we will follow these steps: Install Node.js from the link below on your computer if you haven’t installed it already. https://nodejs.org/en/ Create a folder with any name and open a shell and change your current directory to the new folder you created.  Type npm init in the shell and enter details like name, version and entry point.  Create a file with the same name that you wrote in entry point in above-given step. i.e index.js and it should be in the same folder you created.  Type following commands in command line  npm install --save actions-on-google. After actions-on-google is installed, type npm install --save express.  On success, type npm install --save request, and at last type npm install --save body-parser. When all the modules are installed check your package.json file; therein you’ll see the modules in the dependencies portion. Your package.json file should look like this {  "name": "susi_google",  "version": "1.0.0",  "description": "susi on google",  "main": "app.js",  "scripts": {    "start": "node app.js"  },  "author": "",  "license": "ISC",  "dependencies": {    "actions-on-google": "^1.1.1",    "body-parser": "^1.17.2",    "express": "^4.15.3",    "request": "^2.81.0"  } } Copy following code into file you created i.e index.js // Enable debugging process.env.DEBUG = 'actions-on-google:*'; var Assistant = require('actions-on-google').ApiAiApp; var express = require('express'); var bodyParser = require('body-parser'); //defining app and port var app = express(); app.set('port', (process.env.PORT || 8080)); app.use(bodyParser.json()); const SUSI_INTENT = 'name-of-action-you defined-in-your-intent'; app.post('/webhook', function (request, response) {  //console.log('headers: ' + JSON.stringify(request.headers));  //console.log('body: ' + JSON.stringify(request.body));  const assistant = new Assistant({request: request, response: response});  function responseHandler (app) {    // intent contains the name of the intent you defined in the Actions area of API.AI    let intent = assistant.getIntent();    switch (intent) {      case SUSI_INTENT:        var query = assistant.getRawInput(SUSI_INTENT);        console.log(query);        var options = {        method: 'GET',        url: 'http://api.asksusi.com/susi/chat.json',        qs: {            timezoneOffset: '-330',            q: query          }        };        var request = require('request');        request(options, function(error, response, body) {        if (error) throw new Error(error);        var ans = (JSON.parse(body)).answers[0].actions[0].expression;        assistant.ask(ans);        });        break;    }  }  assistant.handleRequest(responseHandler); }); // For starting the server var server = app.listen(app.get('port'), function () {  console.log('App listening on port %s', server.address().port); }); Now we will make a project on developer’s console of Actions on Google and after that, you will set up action on your project using API.AI Above step will open API.AI console create an agent there for the project you created above.  Now we have an agent. In order to create SUSI action on Google, we will define an “intent” from options in the left menu on API.AI, but as we have to receive responses from SUSI API so we have to set up webhook first.   See “fulfillment” option in the left menu. Open to enable it and to enter the URL. We have to deploy the above-written code onto Heroku, but first, make a Github repository and push the files in…

Continue ReadingHow to integrate SUSI AI in Google Assistant