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:
- Read Dialogflow basics.
- Perform setup steps.
- 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:
- Click Fulfillment in the left sidebar menu.
- Toggle the Inline Editor to Enabled.
- 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.
- 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:
- Click Intents in the left sidebar menu.
- Click Default Welcome Intent.
Scroll down to the Fulfillment section and toggle Enable webhook call for this intent on.
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:
- Enable fulfillment for the
get-agent-name
intent you created in previous steps. - Click Fulfillment in the left sidebar menu, and examine the code in the inline editor.
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 isfunction welcome(agent)
.Below the existing handlers, add this function for the
get-agent-name
intent:function getAgentNameHandler(agent) { agent.add('From fulfillment: My name is Dialogflow!'); }
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 thewelcome
handler with the intent namedDefault Welcome Intent
.Below the existing
intentMap.set
calls, add this line for theget-agent-name
intent:intentMap.set('get-agent-name', getAgentNameHandler);
Click Deploy at the bottom of the form.
Enter
What's your name?
in the simulator. TheFrom 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:
- Enable fulfillment for the
set-language
intent. - Click Fulfillment in the left sidebar menu.
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);
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.