Search code examples
asp.net-coredialogflow-es

How to build a chatbot using .NET Core and dialogflow?


I'm building my own MVC web application using .NET Core and I want to add a chatbot application in my website.

In my point of view, after building an agent in dialogflow, and then I should can use webhook to let my C# application to connect to the agent which already exists and push request to it and get response from the agent.

How can I connect .NET Core client to dialogflow agent which already exists or may be some reference?


Solution

  • It's fairly easy. Follow the instructions below :

    1. Install Nuget package Google.Apis.Dialogflow.v2 and its dependencies.
    2. Create the GCP credentials access key. Save the JSON to your local machine.
    3. Connect to GCP by setting-up the environment variable GOOGLE_APPLICATION_CREDENTIALS. Refer the following code : System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", _CredentialsPath) :

    4. Use following like code to

      Public Function GetResponseTo(whatUserSays As String) As String

          Dim textInput As New TextInput, queryInput As New QueryInput
      
          textInput.Text = whatUserSays
          textInput.LanguageCode = "en"
          queryInput.Text = textInput
      
          _Response = _Client.DetectIntent(New SessionName("newagent-12345", _SessionId), queryInput).QueryResult.FulfillmentText
      
          Debug.Print(_Response)
      
          Return _Response
      
      End Function
      

    where - queryInput contains the text typed by the user.