Getting started with Dialogflow fulfillment

To help you get started with fulfillment, Dialogflow has an integrated editor powered by Cloud Functions for Firebase. The cloud project and resource allocation has been provided for you already. All you have to do is enable it and start coding.

In most cases the free "Spark" tier of Firebase is all you'll need. Tier limitations and pricing information for the other tiers can be found on the Firebase pricing page.

This guide will walk you through deploying and customizing the sample code provided in the editor, as well as steps to take afterwards.

Enabling Cloud Functions for Firebase

  1. Login to Dialogflow.
  2. Create an new agent or open an existing agent.
  3. Click on Fulfillment in the left menu.
  4. Click the switch for Cloud Functions for Firebase.

This will reveal the code editor, with some sample code. This code can also be found on GitHub. Click Deploy at the bottom of the page.

Dialogflow will save your Cloud Function for Firebase and begin the deployment process. Initial deployment may take up to a minute or two, but subsequent updates should be quicker. Once deployed, a time stamp will appear next to the Deploy button, indicating when the function was last updated.

Enable fulfillment for default intents

Now that you've enabled fulfillment for your agent, you'll need to enable webhook fulfillment for each intent you want provide fulfillment for.

  1. Click on Intents in the left menu.
  2. Click on Default Fallback Intent.
  3. At the bottom of the page, click Fulfillment to reveal the options.
  4. Check Use webhook.
  5. Click the Save button.

Repeat these steps for Default Welcome Intent.

The sample code handles the two default intents included when you create an agent, and responds to the user via the Cloud Function for Firebase fulfillment.

If the Default Welcome Intent is matched, the code defined on line 31 (for queries via Google Assistant) or 33 (all other queries) is used. If the Default Fallback Intent is matched, code on line 40 (for queries via Google Assistant) or 40 (all other queries) is used.

You can change the string which will change how your agent responds. If you do make changes, you will need to redeploy to have those changes pushed to the live function.

Actions

To make use of the last response, you will need to setup a new intent.

  1. Click on the plus icon next to Intents in the left menu.
  2. Name the intent whatever you want.
  3. Enter the following User Says example: new intent test
  4. Scroll down and click on Fulfillment.
  5. Check the Use webhook option.
  6. Click the Save button.

After your agent is finished training (the settings gear will stop spinning), type new intent test in the simulator on the right, and hit enter. You should see the following response: This message is from Dialogflow's Cloud Funtions for Firebase editor!

So how does the fulfillment know which block of code to trigger? Dialogflow uses something called actions to indicate to the fulfillment what steps should be taken to complete the user's request. Actions are strings you specify in an intent, and reference in your fulfillment code.

The action name is sent in the body of every request, in the result.action attribute. You can see this on line 13 of the sample code. Dialogflow automatically adds actions for the default Welcome and Fallback intents, and these are referenced in the sample code on lines 28 and 37.

What's next?

Rich responses

Rich responses allow you to take advantage of an individual platform's native UI elements like cards with the Google Assistant, attachments with Slack, and themplates for Facebook.

Google Assistant Slack Facebook

To start using rich messaging in the sample code:

  1. Uncomment lines 50 and 58 and redeploy the function.

  2. Enable and setup the Google Assistant, Slack, or Facebook integration.

  3. Through the integration's interface, talk to your agent and match the default action by responding with "new intent test".

In the response you will see a rich response that uses the native platform's UI elements. To view and modify the rich responses for Google Assistant see lines 129-149 of the sample code. Facebook and Slack rich responses can be found on lines 151-190.

See more about rich messaging.

Contexts and parameters

To apply outgoing contexts to your webhook requests, uncomment lines 51 and 59 and redeploy the function. Now your function will apply an outgoing context of weather with a lifespan of 2 and a city parameter with the value of Rome to any matched intents with the action default.

You can get incoming contexts which are sent in the body of every webhook request originated from Dialogflow in the result.contexts attribute as indicated in line 19. Parameters can be retrieved via the result.parameters attribute as indicated on line 16.

Learn more about contexts and parameters.

Firebase Cloud Function logs

After deploying, you can click on the View execution logs in the Firebase console link, next to the Deploy button. This will take you to the logs related to your deployed function.

This can be useful when debugging issues with your fulfillment. console.log statements are also surfaced in here, to also aid in debugging. For instance, the sample code logs the incoming request headers and body on lines 9 and 10.

Dependencies and package.json

package.json installs dependencies specified in the file. This file can be modified to include additional dependencies your fulfillment uses. These will be installed the next time you deploy.

Moving fulfillment outside of the editor

The inline editor is a great way to get started with fulfillment for your agent. When your fulfillment grows you'll likely want to take advantage of more advanced features, like organizing your code with multiple files, using private npm modules, or using advanced Firebase features like environment config. To do any of this, you'll need to move out of the code editor and start deploying with the Firebase CLI. This section will detail just that.

Prerequisites

  • Download your code by clicking the download button in the upper right hand corner of the code editor.

  • Install the Firebase CLI.

  • Get your Google Project ID from the General tab of your agent's settings.

Deploy your function with the Firebase CLI

  1. Unzip your downloaded code found in "firebaseFulfillment.zip".
  2. Open a terminal or shell and navigate to the "functions" directory inside the unzipped achieve. For example, cd ~/Downloads/firebaseFulfillment/firebase/functions.
  3. Run npm install to install all of your functions' dependencies
  4. Run the command firebase login and login to the Google account associated with your agent and allow the Firebase CLI to manage your Cloud and Firebase Projects.
  5. Run the command firebase init. When prompted for feature selection choose Functions. When prompted for a Firebase Project, select the Cloud Project ID found in your Dialogflow agent's settings.
  6. Deploy your Cloud Function for Firebase with firebase deploy --only functions:dialogflowFirebaseFulfillment
  7. Copy the Function URL after deployment (may take a minute or two) and paste it in your Dialogflow agent's fulfillment!

Limitations

Here is a list of things to be aware of when using the Cloud Functions for Firebase option:

  • If you modify your function outside of code editor, you can no longer use the Dialogflow editor to modify your Cloud Function for Firebase. Your function will continue to provide fulfillment for your agent, but if you need to make changes, you will need to do so in the Firebase console.

  • The function must be named "dialogflowFirebaseFulfillment". If you change the name of your function it will not deploy via Dialogflow. If you want to change the name of your function you can download the code, change the function name, and deploy through Firebase's CLI.

  • Code modified in the dialogflow editor cannot be saved or downloaded without being deployed.

  • Dialogflow's editor only supports two files: index.js and package.json (modifying package.json will install any dependencies you specify upon deployment)

  • Network calls originating from your Cloud Function for Firebase to destinations outside Google's network require billing to be enabled for the underlying Google Cloud or Firebase project (https://cloud.google.com/functions/pricing#networking)