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
- Login to Dialogflow.
- Create an new agent or open an existing agent.
- Click on Fulfillment in the left menu.
- 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.
- Click on Intents in the left menu.
- Click on Default Fallback Intent.
- At the bottom of the page, click Fulfillment to reveal the options.
- Check Use webhook.
- 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.
To make use of the last response, you will need to setup a new intent.
- Click on the plus icon next to Intents in the left menu.
- Name the intent whatever you want.
- Enter the following User Says example:
new intent test
- Scroll down and click on Fulfillment.
- Check the Use webhook option.
- 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.
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.
To start using rich messaging in the sample code:
Uncomment lines 50 and 58 and redeploy the function.
Enable and setup the Google Assistant, Slack, or Facebook integration.
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.
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.
Download your code by clicking the download button in the upper right hand corner of the code editor.
Get your Google Project ID from the General tab of your agent's settings.
Deploy your function with the Firebase CLI
- Unzip your downloaded code found in "firebaseFulfillment.zip".
- Open a terminal or shell and navigate to the "functions" directory inside the unzipped achieve. For example,
npm installto install all of your functions' dependencies
- Run the command
firebase loginand login to the Google account associated with your agent and allow the Firebase CLI to manage your Cloud and Firebase Projects.
- 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.
- Deploy your Cloud Function for Firebase with
firebase deploy --only functions:dialogflowFirebaseFulfillment
- Copy the Function URL after deployment (may take a minute or two) and paste it in your Dialogflow agent's fulfillment!
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)