如何确定通过Webhook请求登录DiallogFlow的用户? [英] How can I identify a user logged into DiallogFlow via webhook request?
问题描述
我正在使用Dialogflow,想知道是否通过向机器人发出的用户问题来确定哪个用户在问这个问题.
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.
附加的部分代码用于读取已接收的数据. 我尝试使用Google文档(" https://developers.google .com/assistant/identity/google-sign-in#java ),但未成功.
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);
推荐答案
DialogFlow通常只标识会话.提供数据以唯一标识用户是客户端的一部分,通常包含在有效负载中.
DialogFlow generally only identifies the session. Providing data to uniquely identify the user is part of the client and usually included in the payload.
例如,可以这样提取来自Google Assistant的登录用户(需要System.IdentityModel.Tokens.Jwt
程序包):
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;
您需要按照您已经在文章中详细介绍的方式配置项目链接.然后可以通过Google Assistant集成设置将任何DialogFlow意图标记为需要登录",或使用帮助者意图(可选登录)(请参见
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).
这篇关于如何确定通过Webhook请求登录DiallogFlow的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!