Build a chatbot using C# and Dialogflow

Fouad Roumieh
Voice Tech Podcast
Published in
7 min readAug 15, 2019

In this article/s I’ll explain the process of setting up a basic chatbot using Dialogflow and C# webhook. If you don’t know what Dialogflow is about, this article will be a good chance to get yourself introduced to. In this article I’ll NOT be explaining the principles of chatbot design, the internet has many resources around this topic, so I’ll leave it for you to dig and learn more about that and I suggest that you start with that before reading through this article.

So what is Dialogflow?. As the official documentation says:

Dialogflow is a natural language understanding platform that makes it easy to design and integrate a conversational user interface into your mobile app, web application, device, bot, interactive voice response system, and so on. Using Dialogflow, you can provide new and engaging ways for users to interact with your product.

In short, Dialogflow api will help us understand the user’s intent when they’re communicating with our chatbot and that will be of a great help to our application as this is the main functionality needed to get a successful chatbot, imagine the effort needed to build an api to take care of such concern.

The chatbot we will be building is around hotel booking. And here are some of the features that we will implement:

1- The chatbot should answer user greetings and kick-off the conversation.

2- The chatbot must understand the user’s intent to book a room and reply.

3- The chatbot must return the total amount for the room the user selected and the total nights of stay.

Setup an account on Dialogflow

Let’s start by setting up an account to access Dialogflow Console and to be able to do that you need a google/gmail account.

1. Go to https://dialogflow.com/ and click Sign in button the top right corner.

2. Proceed by entering your google account credentials.

3. After that, you should land on the Dialogflow homepage, click Go to Console from the top right corner.

4. Proceed by clicking sign in with google (I don’t understand this additional action to sign, but maybe google is planning to add some more sign in options in the future).

And viola you are setup with Dialogflow.

Please familiarize yourself with the console as this will be the main area you will be working with when building chatbots with Dialogflow.

Create your first Agent

The first step in setting up the chatbot functionality is to create an agent:

A Dialogflow agent is a virtual agent that handles conversations with your end-users. It is a natural language understanding module that understands the nuances of human language. Dialogflow translates end-user text or audio during a conversation to structured data that your apps and services can understand. You design and build a Dialogflow agent to handle the types of conversations required for your system.

  1. Login in to dialogflow console if you are not already.

2. From the left menu click on Create new agent:

3. Let’s give a name of HotelBookingBot, you can also set other options for language or timezone. Note that you cannot change language later.

4. Click Create and wait until it redirects you to the agent homepage below:

Agent homescreen

You can see that the agent comes with a default fallback intent and default welcome intent. You can interact with those intents now by typing like “hello” in the simulator on the right of the screen:

Test console.

This is a great feature to help you test intents on the fly.

Build better voice apps. Get more articles & interviews from voice technology experts at voicetechpodcast.com

As you can see with those simple steps we’ve already created a basic chatbot functionality that can understand a greeting and reply back. Also you can type an unknown word or a non-greeting one and the fallback intent will be able to handle the response for you.

The reason for multiple responses is to make the conversation look close to the natural one where the chatbot will pick answers randomly and of course you can keep it as one static response when needed.

With the above setup we’ve managed to cater for one of the chatbot requirements that we’ve mentioned earlier by having the chatbot answering the user’s greeting. Of course you can customize the responses to have it more branded for example if the user typed hello, the chatbot reply with “Welcome to [HotelName], I’ll help to make your booking smoothly”. To do so, click on the Default Welcome intent and go to the responses to update messages/replies and you can do the same for the Fallback intent also.

Create an intent that handles Room booking query

The next step in our flow is that we expect the user to type after greeting something like: I need to make a booking, can you help?. In that case the chatbot should answer something like: I can definitely assist with that and make the booking on your behalf! Could you please tell how many persons and for how many nights?

Let’s create a new Intent by clicking the + sign next to Intents menu item. You can give it a name as: Room Booking.

Jump to training phrase and populate it as below. Those phrases are similar to the phrases that we expect from the end user, this list can grow to enhance the accuracy of our chatbot.

Training Phrases

And because we are expecting our chatbot to reply the above similar inputs from the user, we need to populate the expected response as below:

Responses

Hit the Save button and go ahead to try this conversation in the simulator by typing for example: I need to make a booking. You should get the above response. Notice that the input that you just typed doesn’t need to be always the same as the one we created, you can type: I want to book a room and the response will still be the one we expect. That’s because the NLP algorithm of Dialogflow is helping us matching those input as close as possible and if something mismatched we need to do additional training on the intent, more info available here.

Create an intent to extract booking info

As you might have expected that after the above response from the chatbot the user will answer with something like: 2 persons for 3 nights. From this we will need to extract the info need for the booking.

For that purpose let’s create a new intent and call it Booking Info. Next jump to Training Phrases and type: 2 persons for 3 nights and hit enter. You will notice that Dialogflow automatically extracted 2 entities from that phrase and it will give them a default parameter name, that’s not exactly what we want for our scenario. Let’s do a minor change by highlighting the text (check the below two images) and changing the parameter names to: totalPersons and totalNights also make these entities mandatory (we need those values to be able to make the booking) by ticking the Required checkbox as below, make you un-check the IS List:

You can click on the Prompts field to set up a message to be sent to the user if an entity value is missing, for example the user may type 2 person only without telling the total nights.

I ended up with below the phrases for this scenario, make sure to fix the auto extraction done by Dialogflow by highlight the text from the phrase and selecting the right entity. Dialogflow might add additional entities for extraction you can just delete them.

Give the action a name: book and hit the Save button.

Now let’s test this in the simulator by typing: I would like to book a room for 2 persons over 3 nights. You can see below that Dialogflow extracted the parameters or info correctly.

You can see now where this is going, the next step is to pass these values to a webhook (in a real world would be the booking api) and from within that webhook we will return a response for the user to tell about the expected amount for his/her booking.

So let’s do that in the next article: Create a C# .netcore webhook for a Dialogflow chatbot

--

--