【发布时间】:2018-12-09 11:12:17
【问题描述】:
我正在使用带有 DialogFlow 的 actions-on-google github nodejs 应用程序。我试图弄清楚如何进行一个耗时超过 5 秒的异步 api 调用,并在响应准备好时将响应返回给用户,考虑到如果未从意图收到响应,google 上的操作将返回 Malformat 错误5 秒内。 这是我的代码的简单代码sn-p:
app.intent('first-intent', async (conv: any) => {
conv.ask('Please wait while we make our long api call...');
await myPrivateFunction();
})
// I have put API_RESPONSE_RECEIVED as Events in DialogFlow
app.intent('second-intent', (conv: any) => {
console.log('This is second-intent');
var response = conv.data.apiResponse;
conv.ask(response);
})
function myPrivateFunction(): Promise<void> {
utils.apiCall().then(apiResponse => {
console.log('api response received');
conv.data.apiResponse = apiResponse;
conv.followup('API_RESPONSE_RECEIVED');
});
}
在我的 Firebase 日志中,我可以看到“请稍候,我们正在进行长时间的 api 调用...”和“收到 api 响应”,但没有看到“这是第二意图”。如果我在conv.ask('Please wait while we make our long api call...') 之后将conv.followup('API_RESPONSE_RECEIVED') 放在api 调用之外,我会看到“这是第二意图”。所以,app.followup 看起来不错,显然问题在于我如何处理承诺,但我不知道如何解决它。
我在我的开发环境中使用针对 es5 的 TypeScript。所以,我可以在我的 api 调用中使用await/async。但是,使用 await 会导致格式错误,因为 api 调用需要超过 5 秒。
【问题讨论】:
标签: actions-on-google dialogflow-es