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 the folder we created above.
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.
- Open your app from an option on the top right and copy the link of your Heroku app and append it with /webhook and enter this URL into fulfillment URL.
https://{Your_App_Name}.herokuapp.com/webhook - After setting up webhook we will make an intent on API.AI, which will get what user is asking from SUSI. To create an intent, select “intent” from the left menu and create an intent with the information given in screenshot below and save your intent.
- After creating the intent, your agent is ready. You just have to integrate your agent with Actions on Google. Turn on its integration from the “integration” option in the left menu.
- Your SUSI action on Google Assistant is ready now. To test it click on actions on Google in integration menu and choose “test” option.
- You can only test it with the same email you have used for creating the project in step 7. To test it on Google Assistant follow this demo video https://youtu.be/wHwsZOCKaYY
If you want to learn more about action on Google with API.AI refer to this link https://developers.google.com/actions/apiai/
Resources
Actions on Google: https://developers.google.com/actions/apiai/