Dialogflow fulfillment with C# and App Engine

Mete Atamel
Sep 24, 2018 · 5 min read

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.

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.

  1. 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.

dotnet new web -n fulfillment
dotnet add package Google.Cloud.Dialogflow.V2 --version 1.0.0-beta02
using Google.Cloud.Dialogflow.V2;
using Google.Protobuf;
using System.IO;
private static readonly JsonParser jsonParser = new JsonParser(JsonParser.Settings.Default.WithIgnoreUnknownFields(true));
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());
});

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
env: flex
runtime: aspnetcore
gcloud app deploy --version v0

Test with Dialogflow

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


Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Mete Atamel

Written by

Developer Advocate at Google

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.