To integrate smart chat-bot features (such as natural language processing) into conversational agents, Google offers Dialogflow: a natural language understanding platform that understands user intent. This sample demonstrates how to the Business Messages platform in combination with Dialogflow to understand and respond to user messages.
The sample supports:
- Sending text, rich cards, and suggestions to consumers
- Routing logic between bot (Dialogflow) and human answers
- Customer satisfaction surveys
- Administration console with live agent prototype
- Quick setup of prerequisites and deployment to App Engine on behalf of the user
This runs on the Google App Engine. See the Google App Engine standard environment documentation for detailed instructions.
You must have the following software installed on your development machine:
- Google Cloud SDK (aka gcloud)
- Node.js - version 10 or above
- Python - version 3.7.0 or above
- Pip (Python package manager)
Open Google Cloud Console with your Business Messages Google account and create a new project for your agent. Note the Project ID and Project number values. Use https://PROJECT ID.appspot.com/callback as the webhook URL.
- Register your project with Business Messages. You may have to wait for a response from Google before continuing.
- Save the "Partner Key" provided by Google.
- Go to the API Console to view a list of existing projects.
- From the projects list, select a project or create a new one.
- Open the console left navigation menu and select Billing.
- Click Enable billing (if billing is already enabled then this option isn't available).
- If you don't have a billing account, create one.
- Select your location, fill out the form, and click Submit and enable billing.
This script handles enabling APIs, saving service accounts, enabling Firebase, and deploying to App Engine.
- Clone the Github repository:
$ git clone https://github.com/google-business-communications/bm-nodejs-dialogflow-integration
- Run
gcloud init
, authorize your account, and choose the Google Cloud project created previously. - Run
cd biab && pip install -r requirements.txt
. - Run
python start.py <partner_key>
, where <partner_key> is the token saved earlier during the “Register with Business Messages” step.
Once the project setup script is finished and the app is deployed, it prints out
* Administration Console URL
* Firebase rule URL
Save these URLs for the next step.
If the administration console URL's domain is different from https://PROJECT ID.appspot.com/, then please contact Google to update your webhook URL.
- Go to the Dialogflow console and create a new agent for your bot to send messages to.
- Click the settings icon next to the agent name (in the top left). Note the Project ID.
- On the same page, click the email next to Service Account to visit the GCP project.
- Next to this service account, click the 3-dot menu button and select Create key. Select JSON, and download the file.
- Visit the Administration Console URL (printed in the setup script output). In the top right, click Create Brand and give it a name. You should see it in the List of Brands table.
- For the brand you just created, click See Agents, then click Create Agent in the top right and follow the instructions.
- Submit the agent creation request. The agent appears in the List of Agents table.
- Click the newly created agent. Visit the Bot Connector Tab. Copy and paste the exact contents of the file you just downloaded into the DialogFlow Service Account Key field.
- Type the Dialogflow Project ID in the corresponding field.
- Fill in the CSAT Trigger (in minutes) field. This field specifies after how many minutes of inactivity a customer satisfaction survey will be sent (default 15 minutes)
- Click Save.
Visit the Firebase rules link printed after running the setup script and set your appropriate security rules.
- For testing, the following rules are suggested (open read/write to all):
{
"rules":{
".read": true,
".write": true
}
}
To start creating behavior for your Dialogflow bot, you must define intents (learn more).
- Go to the Dialogflow console, and select the previously created project in the top left.
- Click Intents, and then select Create Intent in the top right.
- In the Training Phrases section, add phrases that should lead to this intent. For example, a “store hours” intent may have such training phrases as:
- “When are you open?”
- “What time do you close?”
- “What are your hours of operation?”
- “Hours”
- Filling the Actions and Parameters section is optional: to store parameters from the input (for example, “can I order $parameter?”), follow the instructions here.
- This sample supports text responses, custom payloads (for cards, suggestions, etc.), or both. Filling the Responses section:
- To add a simple “text” or chat response, fill out the text response.
- Otherwise, click Add Response and then click Custom Payload. Follow the JSON schema available in the Business Messages API. Samples are available in the
examples/
folder. - Leave the
messageId
field, as shown in the example JSON. - If the text response is empty and there is a custom payload, click the garbage can icon to delete the empty response (Dialogflow doesn't send messages with an empty text response).
postbackData
is treated the same way as message content in this sample. To handle suggestions, createpostbackData
messages with similar content to what users would send. See an example inexamples/suggestion.json
.
- Create a “live agent” request:
a. Create training phrases such as “live agent” and “can I speak to a person?”
b. Delete the text response.
c. Create a Custom Payload response, and paste the
examples/live_agent.json
file into the custom payload field. - Once you create your intent, click Save.
- After the agent finishes training, test it in the sidebar to the right and in Google Maps/Search.
- A sample Dialogflow bot is included in
examples/bm_test_bot.zip
. To import it, go to the Dialogflow console, and select the previously created project in the top left. - Click the gear icon in the top right, then click Export and Import.
- Click IMPORT FROM ZIP and select
examples/bm_test_bot.zip
. - This example project contains intents for a sample pizza restaurant and has entities defined for types of pizza (
$pizza_type
) and method of delivery ($delivery_method
), which can be seen in the order pizza intent on the Intents page.
- In the Admin Console (link found in output of the setup script) within the new agent you created, visit the Agent Info tab.
- Fill in the following fields:
- Agent Name
- Logo
- Welcome Message (this will be shown when a user first starts messaging your business)
- Privacy Policy
- Conversation Starters (optional -- these will be shown along with the Welcome Message)
- Click Submit.
- Open the Agent test URL on your iOS or Android device and test your agent.
- If the user requests a live agent, you can visit the Live Agent Chat tab to chat with the user.
As messages are received by the router, they start in the callback function in routes.js
. The router then verifies that the message is sent from Google routing the message. After the message is verified, the router checks the database to see if the user is currently speaking with the bot or a live agent.
After this is determined, the router checks for credentials in Firebase (if configured). If this passes, then the message will then be directed to handleByAgent
or handleByServer
, depending on whether the user is speaking with a live agent or the Dialogflow bot.
handleByAgent
updates the database with message information, and passes the message onto the live agent by calling liveAgent.receiveMessage()
handleByServer
updates the database with the message information, and sends it to handleResponse
, seconds typing, then passing the message onto Dialogflow through dfRequest.callDialogflow()
. This function attempts to retrieve credentials from Firebase, then calls the Dialogflow API through the node.js client library. Results from Dialogflow are then routed to the Business Messages API, where messages are sent to the user.