PRIYANKA VERGADIA: Welcome to Deconstructing Chatbots.
I'm Priyanka Vergadia.
And in this episode, we will learn
how to integrate a Dialogflow agent with a back end
system like a database, calendar, or a CRM.
[MUSIC PLAYING]
Any chatbot you build will need to connect to a back end
to transfer information to and from the business logic.
Within Dialogflow, we are able to make that connection using
fulfillment.
Fulfillment is a piece of code which is deployed as a web hook
and lets your Dialogflow agent call the business
logic on a per intent basis.
During a conversation, this allows
you to use the information extracted from Dialogflow's
natural language processing to generate a dynamic response
or to trigger action on your back end.
Let's take our appointment scheduler chatbot
from previous episodes and implement a fulfillment
for scheduling appointments.
Well, currently, our agent identifies the user's intent
of scheduling an appointment and provides
a static response indicating that the appointment is set up.
Now, in a real world scenario, we
need to set up that appointment on a calendar
and also check for any scheduling conflicts, which
requires us to integrate with the calendar.
So let's jump into our Dialogflow console
and see how it is all set up.
Click on the gear icon near your agent name to open Settings.
Look at the Google Cloud Project ID
and click on it to open the project in GCP console.
Navigate to the hamburger menu icon
and locate APIs and Services.
Click on Enable APIs and Services
and look for Google Calendar API.
Once you find it, enable the API on your cloud project.
Now under the same APIs and Services within Credentials,
click on Create credentials dropdown
to select Service account key.
Under Create service account key,
select the New service account from the dropdown
and enter an account name.
Make sure JSON is the key type and then click Create.
In the popup, select Create without role.
A JSON file will be downloaded to your computer.
And you will need this later, so take note of its location.
Before we close it out though, copy the client email.
Now open Google Calendar.
And on the left, click the plus sign and select New Calendar.
Enter AppointmentCalendar for the name of the calendar
and select Create Calendar.
Next, go to the AppointmentCalendar
that will appear on the left.
Click Add People and paste the email
copied from that JSON file.
Then select Make changes to events in the Permission
dropdown and click Send.
At this point, we are ready to go back to our Dialogflow agent
and set the fulfillment.
Click on Fulfillment and enable the inline editor.
Check out the link in the description
below to get the required code for package.json and index.js.
Open package.json file and copy and paste the dependencies
into fulfillment.
Do the same for index.js as well.
Grab the Calendar ID from the Google Calendar.
We just create it and replace that in index.js.
Next, copy the contents of the JSON file downloaded
in the Service Account Setup section
and paste it into the empty object, ServiceAccount.
Check out the rest of the code.
Make appointment function contains the responses
to the user.
And create calendar event function
takes care of grabbing the entities like date, time,
and appointment type, and creates the calendar invite.
It also looks for the calendar conflicts
and throws exceptions.
Once you're done, click Deploy at the bottom of the page.
Now, we have one more thing to do before we can start testing.
Click on Intents and go to the Schedule Appointment intent.
Scroll down to Fulfillment and enable webhook and save.
Now we're ready to test.
In the simulator on the right, query your Dialogflow agent
with something like, set an appointment at 4:00
PM tomorrow.
And then respond to the follow up queries.
After getting the required information,
you will see that the appointment is added
to the appointment calendar.
You can verify that if you ask for another appointment
at the same time slot, your agent will respond back
with an appropriate message since that slot is not free.
All right.
So that was a lot.
Let's summarize what we learned today.
We enhanced our appointment scheduler chatbot
by creating a fulfillment through inline editor,
enabled Google Calendar API for the GCP project
and used those APIs credentials to connect
our fulfillment Cloud Function with Google Calendar.
And finally, we were able to test
our agent by creating appointment
request on the calendar.
Don't miss the next episode of Deconstructing Chatbots
to learn more about conversational AI.
Please like and subscribe to our channel for more such content.
[MUSIC PLAYING]