Integration of Google Dialogflow Webhook Fullfillment and .
NET Core Web API using C#Fei LiBlockedUnblockFollowFollowingMay 18Google Dialogflow is being a very powerful tool to build chatbots using AI technologies.
Users of Google Dialogflow can train the chatbot and provide responses using the user interface of Dialogflow, but it would be more powerful, efficient, and convenient for users to provide customized responses via Webhook.
In this tutorial, I would like to introduce how to integrate Google Dialogflow Webhook and Web API using .
NET Core and C#.
In this tutorial, we will complete following items:Create a simple chatbot with intent and entities using Google Dialogflow to get some basic information of a library.
Implement Web API using .
NET Core and C#.
Deploy the Web API to Azure.
Enable Webhook with the deployed Web API.
Now let’s get started!Create a Simple Chatbot using Google DialogflowIn this part, I am going to create a simple chatbot for users to query information of a library.
Let’s do this step by step.
At first, I created a new agent named library-helper in Google Dialogflow.
Create a new agent named library-helperAfter that, I created a new intent for users to query information named “library-info” without any training phrases yet.
New Intent named “library-info”Now we can add some training phrases to the intent, this helps to provide some possible questions that a user may ask.
The following picture shows some sample questions I added to this intent.
Since we are supposed to get responses from Web API, I don’t add any responses in this intent.
Sample Training PhrasesTo help our chatbot understand these key points in sample questions, we can create some entities to extract and identify useful information from users’ input questions.
In this tutorial, I created three entities named “name”, “address”, and “business-hours”.
Three EntitiesAfter that, we can now go back to our created Intent “library-info”, and scroll down to “Training Phrases”.
We can help chatbot to extract useful information from the sample questions using different entities.
After that, we are pretty much done with the intent, and we just need to enable webhook call for this intent.
So just scroll down and enable the webhook.
Enable webhook call for this intentNow we are done with this intent, and we need to implement our Web API to handle request from Google Dialogflow.
Web API Implementation for Dialogflow WebhookIn this part, our main purpose is to implement Web API using .
NET Core and C# to handle http post request from Dialogflow Webhook, and return the corresponding response.
This part consists of three parts:Setup Startup.
cs and Program.
cs in .
NET Core Web API projectImplement API controller to communicate with Dialogflow WebhookDeploy .
NET Core Web API to AzureFor part 1 and part 3, you may reference my another tutorial on how to create and deploy .
NET Core Web API to Azure.
We will focus on part 2 here.
The dataflow works like this: with Webhook enabled, when a user sent a query to Google Dialogflow to ask something, useful information would be extracted and matched by intents in the response form, and then Google Dialogflow will send the generated response as a Http Post Request to our web service (Web API).
After that, our Web API will handle the Request and Response withDialogflow.
A sample Webhook Request from documentation looks like following:Sample Webhook Http Post RequestTo handle this Webhook request, we will take advantages of “QueryResult → Parameters” to specify our response to Dialogflow.
At first, we need to install two packages to process Webhook request and response in our .
NET Core Web API project using NuGet:Google.
protobufInstall Dialogflow Package using NuGetWe need to make sure “Include prerelease” checked or we may have problem to find the “Google.
At second, we will create a new API controller class named “WebhookController.
cs” under “Controller” folder in our Web Api project.
Created a WebhookController classThirdly, we will need to implement our Web API to handle the Http Post Request from Google Dialogflow.
In my following sample, I provided customized response to different queries using Parameter entries.
To test our Web API for the Webhook, we can use Postman.
Simply run the web api locally and paste the url to Postman, and then add the webhook request sample and update parameters entry in the request body.
Test Web API for Webhook using PostmanIn the above sample test, we are querying name, address, and business hours.
So we should expect the fullfillment text with these information from our Web API, and the below image verified our Web API.
Web API for Dialogflow Webhook works at localSo now our web api is functioning correctly for Google Dialogflow Webhook.
To integrate our Web API and Google Dialogflow, I deployed this Web API to Azure.
If you want to know how to deploy Web API to Azure, again, you can check my another tutorial here.
After we deployed our Web API, we will get the public Url to access the Web API.
Enable Webhook in FulfillmentNow the real last step is to go back to our dialogflow and enable webhook under Fulfillment category and provide the URL of our Web API, and then save the changes.
Enable Webhook with Url of Web APIIt should be ready to go now, so let’s test it in Google Dialogflow with input “What’s the name, address, and business hours of the library?”, and we should expect all informatio we need from our Web API.
Test Webhook in Google DialogflowBingo!!!.It’s working just as desired!Reference:How to deploy .
NET Core Web API to Azure: https://medium.
8886196/how-to-deploy-net-core-web-api-to-azure-a127bfb20d09Google Dialogflow documentation: https://dialogflow.
com/docsSource Code for .
NET Core Web API is available in my github: https://github.
gitThank you for your time!.. More details