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 as we will use Github to deploy our code.
In command line change current directory to folder we created above and write
git init
git add .
git commit -m”initial”
git remote add origin <URL for remote repository>
git remote -v
git push -u origin masterYou will get URL for the remote repository by making a repository on your Github and copying this link of your repository.
- Now we have to deploy this Github repository to Heroku to get URL.
- If you don’t have an account on Heroku sign up here https://www.heroku.com/ else just sign in and create a new app.
- Deploy your repository onto Heroku from deploy option and choosing Github as a deployment method.
- Select automatic deployment so that when you make any changes in the Github repository, they are automatically deployed to heroku.
You have successfully made SUSI Hangout bot. Try massaging it from your other account on https://hangouts.google.com
Resources
Hangouts bot in python: https://github.com/hangoutsbot/hangoutsbot