Fulfillment defines the conversational interface for your Action to obtain
user input and the logic to process the input and eventually fulfill
the Action.

> [!IMPORTANT]
> **Learn more about building responses**
>
> The documentation here describes how to build fulfillment generally.
> See [Surface capabilities](https://developers.google.com/assistant/df-asdk/surface-capabilities.md.txt)
> and [Responses](https://developers.google.com/assistant/df-asdk/responses.md.txt) for more
> information on how to handle specific portions of your conversation.

## Define your conversation

> [!NOTE]
> **Note:** See the [Dialogflow documentation](https://www.dialogflow.com/docs/concept-intents) for full details on what you can do with intents.

Now that you've [defined Actions](https://developers.google.com/assistant/df-asdk/dialogflow/define-actions.md.txt), you can build
the corresponding conversation for those Actions. You do this by
creating Dialogflow intents that define the grammar or what users need
to say to trigger the intent and the corresponding fulfillment to process
the intent when it's triggered.

You can create as many intents as you want to define your entire conversation's
grammar.

### Create intents

Click the **+** sign by the **Intents** menu item in Dialogflow's
left navigation. The Intent Editor appears where you can enter the following information:

![](https://developers.google.com/static/assistant/df-asdk/dialogflow/images/dialogflow-fulfillment-00.png)

- **Intent name** is the name of the intent that's displayed in the IDE.
- **Contexts** let you scope the triggering of the intent to specific cases. Read the Dialogflow documentation on [Contexts](https://cloud.google.com/dialogflow/docs/contexts-overview) for more information.
- **Events** trigger intents without the need for users to say anything. One example event is the `GOOGLE_ASSISTANT_WELCOME` event, which allows the Google Assistant to invoke your Action. This event is used for your Action's [default Action](https://developers.google.com/assistant/df-asdk/dialogflow/define-actions.md.txt#define-a-default-action). Reference our documentation for more information on [built-in helper intents](https://developers.google.com/assistant/df-asdk/helpers.md.txt#built-in_helper_intents).
- **Training phrases** define what users need to say (the grammar) to trigger the intent. Type a few phrases here (5-10) of what users can say to trigger the intent. Dialogflow automatically handles natural variations of the example phrases you provide.
- **Action and parameters** defines what data to pass to fulfillment, if
  fulfillment is enabled for this intent. This includes data parsed from the
  user input and the name that you can use in your fulfillment to detect which
  intent was triggered. You'll use this name later to map your intent to its
  corresponding fulfillment logic. See [Actions and parameters](https://cloud.google.com/dialogflow/docs/intents-actions-parameters)
  in the Dialogflow documentation for more information about defining Actions.

  > [!NOTE]
  > **Note:** "Action" refers to this specific component of a Dialogflow intent, and doesn't refer to the larger concept of an Action in the Actions on Google platform.

- **Responses** is the Dialogflow Response Builder, where you can define the
  response to this intent directly within Dialogflow, without calling
  fulfillment. This feature is useful for static responses that don't require
  fulfillment. You might use this to provide simple welcome or goodbye messages.
  However, you'll likely use fulfillment to respond to your users for most
  intents.

- **Fulfillment** specifies whether or not you want to call your fulfillment
  when this intent is triggered. You most likely will enable this for most
  intents in your Dialogflow agent. To see this item in the intent, you must
  have fulfillment enabled for the agent in the **Fulfillment** menu.

## Building responses in Dialogflow

For some intents, you might not need to have your fulfillment return a response.
In these cases, you can use the response builder in Dialogflow to create
responses.

In the **Responses** area, provide the textual response you want to return to
users. Default text responses are simple TTS text responses that can work across
multiple Dialogflow integrations. Responses for Google Assistant are described
on the [Responses](https://developers.google.com/assistant/df-asdk/responses.md.txt) page.

![](https://developers.google.com/static/assistant/df-asdk/dialogflow/images/dialogflow-fulfillment-01.png)

## Building fulfillment responses

Your fulfillment code is hosted in the webhook fulfillment logic for an Action.
For instance, in the [Silly Name Maker sample](https://github.com/actions-on-google/dialogflow-silly-name-maker-webhook-nodejs),
this logic is found in `index.js` for the Cloud Function for Firebase.

When an intent is triggered that uses fulfillment, you receive a request from
Dialogflow that contains information about the intent. You then respond to the
request by processing the intent and returning a response. This request and
response is defined by the [Dialogflow webhook](https://developers.google.com/assistant/df-asdk/reference/dialogflow-webhook-json.md.txt).

We highly recommend that you use the Node.js client library to process requests
and return responses. Here's the general process for using the client library:

1. [Initialize the Dialogflow object](https://developers.google.com/assistant/df-asdk/dialogflow/fulfillment.md.txt#initialize_the_dialogflow_object). This object automatically handles listening for requests and parsing them so that you can process them in your fulfillment.
2. [Create functions to handle requests](https://developers.google.com/assistant/df-asdk/dialogflow/fulfillment.md.txt#create_functions_to_handle_requests). These functions process the user input and other components of the intent and build the response to return to Dialogflow.

### Initialize the `Dialogflow` object

The following code instantiates `Dialogflow` and does some boilerplate
Node.js setup for Google Cloud Functions:
Node.js

```
'use strict';

const {dialogflow} = require('actions-on-google');
const functions = require('firebase-functions');

const app = dialogflow({debug: true});

app.intent('Default Welcome Intent', (conv) => {
  // Do things
});
exports.yourAction = functions.https.onRequest(app);
```
Java

```
public class DfFulfillment extends DialogflowApp {
  private static final Logger LOGGER = LoggerFactory.getLogger(DfFulfillment.class);

  @ForIntent("Default Welcome Intent")
  public ActionResponse welcome(ActionRequest request) {
    // Do things
    // ...
  }
```

> [!NOTE]
> **Note:** If deploying the above code using the [Dialogflow fulfillment inline editor](https://cloud.google.com/dialogflow/docs/fulfillment-inline-editor#create_a_webhook_with_the_inline_editor), change `exports.yourAction` to `exports.dialogflowFirebaseFulfillment`.

### Create functions to handle requests

When users speak a phrase that triggers an intent, you receive a request from
Dialogflow that you handle with a function in your fulfillment. In this
function, you'll generally do following things:

1. Carry out any logic required to process the user input.
2. Build your responses to respond to triggered intents. Take into account the surface that your users are using to construct appropriate responses. See [surface capabilities](https://developers.google.com/assistant/df-asdk/surface-capabilities.md.txt) for more information on how to cater responses for different surfaces.
3. Call the `ask()` function with your response.

> [!NOTE]
> **Note:** When using Cloud Functions for Firebase, global variables may be cached across function executions. For more information, read about [function instance lifespans](https://cloud.google.com/functions/docs/concepts/exec#function_instance_lifespan) in the Google Cloud documentation.

The following code shows you how to build two TTS responses that handle an
invocation intent (`input.welcome`) and a dialog intent (`input.number`) that
welcomes the user to your Action and echoes a number that a user has spoken for
a Dialogflow intent with the name:
Node.js

```
const app = dialogflow();
app.intent('Default Welcome Intent', (conv) => {
conv.ask('Welcome to number echo! Say a number.');
});
app.intent('Input Number', (conv, {num}) => {
// extract the num parameter as a local string variable
conv.close(`You said ${num}`);
});
```
Java

```
@ForIntent("Default Welcome Intent")
public ActionResponse defaultWelcome(ActionRequest request) {
  ResponseBuilder rb = getResponseBuilder(request);
  rb.add("Welcome to number echo! Say a number.");
  return rb.build();
}

@ForIntent("Input Number")
public ActionResponse inputNumber(ActionRequest request) {
  ResponseBuilder rb = getResponseBuilder(request);
  Integer number = (Integer) request.getParameter("num");
  rb.add("You said " + number.toString());
  return rb.endConversation().build();
}
```

The custom intent *Input Number* , which accompanies the code above, uses the
`@sys.number` entity to extract a number from user utterances. The intent then
sends the `num` parameter, which contains the number from the user, to the
function in the fulfillment.

![](https://developers.google.com/static/assistant/df-asdk/dialogflow/images/dialogflow-fulfillment-02.png)

Instead of having individual handlers for each intent, you can alternatively add
a fallback function. Inside the fallback function, check which intent triggered
it and do the appropriate thing accordingly.
Node.js

```
const WELCOME_INTENT = 'Default Welcome Intent';
const NUMBER_INTENT = 'Input Number';
const NUMBER_PARAMETER = 'num';
// you can add a fallback function instead of a function for individual intents
app.fallback((conv) => {
 // intent contains the name of the intent
 // you defined in the Intents area of Dialogflow
 const intent = conv.intent;
 switch (intent) {
   case WELCOME_INTENT:
     conv.ask('Welcome! Say a number.');
     break;
   case NUMBER_INTENT:
     const num = conv.parameters[NUMBER_PARAMETER];
     conv.close(`You said ${num}`);
     break;
 }
});
```
Java

```
// you can add a fallback function instead of a function for individual intents
@ForIntent("Default Fallback Intent")
public ActionResponse fallback(ActionRequest request) {
  final String WELCOME_INTENT = "Default Welcome Intent";
  final String NUMBER_INTENT = "Input Number";
  final String NUMBER_ARGUMENT = "num";
  // intent contains the name of the intent
  // you defined in the Intents area of Dialogflow
  ResponseBuilder rb = getResponseBuilder(request);
  String intent = request.getIntent();
  switch (intent) {
    case WELCOME_INTENT:
      rb.add("Welcome! Say a number.");
      break;
    case NUMBER_INTENT:
      Integer num = (Integer) request.getParameter(NUMBER_ARGUMENT);
      rb.add("You said " + num).endConversation();
      break;
  }
  return rb.build();
}
```

### No-match reprompting

When Dialogflow cannot match any of the input grammars defined in your intents'
**Training phrases** , it triggers a fallback intent. Fallback intents typically
reprompt the user to provide the necessary input for your Action. You can
provide reprompt phrases by specifying them in the **Response** area of a
fallback intent or you can use a webhook to provide responses.

When a user's response doesn't match your Action's training phrases, Google
Assistant attempts to handle the input. This behavior facilitates users changing
Actions in the middle of a conversation. For example, a user asks, "What films
are playing this week?" and then changes context mid-conversation: "What is
the weather tomorrow?" In this example, because "What is the weather tomorrow?"
isn't a valid response to the conversation triggered by the initial prompt,
Assistant automatically attempts to handle the match and move the user into an
appropriate conversation.

If Assistant can't find an appropriate Action that matches the user's input,
the user is returned to the context of your Action.

Because Assistant may interrupt your Action to respond to a valid no-match
scenario, do not use fallback intents as a way to fulfill user queries. You
should only use fallback intents to reprompt your user for valid input.

> [!NOTE]
> **Note:** By default, Dialogflow automatically creates a **Default Fallback Intent** for every agent. You should modify the responses provided by this intent or delete it and create your own fallback intents to properly handle no-match cases. The default responses in the **Default Fallback Intent** are too general in most cases.

> [!WARNING]
> **Warning:** Do not use fallback intents to attempt to fulfill user queries. Fallback intents and no-match handling should only be used to reprompt the user. Instead, [define an action that uses the `@sys.any` entity](https://developers.google.com/assistant/df-asdk/dialogflow/define-actions.md.txt#handling_an_unrecognized_action).

To create a fallback intent:

1. Click **Intents** in the navigation menu of Dialogflow.
2. Click ⋮ next to **Create Intent** and select **Create Fallback Intent** . (Alternatively, click the **Default Fallback
   Intent** to edit it.)
3. Specify reprompt phrases to speak back to users. These phrases should be
   conversational and be as useful as possible to the user's current context.

   **To do this without fulfillment** : Specify phrases in the **Response** area
   of the intent. Dialogflow randomly chooses phrases from this list to speak
   back to users until a more specific intent is triggered.

   **To do this with fulfillment**:
   1. Toggle **Enable webhook call for this intent** in the intent's **Fulfillment** section. ![](https://developers.google.com/assistant/df-asdk/dialogflow/images/dialogflow-fulfillment-03.png)
   2. In your fulfillment logic, handle the fallback intent like any other intents, as described in the [Create functions to handle requests](https://developers.google.com/assistant/df-asdk/dialogflow/fulfillment.md.txt#create_functions_to_handle_requests) section.

   For example, the following function uses the `conv.data` object (an
   arbitrary data payload that you can use to maintain state) from the
   [Node.js client library](https://developers.google.com/assistant/df-asdk/reference/nodejsv2/overview.md.txt) to store a
   counter that tracks how many times a fallback intent is triggered. If it's
   triggered more than once, the Action quits. While it's not shown in the
   code, you should make other intents reset this counter to 0
   when a non-fallback intent is triggered. (See the
   [Number Genie sample](https://github.com/actions-on-google/dialogflow-number-genie-nodejs/)
   for details on how to implement this.)
   Node.js

   ```
   app.intent('Default Fallback Intent', (conv) => {
    conv.data.fallbackCount++;
    // Provide two prompts before ending game
    if (conv.data.fallbackCount === 1) {
      conv.contexts.set(DONE_YES_NO_CONTEXT, 5);
      conv.ask('Are you done playing Number Genie?');
    } else {
      conv.close(`Since I'm still having trouble, so I'll stop here. ` +
        `Let's play again soon.`);
    }
   });
   ```
   Java

   ```
   @ForIntent("Default Fallback Intent")
   public ActionResponse defaultFallback(ActionRequest request) {
     final String DONE_YES_NO_CONTEXT = "done_yes_no_context";
     ResponseBuilder rb = getResponseBuilder(request);

     int fallbackCount =
         request.getConversationData().get("fallbackCount") == null
             ? 0
             : (Integer) request.getConversationData().get("fallbackCount");
     fallbackCount++;
     request.getConversationData().put("fallbackCount", fallbackCount);

     if (fallbackCount == 1) {
       rb.add(new ActionContext(DONE_YES_NO_CONTEXT, 5));
       rb.add("Are you done playing Number Genie?");
     } else {
       rb.add("Since I'm still having trouble, so I'll stop here. Let's play again soon")
           .endConversation();
     }
     return rb.build();
   }
   ```

#### Using contexts

Use contexts if you want Dialogflow to trigger fallback intents only in certain
situations. This is helpful if you want to have different fallback intents for
different no-match scenarios.

- If you don't set contexts on a fallback intent, it's considered to be a global fallback intent that Dialogflow triggers when no other intent is matched. You should only have one of these defined if you choose to use one.
- If you set input contexts on a fallback intent, Dialogflow triggers this
  fallback intent when the following are true:

  - The user's current contexts are a superset of the contexts defined in the intent.
  - No other intent matches.

  This lets you use multiple fallback intents with different input contexts to
  customize no-match reprompting to specific scenarios.
- If you set an output context on a fallback intent, you keep the user in the
  same context after the fallback intent is triggered and processed.

See [Dialogflow Contexts](https://cloud.google.com/dialogflow/docs/contexts-overview) for more information.

### No-input reprompting

See the [Reprompts](https://developers.google.com/assistant/df-asdk/reprompts.md.txt) page for details on how
to handle when the user does not provide further input on a voice device like a
Google Home that requires continued interaction.