How can I identify a user logged into DiallogFlow via webhook request?

雨燕双飞 提交于 2020-01-16 08:35:11

问题


I am using Dialogflow and would like to know if through the questions of a user to a bot it is possible to identify which user is asking this question.

Attached is a section of the code for reading the data already received. I tried using the google documentation ('' https://developers.google.com/assistant/identity/google-sign-in#java "), but was unsuccessful.

WebhookRequest request;

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

var pas = request.QueryResult.Parameters;
var queryText = request.QueryResult.QueryText;
var response = new WebhookResponse();

StringBuilder sb = new StringBuilder();

//interactionDAO.SaveInteration(new Interaction(Guid.NewGuid(), "google", queryText));

var intent = request.QueryResult.Intent.DisplayName;
var listaObjetos = await _service.DetectIntentAsync(new[] { queryText }, intent);

foreach (var item in listaObjetos)
{

    var convertItem = JsonConvert.DeserializeObject<Fulfillment>(item.ToString());
    if (!String.IsNullOrWhiteSpace(convertItem.FulfillmentText))
    {
        sb.Append(convertItem.FulfillmentText);
    }

    if (convertItem.Parameters != null && convertItem.Parameters.ContainsKey("date-time"))
    {
        sb.Append(convertItem.Parameters["date-time"]);
    }

    //sb.Append(item);
}

response.FulfillmentText = sb.ToString();

return Json(response);

回答1:


Look for "session" in the JSON you receive in your webhook from DialogFlow, it is a unique identifier for the conversation.

Usually it has a format like this:

"session": "projects/${PROJECTID}/agent/sessions/${SESSIONID}"

Just extract the SESSIONID from the last part.

You can find more about DialogFlow Webhook JSON format here:

https://developers.google.com/assistant/actions/build/json/dialogflow-webhook-json




回答2:


DialogFlow generally only identifies the session. Providing data to uniquely identify the user is part of the client and usually included in the payload.

For example, a signed in user from Google Assistant can be extracted like this (requires the System.IdentityModel.Tokens.Jwt package):

WebhookRequest request;

if (!request.OriginalDetectIntentRequest.Payload.Fields.ContainsKey("user"))
{
  throw new ArgumentException("Payload does not contain user.");
}

string idToken = request.OriginalDetectIntentRequest.Payload.Fields["user"]
  .StructValue.Fields["idToken"].StringValue;

var userInfo = new JwtSecurityTokenHandler().ReadJwtToken(idToken).Payload;
if (!userInfo["iss"].ToString().EndsWith("accounts.google.com")
  || !userInfo["aud"].ToString().Equals("((your_action_id))")
{
  throw new SecurityException("Issuer or authentication token do not match expected value.");
}

string accountName = userInfo["email"].ToString();
if (string.IsNullOrEmpty(accountName))
{
  throw new ArgumentException("Id token does not contain mail address.");
}

return accountName;

You need to configure the project as detailed in the article you already linked. It is then possible to mark any DialogFlow intent as "Sign-in required" via the Google Assistant integration settings or use the helper intent for optional sign-in (see this question for details on implementing the helper).



来源:https://stackoverflow.com/questions/58546922/how-can-i-identify-a-user-logged-into-diallogflow-via-webhook-request

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