Deconstructing Chatbots: Understanding Fulfillment by integrating Dialogflow with Google Calendar
In this article we will learn how Dialogflow connects with backend systems to provide rich and dynamic responses to users questions. We will use the same Agent that we created in previous labs “Appointment Scheduler”. In the Agent’s GCP project we will enable Google Calendar API and a service account to access google calendar. Then we create the fulfillment and use the credentials we generated for the calendar access to connect it with the fulfillment. Finally we will test to see if the calendar invites are being set up per user request.
What you’ll learn
- What is Fulfillment
- How to set up service account for the Google Calendar
- How to set up Google Calendar
- How to enable fulfillment in Dialogflow
- How to test Fulfillment
- Basic concepts and constructs of Dialogflow. For introductory Dialogflow tutorial videos that covers basic conversational design check out the Deconstructing chatbot series.
- We will be using the same Appointment Scheduler chatbot that we built in this article and build upon it to understand entities.
- Also go through the “Deconstructing Chatbots: Understanding Entities in Dialogflow” article as well.
- If you are interested in following visual guidance to se this up, check out this episode of deconstructing chatbots on youtube.
What is Fulfillment?
Fulfillment is code that’s deployed as a webhook that lets your Dialogflow agent call business logic on an intent-by-intent basis. During a conversation, fulfillment allows you to use the information extracted by Dialogflow’s natural language processing to generate dynamic responses or trigger actions on your back-end.
Most Dialogflow agents make use of fulfillment. The following are some example cases where you can use fulfillment to extend an agent:
- To generate dynamic responses based on information looked up from a database.
- To place orders based on products a customer has asked for.
- To implement the rules and winning conditions for a game.
Enable Calendar API
- In Dialogflow’s console, go to settings ⚙ and under the general tab, you’ll see the project ID section with a Google Cloud link to open the Google Cloud console. Open Google Cloud.
- In the Cloud console, go to the menu icon ☰ > APIs & Services > Library
- Search for Google Calendar API and then Enable to enable the API on your cloud project.
Setup Service Account
- Under the menu icon ☰ click on APIs & Services then click on Credentials
- Click on Create Credentials followed by Service Account Key.
- Under Create service account key, select New Service Account from the dropdown and enter AppointmentCalendar for the name and click Create. In the popup, select Create Without Role.
- A JSON file will be downloaded to your computer that you will need in the setup sections below.
- Open Google Calendar. On the left, next to “Add Calendar” click the three dots and select New Calendar
- Enter Appointment Calendar for the name of the calendar and select Create Calendar.
- Next, click the newly created “Appointment Calendar”, you will see it appear in the “Settings for my calendars” list on the left column.
- Copy client_email from the json file we downloaded as a part of the service account set up.
- Paste the client_email copied from the previous step into the “Add people” field of the Share with specific people section and then select “Make changes to events” in the permissions dropdown and select Send.
- While still in Settings, scroll down and copy the Calendar ID in the Integrate Calendar section.
Setup Fulfillment in Dialoflow
Add Service Account and Calendar ID to Fulfillment
- Navigate to Dialogflow Agent “AppointmentScheduler” and click on “Fulfillment”
- Enable inline code editor by toggling the switch.
- Update the index.js file with the following code.
- In index.js, update your CalendarID copied from prior section and replace <INSERT YOUR CALENDAR ID>
- Also update “serviceAccount” constant with the contents of your JSON file. Replace <INSERT CONTENTS OF YOUR JSON FILE HERE
- (Optional) Change the below fields as per your calendar’s “Appointment Calendar” timezone if not in PST (below is an example for CST):
const timeZone = ‘America/Chicago’;
const timeZoneOffset = ‘-05:00’;
- Click Deploy at the bottom of the page.
Enable Fulfillment response
- Navigate to Dialogflow “Intent”
- Click on “Schedule Appointment” Intent.
- Scroll down to Fulfillment and toggle “Enable webhook call for the intent”
- Click “Save” to save changes to the agent.
- Click “Deploy” under Fulfillment whenever there is a Cloud Functions change.
Test Your Chatbot!
Let’s test our chatbot, you can test it in the simulator or use the web or google home integration we have learnt in previous codelabs.
- User: “Set an appointment for vehicle registration at 2pm tomorrow”
- Chatbot response: “Ok, let me see if we can fit you in. April 24, 2 PM is fine!.”
- Calendar booking response:
You created a fulfillment using inline editor and integrated with Google Calendar. Think of more such integrations like these.