Quickstart: Fulfillment

This guide 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 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 guide. Steps below continue working on the same agent. If you no longer have that agent, you can download build-agent-quickstart.zip and import the file.

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. Cloud Functions has associated charges, but the service is available for no charge up to a significant number of monthly invocations. Note that you still must register and provide a valid billing account. Cancel anytime.

When you initially enable the inline editor, Node.js 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.

The inline editor is intended for simple fulfillment testing and prototyping. Once you are ready to build a production application, you should create a webhook service.

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. If you did not enable billing in setup steps, you are prompted to enable billing now. Cloud Functions has associated charges, but the service is available for no charge up to a significant number of monthly invocations. Note that you still must register and provide a billing account. Cancel anytime.
  4. Click Deploy at the bottom of the form, and wait until dialogs indicate that it has been deployed.

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. Scroll down to the Fulfillment section and toggle Enable webhook call for this intent on.

  4. Click the Save button and wait until the Agent Training dialog indicates that training is complete.

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 get-agent-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 get-agent-name intent:

    function getAgentNameHandler(agent) {
      agent.add('From fulfillment: 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 get-agent-name intent:

    intentMap.set('get-agent-name', getAgentNameHandler);
    
  7. Click Deploy at the bottom of the form.

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

Access parameter values

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

To add the handler:

  1. Enable fulfillment for the set-language 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['language-programming'];
        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('set-language', languageHandler);
    
  4. Click Deploy.

This handler retrieves the parameters language and language-programming 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.