Dialogflow fulfillment with C# and App Engine

Dialogflow

Dialogflow is a developer platform for building voice or text-based conversational apps on a number of platforms such as Google Assistant, Facebook Messenger, Twilio, Skype and more. Earlier this year, we used Dialogflow to build a Google Assistant app and extended it to use the power of Google Cloud. You can read more about it on Google Cloud blog here, see the app code on GitHub here and one of my talk videos about the app is here.

Out of the box, Dialogflow provides features such as intents for matching user input to responses, entities to extract relevant information from conversations, contexts to maintain the state of the conversation. It also provides a way to extend its functionality via fulfillments.

Fulfillment

A fulfillment is a HTTPS webhook that Dialogflow can forward requests to. From then on, your code is in charge of handling the request and generating a response. This is very useful for having custom logic or introducing further intelligence/data to your app. In our app, we used fulfillment to search for images using Google Custom Search, used Vision API to analyze images with Machine Learning and performed rich data analysis with BigQuery.

An easy way of implementing a fulfillment webhook is via the Inline Editor in Dialogflow console. Inline Editor enables you to write a Node.js function to handle Dialogflow requests and deploy it to Cloud Functions with Firebase (see this codelab for details). While Inline Editor is easy, it only allows a single webhook and it’s not developer friendly. Ideally, you’d develop code locally and use some kind of version control.

You can setup a local environment and use Firebase to deploy the function to Cloud Function (this codelab shows how). While this is a more realistic setup, it’s still constrained by Cloud Functions limitations and Node.js is the only language you can use.

AppEngine for fulfillment

In our app, we took a different approach to implement the webhook. Instead of Node.js, we decided to use C# and instead of Cloud Functions, we containerized our app and deployed to App Engine for more flexibility.

Since it’s a container, we could have deployed to Kubernetes Engine as well but we chose App Engine because:

  1. App Engine gives us a HTTPS endpoint out of the box with minimal hassle whereas Kubernetes Engine HTTPS setup is not trivial.
  2. Versioning in App Engine allowed us to update and deploy our app easily, test it out and forward all the traffic to the new version when we’re ready.

HelloWorld fulfillment with C#

To give you an idea on how to write a Dialogflow fulfillment on AppEngine using C#, let’s write a HelloWorld fulfillment webhook.

First, you need to first create an ASP.NET Core web app:

dotnet new web -n fulfillment

And add Dialogflow NuGet package to the project. This package will allow us to parse requests from Dialogflow and generate responses:

dotnet add package Google.Cloud.Dialogflow.V2 --version 1.0.0-beta02

Now, we can edit Startup.cs file and add logic to handle Dialogflow requests. We need to first refer to Dialogflow NuGet library and some of its dependencies:

using Google.Cloud.Dialogflow.V2;
using Google.Protobuf;
using System.IO;

Then, we need to create a Protobuf jsonParser that we’ll use to parse the body of HTTP requests:

private static readonly JsonParser jsonParser = 
   new JsonParser(JsonParser.Settings.Default.WithIgnoreUnknownFields(true));

Finally, let’s change the Configure method to parse the request from Dialogflow and simply echo back with a Hello message with the intent name:

app.Run(async (context) =>
{
    WebhookRequest request;

    using (var reader = new StreamReader(context.Request.Body))
    {
         request = jsonParser.Parse<WebhookRequest>(reader);
    }

    var response = new WebhookResponse
    {
        FulfillmentText = "Hello from " + request.QueryResult.Intent.DisplayName
    };

    await context.Response.WriteAsync(response.ToString());
});

Notice how the HTTP request body is parsed as a WebhookRequest. This is the request Dialogflow sends to our code. In response, we create a WebhookResponse with FulfillmentText and send back. Dialogflow will use the FulfillmentText to say what we asked.

Deploy to App Engine

Now, we’re ready to deploy our code to App Engine. We need to publish the app first:

dotnet publish -c Release

This will create a DLL inside the bin/Release/netcoreapp2.0/publish/ folder. Inside this folder, create an app.yaml file for App Engine with the following contents:

env: flex
runtime: aspnetcore

This will tell App Engine that this is an ASP.NET Core app and it should deploy to App Engine flex.

Finally, deploy to App Engine and let’s call this initial version v0:

gcloud app deploy --version v0

Once the deployment is done, you will have an HTTPS endpoint in the form of https://<yourprojectid&gt;.appspot.com that you can use a fulfillment webhook in Dialogflow.

Test with Dialogflow

We’re finally ready to test our fulfillment webhook. In Dialogflow console, specify fulfillment url:

fulfillment

Then, create an intent with some training phrases:

intent

Finally, make sure the fulfillment webhook is enabled for the intent:

webhook

We can use the simulator in Dialogflow console to test out the intent:

hello .net

When we say “Hello .NET”, this triggers the webhook and our webhook code simply echoes back the intent name which is “hello c# intent” in this case.

Obviously, a real world webhook would do much more than a simple reply. For example, you would need to keep track of user sessions and get or create conversations based on those sessions (see DialogflowApp of our app on GitHub). You also need to find a way to match intents to handlers on the server (see Conversation of our app on GitHub on this).

Hopefully, this blog post provided the basics to get started with fulfillment webhook using C# and App Engine.

 

Advertisements

Deploying ASP.NET Core apps on Kubernetes/Container Engine

In my previous post, I talked about how to deploy a containerised ASP.NET Core app to App Engine (flex) on Google Cloud. App Engine (flex) is an easy way to run containers in production: Just send your container and let Google Cloud figure out how to run it at scale. It comes with some nice default features such as versioning, traffic splitting, dashboards and autoscaling. However, it doesn’t give you much control.

Sometimes, you need to create a cluster of containers and control how each container is deployed and scaled. That’s when Kubernetes come into play. Kubernetes is an open source container management platform that helps you to manage a cluster of containers and Container Engine is Kubernetes managed by Google Cloud.

In this cloud minute, I show how to deploy an ASP.NET Core app to Kubernetes running on Container Engine.

If you want to go through these steps yourself, we also have a codelab for you that you can access here.

Deploying ASP.NET Core apps on App Engine

I love how easy it is to deploy and run containerized ASP.NET Core apps on App Engine (flex). So much so that, I created a Cloud Minute recently to show you how, here it is.

It basically involves 3 steps:

  1. Create your ASP.NET Core app using dotnet command line tool inside Cloud Shell and publish your app to get a self-contained DLL.
  2. Containerize your app by creating a Dockerfile, relying on the official App Engine image and pointing to the self-contained DLL of your app.
  3. Create an app.yaml file for App Engine and use gcloud to deploy to App Engine.

That’s it! If you want to go through these steps yourself, we also have a codelab for you that you can access here.

Capture

Google Cloud Next’17

In my previous post, I promised to talk about some good conferences I’m attending or speaking over the coming months. One of those conferences that I’m most excited about is Google Cloud Next’17: Google’s main cloud conference happening March 8-10 in San Francisco.

google-next-logo

Last year, I attended that conference as a Noogler. There were a lot of developers and great technical content. This year’s schedule has just been published and it looks even more exciting, especially if you’re a .NET developer!

First, a shameless plug. I’m speaking at Next’17 and my session is called Take your ASP.NET apps to the next level with Google Cloud.  I will talk about how to migrate existing ASP.NET apps to Google Cloud and what kind of benefits you get by running your ASP.NET apps on Google Cloud. It should be an informative and fun talk for .NET developers.

I’m also excited about Running .NET and containers in Google Cloud Platform session by Jon Skeet and Chris Smith. This session will be about deploying ASP.NET Core apps to App Engine and Kubernetes on Container Engine. ASP.NET Core and Kubernetes are both hugely popular in the development world and I’m so happy that Google Cloud supports ASP.NET Core apps on Kubernetes in a big way.

You probably didn’t know but you can run Windows Server and Microsoft SQL Server on Google Cloud and there’re sessions for both of them on Next’17. Deploying Windows-based infrastructure on Google Compute Engine and Microsoft SQL Server on Google Compute Engine should both be interesting sessions to get to know about all the details.

Apart from all of the great Windows and .NET sessions, sessions on Serverless architectures (cloud functions), Machine Learning, big data processing with Dataflow all sound very interesting. Not to mention, we will have a ton of codelabs at Next for people to get hands-on experience with Google Cloud.

As a .NET developer, I have a lot of reasons to be excited about Next’17. Hope to see some of you there!