目前我正在尝试开发一个动作。
我正在尝试实现重用意图。具体来说,就是助理给我一个列表的前三项,如果我要求更多,那么它应该给我列表的后三项。
我实现了本地工作的逻辑,但遗憾的是我总是从列表中获得前三项。因此,我尝试使用上下文,我想将索引作为参数传递,在这里我遇到了问题。云函数日志总是给我:
无法读取未定义的属性“参数”。
我正在使用 dialog 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']
我找到了答案!
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句