使用C#进行DialogFlow v2 DetectIntent调用(包括输入上下文)

时间:2018-08-29 17:52:07

标签: c# google-api dialogflow

因此,我终于找到了一种成功进行检测意图调用并提供输入上下文的方法。我的问题是这是否是正确 (或最佳) 的方法:

(是的,我知道您可以只调用DetectIntent(代理,会话,查询),但是我必须根据请求提供输入上下文)

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;

注意:我包括了显式ShutDownAsync(不在异步调用中),因为在尝试重新部署WebAPI项目时(并且仅在执行此代码之后),我遇到了一些文件锁定问题)。

谢谢 克里斯

更新4/25::我使用此方法最基本的方法是将用户名集成到意图响应中: Intent response example

也可以从webhook /内嵌实现index.js中读取它:

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

0 个答案:

没有答案