Skip to content

A Firebase Gateway and Fulfillment Webhook for the Dialogflow Google Service.

License

Notifications You must be signed in to change notification settings

karoldavid/dialogflow-starter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dialogflow Starter

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.

Prerequisites

Make sure you have installed all of the following prerequisites on your development machine:

Installation

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.

Recommended to Read First

It is highly suggested to read and follow the instructions here first (especially the part about authentication).

Dialogflow Setup

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

Firebase Setup

Initialize the Project

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.

Add the Service Account Key

  • 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

Connect the Webhook

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));

Run and deploy

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

Test the gateway and webhook

  • 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:
https://localhost:5000/walkthrough-fhakgj/us-central1/dialogflowGateway
  • Finally, add the request body. It should contain the sessionId string, and the queryInput object. Set the queryInput.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.

License

The MIT License (MIT).

Releases

No releases published

Packages

No packages published