用户已登录,但它的负载给出 "undefined" 作为结果
User logged in but it payload gives "undefined" as the result
我正在使用 Dialog Flow,我需要获取用户 ID(更具体地说,是用户的唯一 ID)。 Google 助手应用程序可以正常登录,但是当我尝试获取唯一 ID 时,结果是 "undefined"。
我在 Google 中搜索了很多,但找不到答案。
我的 Google 助手应用程序最初让用户登录,然后将他的唯一 ID 作为 firebase 数据库 ID,将他的详细信息存储在字段中(如订单详细信息、付款详细信息等)并显示它们当用户调用与之相关的 Intent 时。
我已经声明了在实现中指定的实体和意图。
index.js 的实现--------------------------------
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const {
dialogflow,
SignIn
} = require("actions-on-google");
process.env.DEBUG = 'dialogflow:debug';
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
const app = dialogflow({
clientId:'<CLIENT ID>'
});
app.intent("Sign in user", conv => {
conv.ask(new SignIn("To personalize"));
});
var payload;
app.intent('Get Signin', (conv, params, signin) => {
if (signin.status === 'OK') {
payload = conv.user.profile.payload;
}
}
);
function welcome(agent) {
let conv = agent.conv();
var name = conv.user.access.token;
agent.add(`Welcome to the Bot ${name}! How can I help?`);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
function getOrderDetails(agent) {
const dbRef = db.collection('users').doc("test");
return dbRef.get()
.then(doc => {
if (!doc.exists) {
agent.add('No data found in the database!');
} else {
agent.add("Your order status is "+doc.data().orderStatus);
}
return Promise.resolve('Read complete');
});
}
function getPaymentDetails(agent) {
const dbRef = db.collection('users').doc("test");
return dbRef.get()
.then(doc => {
if (!doc.exists) {
agent.add('No data found in the database!');
} else {
agent.add("Your payment is "+doc.data().paymentStatus);
}
return Promise.resolve('Read complete');
});
}
function getDeliveryDetails(agent) {
const dbRef = db.collection('users').doc("test");
return dbRef.get()
.then(doc => {
if (!doc.exists) {
agent.add('No data found in the database!');
} else {
agent.add("Your delivery Status is "+doc.data().deliveryDetails);
}
return Promise.resolve('Read complete');
});
}
function getAllDetails(agent) {
const dbRef = db.collection('users').doc("test");
return dbRef.get()
.then(doc => {
if (!doc.exists) {
agent.add('No data found in the database!');
} else {
agent.add("Delivery details" + doc.data().deliveryDetails + "\nOrder Status " + doc.data().orderStatus + "\nPayment Details " + doc.data().paymentStatus);
}
return Promise.resolve('Read complete');
});
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('GetDeliveryStatus', getDeliveryDetails);
intentMap.set('GetPaymentDetails', getPaymentDetails);
intentMap.set('GetOrderDetails', getOrderDetails);
agent.handleRequest(intentMap);
});
老实说,我无法弄清楚您的代码如何运行以及您如何发送登录请求。
您正在以一种奇怪的方式混合 actions-on-google 库和 dialogflow-fulfillment 库,设置大部分 Intent Handlers 以与 dialogflow-fulfillment 库一起工作,并且这两个登录相关内容以使用 actions-on-google 库。
但是,您没有将 a-o-g 库设置为由 webhook 触发(因为您设置了 d-f 库来执行此操作),因此永远不会调用这些 Intents。所以它不能发送登录。所以用户没有登录。因此 user.profile
字段永远不会被填充。
此外,仅当您使用 OAuth 方法验证用户而不是 Google 登录时,才会设置 conv.user.access.token
字段。你在上面的评论中指出你没有使用 OAuth,所以你永远不会看到这个集合。
我正在使用 Dialog Flow,我需要获取用户 ID(更具体地说,是用户的唯一 ID)。 Google 助手应用程序可以正常登录,但是当我尝试获取唯一 ID 时,结果是 "undefined"。
我在 Google 中搜索了很多,但找不到答案。
我的 Google 助手应用程序最初让用户登录,然后将他的唯一 ID 作为 firebase 数据库 ID,将他的详细信息存储在字段中(如订单详细信息、付款详细信息等)并显示它们当用户调用与之相关的 Intent 时。
我已经声明了在实现中指定的实体和意图。
index.js 的实现--------------------------------
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const {
dialogflow,
SignIn
} = require("actions-on-google");
process.env.DEBUG = 'dialogflow:debug';
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
const app = dialogflow({
clientId:'<CLIENT ID>'
});
app.intent("Sign in user", conv => {
conv.ask(new SignIn("To personalize"));
});
var payload;
app.intent('Get Signin', (conv, params, signin) => {
if (signin.status === 'OK') {
payload = conv.user.profile.payload;
}
}
);
function welcome(agent) {
let conv = agent.conv();
var name = conv.user.access.token;
agent.add(`Welcome to the Bot ${name}! How can I help?`);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
function getOrderDetails(agent) {
const dbRef = db.collection('users').doc("test");
return dbRef.get()
.then(doc => {
if (!doc.exists) {
agent.add('No data found in the database!');
} else {
agent.add("Your order status is "+doc.data().orderStatus);
}
return Promise.resolve('Read complete');
});
}
function getPaymentDetails(agent) {
const dbRef = db.collection('users').doc("test");
return dbRef.get()
.then(doc => {
if (!doc.exists) {
agent.add('No data found in the database!');
} else {
agent.add("Your payment is "+doc.data().paymentStatus);
}
return Promise.resolve('Read complete');
});
}
function getDeliveryDetails(agent) {
const dbRef = db.collection('users').doc("test");
return dbRef.get()
.then(doc => {
if (!doc.exists) {
agent.add('No data found in the database!');
} else {
agent.add("Your delivery Status is "+doc.data().deliveryDetails);
}
return Promise.resolve('Read complete');
});
}
function getAllDetails(agent) {
const dbRef = db.collection('users').doc("test");
return dbRef.get()
.then(doc => {
if (!doc.exists) {
agent.add('No data found in the database!');
} else {
agent.add("Delivery details" + doc.data().deliveryDetails + "\nOrder Status " + doc.data().orderStatus + "\nPayment Details " + doc.data().paymentStatus);
}
return Promise.resolve('Read complete');
});
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('GetDeliveryStatus', getDeliveryDetails);
intentMap.set('GetPaymentDetails', getPaymentDetails);
intentMap.set('GetOrderDetails', getOrderDetails);
agent.handleRequest(intentMap);
});
老实说,我无法弄清楚您的代码如何运行以及您如何发送登录请求。
您正在以一种奇怪的方式混合 actions-on-google 库和 dialogflow-fulfillment 库,设置大部分 Intent Handlers 以与 dialogflow-fulfillment 库一起工作,并且这两个登录相关内容以使用 actions-on-google 库。
但是,您没有将 a-o-g 库设置为由 webhook 触发(因为您设置了 d-f 库来执行此操作),因此永远不会调用这些 Intents。所以它不能发送登录。所以用户没有登录。因此 user.profile
字段永远不会被填充。
此外,仅当您使用 OAuth 方法验证用户而不是 Google 登录时,才会设置 conv.user.access.token
字段。你在上面的评论中指出你没有使用 OAuth,所以你永远不会看到这个集合。