Build Fulfillment

Fulfillment defines the conversational interface for your app to obtain user input and the logic to process the input and eventually fulfill the action.

Overview

Your fulfillment receives requests from the Assistant, processes the request and responds. This back-and-forth request and response process drives the conversation forward until you eventually fulfill the initial user request.

The following steps describe how to build fulfillment with the Node.js client library:

  1. Initialize the ActionsSdkApp object.
  2. Create functions to handle requests in your fulfillment logic.

Building dialogs

Initialize the ActionsSdkApp object

The following code instantiates ActionsSdkApp and does some boilerplate Node.js setup for Google Cloud Functions:

'use strict';

const {actionssdk} = require('actions-on-google');
const functions = require('firebase-functions');

const app = actionssdk({debug: true});

app.intent('actions.intent.MAIN', (conv) => {
  conv.ask('Hi!');
});

// More intent handling if needed

exports.myFunction = functions.https.onRequest(app);

Create functions to handle requests

When users speak a phrase, you receive a request from the Google Assistant. To fulfill the intents that come in requests, create functions that handle the triggered intent.

To handle requests:

  1. Carry out any logic required to process the user input.

  2. Call the Conversation.ask() function passing the response you want to show as an argument.

The following code shows how to build a simple response:

conv.ask(`Hi! Say something, and I'll repeat it.`);

Handling intents

Once you have all your functions to handle triggered intents, use app.intent to assign handlers to intents.

app.intent('actions.intent.TEXT', handleTextIntent);
app.intent('actions.intent.MAIN', handleMaiIntent);

Ending conversations

When you no longer want any user input in return and want to end the conversation, call the Conversation.close() function. This function tells the Google Assistant to speak back the text to the user and end the conversation by closing the microphone.

Handling the main invocation intent

When users trigger the app.intent.action.MAIN intent, you normally don't need to do any user input processing. If your action package contains many actions and covers many use cases, it's a good idea to orient the user by telling them a few things that they can do.

  1. Call the Conversation.ask() function passing your response as an argument. The Google Assistant speaks your response to the user and then waits for the user to trigger one of the intents that you specified.

The following snippet shows how to handle a simple welcome intent:

// handle the initialTrigger
function handleMainIntent(conv, input) {
  conv.ask(`Hi, I'm Do It All. You can ask to do this or that. What would you like to do?`);
}

Conversation state

You can maintain state of your conversations with a JSON formatted object that is passed back and forth between you and the Google Assistant. You can write to and read from the app.data field directly. This field is passed back and forth automatically between requests and responses.

conv.data = {something: 10};
let value = conv.data.something;