Making DialogFlow v2 DetectIntent Calls w/ C# (including input context)

北城以北 提交于 2019-12-07 16:42:56

问题


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

来源:https://stackoverflow.com/questions/52083499/making-dialogflow-v2-detectintent-calls-w-c-sharp-including-input-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!