Integrate Actions on Google with fulfillment

This page describes how to connect your Dialogflow agent to your services with fulfillment via Actions on Google. Fulfillment allows you to surface dynamic data from your databases to your users.

Setup: Deploy and enable fulfillment

To enable fulfillment:

  1. Click on Fulfillment in the left menu. If you don't see the navigation, click the menu menu button in the upper left corner.

  2. Enable the inline editor by clicking the switch next to Inline Editor.

  3. Replace the code in the inline editor with the code in this file.

  4. Click Deploy at the bottom of the page.

You need to enable fulfillment for the "name" intent created in Create your first agent:

  1. Click on Intents in the left menu and then the "name" intent.

  2. Scroll down to the Fulfillment section and click on the switch to enable it.

  3. Click the Save button.

Create a handler

Handlers are functions you can call throughout your code. These handlers are set to run when a specific intent is matched.

  1. Navigate back to the fulfillment page by clicking on Fulfillment in the left menu, and take a look at the code.

    The last line should look like this:

    exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app)

    This is a function that handles HTTP requests from Dialogflow, and it uses Cloud Functions for Firebase.

  2. Add the following code just above the last line that starts with exports.dialogflowFirebaseFulfillment:

    app.intent('Name', conv => {
      conv.ask('My name is Dialogflow!')

When Dialogflow matches the "name" intent, the above code responds to the user's query.

To test it out, click Deploy at the bottom of the page. Once it's done, invoke the "name" intent by entering "What's your name?" into the simulator on the right. You should see your new message as the response!

Use entities and contexts in fulfillment


Entities are used for extracting parameter values from a user's query. Any important data you want to get from a user's request must have a corresponding entity.

In the Extract data with entities guide, you created a "Languages" intent to identify spoken and programming languages. The intent has the parameters language and ProgrammingLanguages.

To access these parameters:

  1. Click on Intents in the left menu and click on the "Languages" intent.

  2. Under the Fulfillment section of the intent, click the switch for the "Enable webhook call for this intent" option.

  3. Click the Save button at the top of the intent page.

  4. Click Fulfillment in the left menu.

    Now, you'll write some code to handle the "language" intent in the fulfillment. Add the following code just above the last line that starts with exports.dialogflowFirebaseFulfillment:

    app.intent('languages', (conv, {language, ProgrammingLanguage}) => { if (language) { conv.ask(Wow! I didn't know you knew ${language}); } else if (ProgrammingLanguage) { conv.ask(${ProgrammingLanguage} is cool.); } else { conv.ask(What language do you know?) } });

In this code, you're retrieving the parameters language and ProgrammingLanguages. When either parameter is sent to your fulfillment from Dialogflow, you construct the same responses as in our fulfillment. When no parameter is sent, you use another response to ask the user what language they know.

To try it out, click Deploy and invoke the "Languages" intent by saying something like "I know how to speak German".


Contexts represent the current context of a user's request. This is helpful for differentiating phrases which may be vague or have different meanings depending on the user’s preferences, the user's geographic location, the current page in an app, or the topic of conversation.

In the Manage state with contexts guide, you created a follow-up intent for the "Languages" intent. As mentioned on that page, follow-up intents work by setting up output contexts from the parent intent and using the same context as input for the child intent.

In order to properly mimic the same behavior in your fulfillment, you need to set the same output context as Dialogflow with the same lifetime (conversation turns during which the context is active), and parameters. Modify your handler to include the context by using the conv.contexts.set method:

app.intent('Languages', (conv, {language, ProgrammingLanguage}) => {
  if (language) {
    conv.ask(`Wow! I didn't know you knew ${language}`);
    conv.contexts.set('Languages-followup', 2, {language: language})
  } else if (ProgrammingLanguage) {
    conv.ask(`${ProgrammingLanguage} is cool.`);
    conv.contexts.set('Languages-followup', 2, {ProgrammingLanguage: ProgrammingLanguage})
  } else {
    conv.ask(`What language do you know?`)

In this code, you are adding an output context with the name "languages-followup", a lifespan of 2 turns, and a parameter value (language or ProgrammingLanguages) in the context for later retrieval. Click Deploy to try it out.

To retrieve the contexts in fulfillment, you can use the conv.contexts.get method or get a list of all the contexts with app.getContexts. For instance, if you want to create a fulfillment handler for the "Languages - custom" follow-up intent, it would look like this:

app.intent('Languages - custom', (conv, {duration}) => {
  const langContext = conv.contexts.get('languages-followup')
  const language = langContext.parameters.language || langContext.parameters.ProgrammingLanguage;
  conv.ask(`I can't believe you've know ${language} for ${duration.amount} ${duration.unit}!`);

Next steps

If you have any questions or thoughts, let us know on the Dialogflow Google Plus Community. We'd love to hear from you!

You might also want to check out: