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…
