Use this code, written in TypeScript, to set up a "serverless" chatbot running on Node.js. The script consists of a gateway and one fulfillment webhook with Google Dialogflow and Firebase Cloud Functions.
Make sure you have installed all of the following prerequisites on your development machine:
- Node.js and the npm package manager. It is recommended to use the Node Version Manager.
- Google Cloud SDK.
- Firebase Command Line Interface.
- Postman API client (to test the functions).
Download or clone the repository:
git clone --depth 1 https://github.com/karoldavid/dialogflow-starter.git
Find the next installation instructions below in the Firebase Setup section.
It is highly suggested to read and follow the instructions here first (especially the part about authentication).
To set up your Dialogflow agent with intent and webhook, read the Dialogflow basics and follow below steps:
- Create an agent with Dialogflow.
- Associate the agent with an existing Google Project or create a new Google Project.
- Create an intent, add a couple of training phrases for this intent, and add one ore more actions, parameters with prompts.
- At the very bottom, enable Fulfillment for this intent to be able to call a webservice to connect the backend. Finally, save the intent.
- Got to Fulfillment, enable the Webhook, add the webhook url with your project ID the webhook name and hit save:
https://us-central1-<project-id>.cloudfunctions.net/dialogflowFirebaseFulfillment
- At the root level of your local dialogflow-starter repository initialize the Firebase SDK for Cloud Functions:
firebase init functions
- Choose your Dialogflow project from the project list.
- Choose TypeScript.
- Choose to use TSLINT.
- When asked to overwrite an existing file, always choose NO.
- When asked to install npm dependencies, say YES.
- Got to the Firebase Console, select your project, got to Settings, and then Service accounts.
- Click the Generate Private Key button.
- Download the json file to your local project's functions folder.
- Rename the key file to
service-account.json
:
dialogflow-starter/functions/service-account.json
In the webhook script in functions/src/fulfillment/index.ts
:
-
Replace the intent name with your intent name.
-
Add your own intent handler.
// functions/src/fulfillment/index.ts
intentMap.set([YOUR INTENT NAME], yourCallback(queryResult));
Before launching the app, cd into to functions folder and execute the following command:
npm run build
Next, execute below command to start the emulator:
firebase serve
Finally, if everything looks just fine, deploy the functions:
firebase deploy
- To test the interactions with the agent via http requests, open the Postman API Client.
- Set the request method to POST.
- Set the Dialogflow gateway's request url (which can be copied from the terminal output). Here is an example:
http://localhost:5000/walkthrough-fhakgj/us-central1/dialogflowGateway
- Finally, add the request body. It should contain the
sessionId
string, and thequeryInput
object. Set thequeryInput.text
property to one of your intent's training phrases:
{
"sessionId": "foo",
"queryInput": {
"text": {
"text": [INTENT_TRAINING_PHRASE_STRING],
"languageCode": "en-US"
}
}
}
- When you now hit the send button, you should get a response from your Dialogflow agent.
- The answer of the last question should trigger the webhook. If your intent's fulfillement is set up to save data to the firebase db, you can control the operation in your Firebase project database.
- The Dialogflow agent's final answer should look as configured in the intent handler.
The MIT License (MIT).