Integrate other services with fulfillment

This page describes how to connect your Dialogflow agent to your services with fulfillment. Fulfillment allows you to surface data from your own website or service in your Dialogflow agent.

Setup: Deploy and enable fulfillment

To enable fulfillment:

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

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

  3. 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 on "Default Welcome Intent".

  2. In the Training Phrases section, add the following phrase:

    • hi
  3. Scroll down to the Fulfillment section and click on the switch to enable it.

  4. Click the Save button.

Try it out!

Now enter "Hi" into the Dialogflow simulator on the right. You should see a message, "Welcome to my agent!". This response is coming from the fulfillment you just deployed!

Create a fulfillment handler

Next, we'll need to enable fulfillment for the "name" intent you created in the 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. Navigate back to the fulfillment page by clicking on Fulfillment in the left menu, and take a look at the code.

    The first few lines import the necessary libraries and define constants. You should then see this line:

    exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {

    This is a function that handles HTTP requests from Dialogflow and uses Cloud Functions for Firebase. Inside the function you should see several other functions. These are handler functions that are invoked when the proper intent is matched.

  4. Next, look for intentMap on lines 51-55.

    Now, you'll add an entry to the intent map to call the handler function nameHandler when the "name" intent is triggered.

  5. Add the following code after the line that starts with intentMap.set:

    intentMap.set('Name', nameHandler);

    Now, when Dialogflow matches the "name" intent, the function nameHandler responds to the user's query. Next, you'll write the nameHandler function.

  6. Just above the line with let intentMap = new Map(); add the following code:

    function nameHandler(agent) {
      agent.add('My name is Dialogflow!');
    }
    
  7. Click Deploy at the bottom of the page.

Try it out!

Once it's deployed, 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

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

In the extracting 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 create a languageHandler function in the fulfillment. Use the following code for the languageHandler function:

function languageHandler(agent) {
    const language = agent.parameters.language;
    const programmingLanguage = agent.parameters.ProgrammingLanguages;
    if (language) {
        agent.add("Wow! I didn't know you knew ${language}");
    }
    else if (programmingLanguage) {
        agent.add("${programmingLanguage} is cool");
    } else {
        agent.add("What language do you know?")
    }
}

The code retrieves the parameters language and ProgrammingLanguages and stores them in variables. It does this by accessing the agent.parameters attribute, which is an object that contains all of the parameter names and values users specify in their requests.

When either parameter is sent to your fulfillment from Dialogflow, you construct the same responses as in your 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

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 managing 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 (turns the context is active), and parameters. Modify your languageHandler function to include the context by using the agent.setContext method:

function languageHandler(agent) {
    const language = agent.parameters.language;
    const programmingLanguage = agent.parameters.ProgrammingLanguages;
    if (language) {
        agent.add("Wow! I didn't know you knew ${language}");
        agent.setContext({
              name: 'languages-followup',
              lifespan: 2,
              parameters:{language: language}
            });
    else if (programmingLanguage) {
        agent.add("${programmingLanguage} is cool.");
        agent.setContext({
              name: 'languages-followup',
              lifespan: 2,
              parameters:{ProgrammingLanguage: programmingLanguage}
            });
    } else {
        agent.add("What language do you know?")
    }
}

In this code, you are adding an output context with the name "languages-followup" that has 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 agent.getContext method or get a list of all the contexts with agent.contexts. For instance, if you want to create a fulfillment handler for the "languages - custom" follow-up intent, it would look like this:

function languagesCustomHandler(agent) {
    const context = agent.getContext('languages-followup');
    const allContexts = agent.contexts; // [{ name: 'languages-followup', ...}]

    const language = context.parameters.language || context.parameters.ProgramingLanguages;

    const duration = agent.parameters.duration;
    agent.add(`I can't believe you've know ${language} for ${duration}!`);
}

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: