Implementing Rich Responses in SUSI Action on Google Assistant

Google Assistant is a personal assistant. This tutorial will tell you how to make a SUSI action on Google, which can only respond to text messages. However, we also need rich responses such as table and rss responses from SUSI API. We can implement following types of rich responses: List Carousel Suggestion Chip List: A list will give the user multiple items arranged vertically. In SUSI webchat table type responses are handled with a list and it looks like this: Now in order to show to list from SUSI Action on Google, we will use first key (i.e Recipe Name in above screenshot) as a title for each item in the list and rest of keys as a description for that item. For building we will use the following code: assistant.askWithList(assistant.buildRichResponse()    .addSimpleResponse('Any text you want to send before list'),    // Build a list    assistant.buildList(‘Title of the list here’)    // First item in list    .addItems(list[1])    // Second item in list    .addItems(list[2])    // Third item in carousel    .addItems(list[3]) ); In .addItems we will add the object after populating it from data that we will get from SUSI API and we will populate it with following code: for (var i = 0; i < metadata.count; i++) {    list[i] = assistant.buildOptionItem('ID for item', ['keyword','keyword','keyword'])        .setTitle('title of your item in the list here')        .setDescription('description of item in list') } A list should have at least two items in it. List title and descriptions are optional and title of the item in the list is compulsory. We can also set image for list items in following way. .setImage('link for the image here', 'Image alternate text') Setting image in the list is optional and image size will be 48x48 pixels. Carousel: Carousel messages are cards that we can scroll horizontally. In SUSI webchat RSS type responses are handled with the list and it looks like this: In order to show carousel from SUSI Action on Google we will use the following code: assistant.askWithCarousel(assistant.buildRichResponse()    .addSimpleResponse('Any text you want to send before carousel'),    // Build a list    assistant.buildCarousel()    // First item in carousel    .addItems(list[1])    // Second item in carousel    .addItems(list[2])    // Third item in carousel    .addItems(list[3]) ); Again we will populate .addItems() just like we did for the list. Carousel should have at least two tiles. Just like in the list, the title is compulsory and description and image are optional. Image size in the carousel is 128dp x 232dp. You can set image same as we have set image in the list above. Suggestion Chip: Suggestion chip is used to add a hint to a response i.e if you want to give the user a list of suggested responses, we can use suggestion chip. To add suggestion chip to any response you just have to add .addSuggestions(['Hint1', 'Hint2', 'Hint3', 'Hint4']) to your response code after .addSimpleResponse()   See how SUSI Action on Google replies to table and RSS type responses in this video https://youtu.be/rjw-Vg4X57g and If you want to learn more about Actions on Google refer to https://developers.google.com/actions/components/…

Continue ReadingImplementing Rich Responses in SUSI Action on Google Assistant

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