Skip to content

Instantly share code, notes, and snippets.

@apkelly
Last active April 21, 2017 14:54
Show Gist options
  • Save apkelly/037e96defab192e3e2fa7a04e8626c5e to your computer and use it in GitHub Desktop.
Save apkelly/037e96defab192e3e2fa7a04e8626c5e to your computer and use it in GitHub Desktop.
Cloud Function for ChicagoRoboto talk.
'use strict';
process.env.DEBUG = 'actions-on-google:*';
const Assistant = require('actions-on-google').ApiAiAssistant;
exports.myAction = (req, res) => {
const assistant = new Assistant({request: req, response: res});
console.log('Request headers: ' + JSON.stringify(req.headers));
console.log('Request body: ' + JSON.stringify(req.body));
// Fulfill action business logic
function responseHandler (assistant) {
// Complete your fulfillment logic and send a response
assistant.ask('Im sorry to hear that your ' + assistant.getArgument('body-parts') + ' hurts. Does anything else hurt?', ['I didnt hear you', 'What else is causing you pain', 'I guess this concludes our consultation']);
}
const actionMap = new Map();
actionMap.set('describe-symptoms', responseHandler);
assistant.handleRequest(actionMap);
};
@apkelly
Copy link
Author

apkelly commented Apr 21, 2017

In the javascript code above, the mapping on line 18 is where we map the intent name as setup in api.ai to the response handler.

The responseHandler can query the parameters and context from the assistant class, as you can see on line 14.

As mentioned in my talk at the end, you respond back to api.ai by calling assistant.ask (if you want to leave the mic open), or assistant.tell (if you want to close the mic and the conversation). The extra parameters you see at the end of the ask method call are some prompts that Google Assistant will use if the user doesn't respond in a timely fashion.

@apkelly
Copy link
Author

apkelly commented Apr 21, 2017

Learn more about the ApiAiAssistant class here https://developers.google.com/actions/reference/ApiAiAssistant

The webhook request/response format is described here https://developers.google.com/actions/reference/webhook-format

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment