Making DialogFlow v2 DetectIntent Calls w/ C# (inc

2019-07-13 16:29发布

问题:

So I finally figured out a way to successfully make detect intent calls and provide an input context. My question is whether or not this is the CORRECT (or best) way to do it:

(And yes, I know you can just call DetectIntent(agent, session, query) but I have to provide a input context(s) depending on the request)

var query = new QueryInput
    {
    Text = new TextInput
    {
        Text = model.Content,
        LanguageCode = string.IsNullOrEmpty(model.Language) ? "en-us" : model.Language,
    }
};

var commonContext = new global::Google.Cloud.Dialogflow.V2.Context
{
    ContextName = new ContextName(agent, model.sessionId, "my-input-context-data"),
    LifespanCount = 3,
    Parameters = new Struct
    {
        Fields = {
            { "Source", Value.ForString(model.Source) },
            { "UserId" , Value.ForString(model.UserId.ToString())},
            { "Name" , Value.ForString(model.FirstName)}
        }
    }
};      

var request = new DetectIntentRequest
{
    SessionAsSessionName = new SessionName(agent, model.sessionId),
    QueryParams = new QueryParameters
    {
        GeoLocation = new LatLng {Latitude = model.Latitude, Longitude = model.Longitude},
        TimeZone = model.TimeZone ?? "MST"
    },
    QueryInput = query
};
request.QueryParams.Contexts.Add(commonContext);


// ------------

var creds = GetGoogleCredentials("myCredentials.json");
var channel = new Grpc.Core.Channel(SessionsClient.DefaultEndpoint.Host, creds.ToChannelCredentials());
var client = SessionsClient.Create(channel);

var response = client.DetectIntent(request);

channel.ShutdownAsync();

return response;

Note: I included the explicit ShutDownAsync (it's not in an async call) because I was getting some file locking issues when attempting to re-deploy the WebAPI project (and only after having executed this code).

Thanks Chris

Updated 4/25: The most basic way I use this is to integrate the user's name into intent responses:

It can also be read from within the webhook/inline fulfillment index.js:

const name = request.body.queryResult && request.body.queryResult.outputContexts && request.body.queryResult.outputContexts[0].parameters.Name