Google cloud functions DialogflowSDK中间件在解析承诺后返回
我目前正在googleNodeSDK上玩Google cloud functions DialogflowSDK中间件在解析承诺后返回,google-cloud-functions,dialogflow-es,actions-on-google,Google Cloud Functions,Dialogflow Es,Actions On Google,我目前正在googleNodeSDK上玩操作,我正在努力解决如何等待中间件中的承诺得到解决,然后再执行我的意图。我尝试过使用async/await并从中间件函数返回promise,但这两种方法似乎都不起作用。我知道通常情况下,你不会像我在这里所做的那样推翻意图,但这是为了测试发生了什么 const {dialogflow} = require('actions-on-google'); const functions = require('firebase-functions'); const
操作,我正在努力解决如何等待中间件中的承诺得到解决,然后再执行我的意图。我尝试过使用async/await
并从中间件函数返回promise
,但这两种方法似乎都不起作用。我知道通常情况下,你不会像我在这里所做的那样推翻意图,但这是为了测试发生了什么
const {dialogflow} = require('actions-on-google');
const functions = require('firebase-functions');
const app = dialogflow({debug: true});
function promiseTest() {
return new Promise((resolve,reject) => {
setTimeout(() => {
resolve('Resolved');
}, 2000)
})
}
app.middleware(async (conv) => {
let r = await promiseTest();
conv.intent = r
})
app.fallback(conv => {
const intent = conv.intent;
conv.ask("hello, you're intent was " + intent );
});
看起来我至少应该能够返回一个承诺
但我不熟悉typescript,所以我不确定我是否正确阅读了这些文档
有人能建议如何正确地执行此操作吗?例如,现实生活中的一个示例可能是,在继续下一步之前,我需要进行一个DB调用,并等待该调用在我的中间件中返回
我的函数是使用谷歌云函数中的nodejsv8测试版
此代码的输出是实际意图的内容,例如默认的欢迎意图,而不是“已解决”,但没有错误。因此,中间件启动,但在承诺解决之前,它会转移到回退意图上。e、 在设置conv.intent=r
之前,异步的东西真的很适合v2api。对我来说,只有正确使用nodejs8。原因是从V2开始,除非您返回承诺,否则操作将返回空,因为它在对函数的其余部分求值之前已完成。要想弄明白这一点,有很多工作要做,下面是我的一些样本样板,应该可以帮助您:
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {BasicCard, MediaObject, Card, Suggestion, Image, Button} = require('actions-on-google');
var http_request = require('request-promise-native');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
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));
function welcome(agent) {
agent.add(`Welcome to my agent!`);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
function handleMyIntent(agent) {
let conv = agent.conv();
let key = request.body.queryResult.parameters['MyParam'];
var myAgent = agent;
return new Promise((resolve, reject) => {
http_request('http://someurl.com').then(async function(apiData) {
if (key === 'Hey') {
conv.close('Howdy');
} else {
conv.close('Bye');
}
myAgent.add(conv);
return resolve();
}).catch(function(err) {
conv.close(' \nUh, oh. There was an error, please try again later');
myAgent.add(conv);
return resolve();
})})
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('myCustomIntent', handleMyIntent);
agent.handleRequest(intentMap);
});
简要概述您需要什么:
- 你必须归还承诺决议
- 对于HTTP请求,您必须使用“request-promise-native”包
- 您必须升级计划以允许出站HTTP请求()
所以我的问题是关于google sdk上的actions的过时版本。dialogflow firebase示例使用v2.0.0,将其在包中更改为2.2.0。json解决了问题此代码的输出是什么?@AzaTulepbergenov更新。但基本上没有错误,它只是激发了回退意图。如果我在wait promise语句之前将conv.intent
设置为另一个值,它确实设置了它,然后在它进入回退意图时正确地打印出来,那么我已经玩了一点,如果我在回退
intent中使用与您的承诺类似的代码,我可以像在我设计的示例中那样操作conv
对象(仅覆盖意图),但是,如果我尝试使用app.middleware
中的中间件来更改此对象,则这不起作用。事实上,返回承诺然后返回解析函数实际上会导致代理停止工作。错误是:“TypeError:无法读取未定义at函数的属性'parsed'(/srv/node_modules/actions on-google/dist/service/dialogflow/dialogflow.js:115:69)