actions-on-google - 如何使用 Diagflow 识别唯一用户
问题描述
我正在尝试制作一个助手应用程序,并且正在使用 firebase 的云 Firestore 服务将响应发送回我的应用程序,并使用 webhook 作为实现。我根据本文档在请求 JSON 中使用了“会话”参数,并将 fulfilmentText 作为响应发送给用户。但是每当用户启动应用程序时,都会创建一个我不想要的新会话。我只是想要,我的数据库中的每个用户只需要一个条目,那么如何使用对话流来实现这一点。
在 Alexa Skill 中,我们将 deviceId 作为参数,通过它我们可以唯一地识别用户,而不管会话 ID 是什么,但在对话流请求 JSON 中是否有任何参数。如果没有,那么没有它如何完成这个任务。
我从 Dialogflow 获得的请求 JSON 中有一个 userID,所以我可以使用 userId 还是应该使用 userStorage,前提是请求 JSON 中没有 userStorage 参数。
request.body.originalDetectIntentRequest { source: 'google', version: '2', payload: { surface: { capabilities: [Object] },
inputs: [ [Object] ],
user:
{ locale: 'en-US',
userId: 'ABwppHG5OfRf2qquWWjI-Uy-MwfiE1DQlCCeoDrGhG8b0fHVg7GsPmaKehtxAcP-_ycf_9IQVtUISgfKhZzawL7spA' },
conversation:
{ conversationId: '1528790005269',
type: 'ACTIVE',
conversationToken: '["generate-number-followup"]' },
availableSurfaces: [ [Object] ] } }
编辑:谢谢@Prisoner 的回答,但我无法发送响应中生成的随机 ID 并在有效负载中设置。下面是我生成 uuid 并将其存储在 firestore 中的代码。我在下面的代码中做错了什么,因为为返回用户生成了新的 uuid,因此响应显示为在数据库中找不到文档。我想我没有适当地发送 uuid。请帮忙。
exports.webhook = functions.https.onRequest((request, response) => {
console.log("request.body.queryResult.parameters", request.body.queryResult.parameters);
console.log("request.body.originalDetectIntentRequest.payload", request.body.originalDetectIntentRequest.payload);
let userStorage = request.body.originalDetectIntentRequest.payload.user.userStorage || {};
let userId;
console.log("userStorage", userStorage);
if (userId in userStorage) {
userId = userStorage.userId;
} else {
var uuid = require('uuid/v4');
userId = uuid();
userStorage.userId = userId
}
console.log("userID", userId);
switch (request.body.queryResult.action) {
case 'FeedbackAction': {
let params = request.body.queryResult.parameters;
firestore.collection('users').doc(userId).set(params)
.then(() => {
response.send({
'fulfillmentText' : `Thank You for visiting our ${params.resortLocation} hotel branch and giving us ${params.rating} and your comment as ${params.comments}.` ,
'payload': {
'google': {
'userStorage': userStorage
}
}
});
return console.log("resort location", params.resortLocation);
})
.catch((e => {
console.log('error: ', e);
response.send({
'fulfillmentText' : `something went wrong when writing to database`,
'payload': {
'google': {
'userStorage': userStorage
}
}
});
}))
break;
}
case 'countFeedbacks':{
var docRef = firestore.collection('users').doc(userId);
docRef.get().then(doc => {
if (doc.exists) {
// console.log("Document data:", doc.data());
var dat = doc.data();
response.send({
'fulfillmentText' : `You have given feedback for ${dat.resortLocation} and rating as ${dat.rating}`,
'payload': {
'google': {
'userStorage': userStorage
}
}
});
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
response.send({
'fulfillmentText' : `No feedback found in our database`,
'payload': {
'google': {
'userStorage': userStorage
}
}
});
}
return console.log("userStorage_then_wala", userStorage);
}).catch((e => {
console.log("Error getting document:", error);
response.send({
'fulfillmentText' : `something went wrong while reading from the database`,
'payload': {
'google': {
'userStorage': userStorage
}
}
})
}));
break;
}
解决方案
您有几个选择,具体取决于您的确切需求。
简单:用户存储
Google 提供了一个对象,当它可以识别用户时,该userStorage
对象会在对话中持久存在。这使您可以在需要跟踪用户何时返回时存储自己的标识符。
最简单的方法是在userStorage
调用 webhook 时检查对象的标识符。如果它不存在,请使用 v4 UUID 之类的东西创建一个并将其保存在userStorage
对象中。
如果您使用的是 actions-on-google 库,则代码可能如下所示:
let userId;
// if a value for userID exists un user storage, it's a returning user so we can
// just read the value and use it. If a value for userId does not exist in user storage,
// it's a new user, so we need to generate a new ID and save it in user storage.
if (userId in conv.user.storage) {
userId = conv.user.storage.userId;
} else {
// Uses the "uuid" package. You can get this with "npm install --save uuid"
var uuid = require('uuid/v4');
userId = uuid();
conv.user.storage.userId = userId
}
如果您使用的是 dialogflow 库,则可以使用上述内容,但首先需要此行:
let conv = agent.conv();
如果您使用的是多声库,它会为您完成以上所有工作,并将在 path 下的环境中提供一个 UserID User/Id
。
如果您直接处理 JSON,并且使用的是 Dialogflow v2 协议,则可以通过检查originalDetectIntentRequest.payload.user.userStorage
JSON 请求对象来获取 userStorage 对象。您将payload.google.userStorage
在 JSON 响应中设置对象。代码与上面类似,可能看起来像这样:
let userStorage = body.originalDetectIntentRequest.payload.user.userStorage || {};
let userId;
// if a value for userID exists un user storage, it's a returning user so we can
// just read the value and use it. If a value for userId does not exist in user storage,
// it's a new user, so we need to generate a new ID and save it in user storage.
if (userId in userStorage) {
userId = userStorage.userId;
} else {
// Uses the "uuid" package. You can get this with "npm install --save uuid"
var uuid = require('uuid/v4');
userId = uuid();
userStorage.userId = userId
}
// ... Do stuff with the userID
// Make sure you include the userStorage as part of the response
var responseBody = {
payload: {
google: {
userStorage: JSON.stringify(userStorage),
// ...
}
}
};
请注意代码的第一行 - 如果userStorage
不存在,请使用空对象。在您发送包含第一次在其中存储某些内容的响应之前,它不会存在,这将发生在此代码的最后几行中。
高级:帐户链接
您可以请求用户使用 Google 登录 登录您的操作。这可以在最简单的情况下使用语音来完成,并且只会在第一次中断流程。
在此之后,您的 Action 将获得一个 JWT,其中包含他们的 Google ID,您可以将其用作他们的标识符。
如果您使用的是 actions-on-google 库,则可以使用以下行从解码的 JWT 中获取 ID:
const userId = conv.user.profile.payload.sub;
在 multivocal 库中,从解码的 JWT 得到的 ID 在环境路径下可用User/Profile/sub
已弃用:匿名用户 ID
您将在 StackOverflow 上看到一些引用匿名用户 ID 的答案。Google 已弃用此标识符,它并不总是验证回访用户的可靠方法,并将于 2019 年 6 月 1 日将其删除。
此代码目前仍在发送中,但将从 2019 年 6 月 1 日开始删除。
推荐阅读
- python - 如何让 python 能够访问网络
- python - 为在 Gunicorn 上运行的 Flask-SQLAlchemy 应用程序选择 DB pool_size
- sql - 如何使用带有 MONEY 数据类型的 WHERE 子句?
- javascript - 包含的页面 javascript 范围
- python - Python 正则表达式忽略空行
- javascript - 单击时自动重新加载延迟
- r - 关于tidygraph中复制失败的问题
- .net - 如何使用默认基地址配置 WCF 服务?
- reactjs - 如何在 React 应用程序中提交时将一个对象从一个数组移动到另一个数组
- android - 使用 Android Studio 将 SQLite 中的数据显示为动态卡片