Quickstart: Integrate services with fulfillment

This quickstart shows you how to integrate your agent with your services using fulfillment.

Before you begin

If you do not plan on using fulfillment, you can skip this quickstart.

This quickstart is a continuation of Agent building and interaction using the console. Steps below assume you have completed that quickstart and continue working on the same agent.

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.

Setup: Deploy and enable fulfillment

To enable and deploy fulfillment using the inline editor and Cloud functions for Firebase:

  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 it for the intent. To enable fulfillment for the 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 try it 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.

Create a custom fulfillment handler

The steps above used 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.

Entities

In previous steps, you created a "Languages" intent to identify spoken and programming languages. The intent uses the language and ProgrammingLanguages entities. In this section, you access parameter values for these entities 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.ProgrammingLanguages;
        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('Language', languageHandler);
    
  4. Click Deploy.

This handler retrieves the parameters language and ProgrammingLanguages 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 users specify in their requests.

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

Contexts

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.ProgrammingLanguages;
    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 ProgrammingLanguages) in the context for later retrieval. Click Deploy and interact with the simulator 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}!`);
}
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.