Quickstart: Fulfillment

This quickstart shows you how to integrate your agent with your services using fulfillment. Integrating your service allows you to take actions based on end-user expressions and send dynamic responses back to the end-user.

Before you begin

If you don't plan on using fulfillment, you can skip this quickstart.

These steps use Cloud Functions for Firebase and assume basic Node.js knowledge. If you want to create fulfillment using your own server or another language, see the more detailed fulfillment documentation.

You should do the following before reading this guide:

  1. Read Dialogflow basics.
  2. Perform setup steps.
  3. Perform steps in the Build an agent quickstart. Steps below continue working on the same agent.

The Inline Editor

The Dialogflow Console has a built-in code editor, called the Inline Editor, that you can use to create fulfillment code and deploy the code to Cloud Functions for Firebase. The Inline Editor only supports Node.js.

When you initially enable the Inline Editor, the fulfillment code is pre-populated with default handlers for default intents that are included for all agents. The code also has commented instructions for adding handlers for developer-defined intents.

Enable and deploy fulfillment with the Inline Editor

To enable and deploy the default fulfillment code using the Inline Editor:

  1. Click Fulfillment in the left sidebar menu.
  2. Toggle the Inline Editor to Enabled.
  3. Click Deploy at the bottom of the form.

For each intent that requires fulfillment, you must enable fulfillment for the intent. To enable fulfillment for the Default Welcome Intent:

  1. Click Intents in the left sidebar menu.
  2. Click Default Welcome Intent.
  3. In the Training Phrases section, add the following phrase:
    • hi
  4. Scroll down to the Fulfillment section and toggle Enable webhook call for this intent on.

  5. Click Save.

Now you can test your fulfillment in the simulator. Enter Hi in the simulator, and it responds with Welcome to my agent!. This response is sent from the fulfillment webhook you just deployed. In particular, the response came from this code:

function welcome(agent) {
  agent.add(`Welcome to my agent!`);
}

Create a custom fulfillment handler

The steps above use a handler supplied by the default inline editor code. To create a custom handler:

  1. Enable fulfillment for the name intent you created in previous steps.
  2. Click Fulfillment in the left sidebar menu, and examine the code in the inline editor.
  3. Find this line:

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

    The onRequest function handles all requests from Dialogflow. Within the body of this function, specific handler functions are defined. These handler functions are invoked when associated intents are matched. For example, the function used in the previous section is function welcome(agent).

  4. Below the existing handlers, add this function for the name intent:

    function nameHandler(agent) {
      agent.add('My name is Dialogflow!');
    }
    
  5. Below the handler function definitions, there are intentMap.set() calls. These calls associate specific handlers with intents by name. For example, intentMap.set('Default Welcome Intent', welcome) associates the welcome handler with the intent named Default Welcome Intent.

  6. Below the existing intentMap.set calls, add this line for the name intent:

    intentMap.set('name', nameHandler);
    
  7. Click Deploy at the bottom of the form.

  8. Enter What's your name? in the simulator. The "My name is Dialogflow!" response is sent from your new handler.

Access parameter values

In previous steps, you created a Languages intent to identify spoken and programming languages. The intent uses the language and ProgrammingLanguage parameters. In this section, you access values for these parameters in your fulfillment handler.

To add the handler:

  1. Enable fulfillment for the Languages intent.
  2. Click Fulfillment in the left sidebar menu.
  3. Similar to steps above, add the following handler and intentMap call:

    function languageHandler(agent) {
        const language = agent.parameters.language;
        const programmingLanguage = agent.parameters.ProgrammingLanguage;
        if (language) {
            agent.add(`From fulfillment: Wow! I didn't know you knew ${language}`);
        } else if (programmingLanguage) {
            agent.add(`From fulfillment: ${programmingLanguage} is cool`);
        } else {
            agent.add(`From fulfillment: What language do you know?`)
        }
    }
    ...
    intentMap.set('Languages', languageHandler);
    
  4. Click Deploy.

This handler retrieves the parameters language and ProgrammingLanguage and stores them in variables. It retrieves these values from the agent.parameters attribute, which is an object that contains all of the parameter names and values that end-users specify in their requests.

To try it, enter "I know how to speak German" in the simulator.

Set output context

In previous steps, you created a follow-up intent for the Languages intent. In order to properly mimic the same behavior in your fulfillment, you need to set the same output context, lifespan, and parameters as the static response. 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.ProgrammingLanguage;
    if (language) {
        agent.add(`From fulfillment: Wow! I didn't know you knew ${language}`);
        agent.setContext({
              name: 'languages-followup',
              lifespan: 2,
              parameters:{language: language}
            });
    } else if (programmingLanguage) {
        agent.add(`From fulfillment: ${programmingLanguage} is cool.`);
        agent.setContext({
              name: 'languages-followup',
              lifespan: 2,
              parameters:{ProgrammingLanguage: programmingLanguage}
            });
    } else {
        agent.add(`From fulfillment: What language do you know?`)
    }
}

In this handler, you are adding an output context with the name "languages-followup" that has a lifespan of 2 turns, and a parameter value (language or ProgrammingLanguage) in the context for later retrieval. Click Deploy and interact with the simulator to test it.

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 example, 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.ProgrammingLanguage;
    const duration = agent.parameters.duration;
    agent.add(`I can't believe you've know ${language} for ${duration}!`);
}
Kunde den här sidan hjälpa dig? Berätta:

Skicka feedback om ...

Dialogflow Documentation
Behöver du hjälp? Besök vår supportsida.