目前,我正在尝试制定一项行动。
我试图实现重用意图。具体来说,这意味着助理应将清单的前三项给我,如果我要求更多,则应将清单的后三项给我。
我实现了在本地运行的逻辑,但可悲的是,我总是从列表中获得前三项。因此,我尝试使用上下文,在这里我想将索引作为参数传递,在这里我遇到了问题。云功能日志始终给我:
无法读取未定义的属性“参数”。
我正在使用对话框v2 api和Google sdk上的操作。以下链接提供了一个示例,说明如何实现上下文,在这里看不到什么大的不同。 https://developers.google.com/actions/reference/nodejsv2/overview
为此编写的代码如下:
app.intent('Default Welcome Intent', conv => {
conv.ask('Welcome.')
const lifespan = 100;
const parameters = {
index: 0,
};
conv.contexts.set('movieCtx', lifespan, parameters);
});
app.intent('New Cinema Movies Intent', async conv => {
let movieCtx = conv.contexts.get('movieCtx');
let index = movieCtx.parameters[index]
let movieRepository = new MovieRepository();
movieRepository.index = index;
await movieRepository.fetchNextTitle(3).then(function(titles) {
movieCtx.parameters[index] = movieRepository.index;
conv.ask('The 3 movies are:' + titles.join(', '))
return true;
}).catch(function(err) {
console.log(err);
return false;
});
})
我的依赖项:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"actions-on-google": "^2.5.0",
"firebase-admin": "~6.0.0",
"firebase-functions": "^2.0.3",
"pg": "^7.7.1"
},
"devDependencies": {
"eslint": "^4.12.0",
"eslint-plugin-promise": "^3.6.0"
},
"private": true
}
更新:
我也尝试更改以下几行,但这也无济于事:
const parameters = {
movieIndex: 0,
};
let index = movieCtx.parameters['movieIndex']
答案 0 :(得分:0)
行
let index = movieCtx.parameters[index]
应该是
let index = movieCtx.parameters['index']
(您之前没有定义index
,因此它试图获取未定义的引用,这是无效的。)
答案 1 :(得分:0)
我能够找到答案!
const {dialogflow} = require('actions-on-google');
const functions = require('firebase-functions');
const MovieRepository = require('./PostgreSQL/MovieRepository');
let movieRepository = new MovieRepository();
const app = dialogflow({debug: true});
const AppContexts = {
NUMBER: 'number',
}
app.intent('Default Welcome Intent', (conv) => {
const lifespan = 10;
const parameters = {
movieIndex: 0,
};
conv.contexts.set(AppContexts.NUMBER, lifespan, parameters);
conv.ask('Welcome!')
});
app.intent('New Cinema Movies Intent', async (conv) => {
const movieCtx = conv.contexts.get(AppContexts.NUMBER);
await movieRepository.fetchNextTitle(3).then(function(titles) {
movieCtx.parameters.movieIndex = movieRepository.index;
conv.ask('The 3 movie titles are: ' + titles.join(', '));
return true;
}).catch(function(err) {
console.log(err);
return false;
});
})
app.intent('Default Fallback Intent', conv => {
conv.ask('Sorry. Can you repeat?')
})
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
如您所见,您必须在意图上方创建AppContext。有点奇怪,但重要的是您申请
NUMBER: 'number'
到上下文,否则将再次获得未定义的上下文。要访问您的参数,您只需要像下面这样附加您的参数值即可:
movieCtx.parameters.movieIndex