Dialogflow es dialogflow'的基于承诺的函数;s动作图

Dialogflow es dialogflow'的基于承诺的函数;s动作图,dialogflow-es,actions-on-google,Dialogflow Es,Actions On Google,通过NodeJS Google Actions SDK设置Google Assistant应用程序的方式如下: 这些似乎是同步函数(根据上提供的文档) pickOption和optionPicked是否可能是异步函数?即,将pickOption实现为 function pickOption(){ var pickOptionPromise = Q.defer(); pickOptionPromise.resolve({ /*Some results here*/ })

通过NodeJS Google Actions SDK设置Google Assistant应用程序的方式如下:

这些似乎是同步函数(根据上提供的文档)

pickOption
optionPicked
是否可能是异步函数?即,将
pickOption
实现为

function pickOption(){
   var pickOptionPromise = Q.defer();
   pickOptionPromise.resolve({
     /*Some results here*/
   });
   return pickOptionPromise.promise;
}

是的,有两个函数在大多数情况下用于将响应返回给助手。
app.ask
app.tell
。这两个函数将您的响应包装成JSON对象并返回给Google


您的
pickOption
optionpick
函数可以异步运行,并在逻辑完成后运行
app.tell

对于初学者,根本不要求您使用
app.handleRequest()
(您甚至不需要使用
app
,但这不是您的问题。)它是一种方便的方法,可以将意图映射到函数,但您可以通过其他方式自由地执行该任务

function handler1( app ){
  // Does the logic asynchronously, but no voice response for some situation.
  // Returns a Promise that resolves to an Object about what we did
}

function handler2( app ){
  // Does the logic asynchronously, but no voice response for some situation.
  // Returns a Promise that resolves to an Object about what we did
}

function chooseHandler( app ){
  // Some logic which may be async, but which returns
  // a Promise that will resolve to either handler1 or 2
}

function sendResponse( app, handlerResult ){
  // Uses the handlerResult object to determine what to
  // send back to the user. This may involve async database calls.
  // Calls either app.ask(), or app.tell(), or a related call.
  // Returns a Promise indicating success
}

chooseHandler( app )
  .then( handler => handler( app ) )
  .then( result  => sendResponse( app, result ) )
  .catch( err => {
    // Log the error or something
  } );
(这是为了说明问题。它没有经过实际测试,也不能保证为特定用途提供最高质量或最佳设计。)

在本例中,
chooseHandler()
通常是同步的,但如果我们需要检查用户以前是否与我们在一起,并在此基础上选择其他处理程序,则可以是异步的。处理程序几乎总是异步的,重要的一点是它们实际上不会选择发送回用户的短语-这在
sendResponse()中完成
,还可能涉及异步的数据库调用

function handler1( app ){
  // Does the logic asynchronously, but no voice response for some situation.
  // Returns a Promise that resolves to an Object about what we did
}

function handler2( app ){
  // Does the logic asynchronously, but no voice response for some situation.
  // Returns a Promise that resolves to an Object about what we did
}

function chooseHandler( app ){
  // Some logic which may be async, but which returns
  // a Promise that will resolve to either handler1 or 2
}

function sendResponse( app, handlerResult ){
  // Uses the handlerResult object to determine what to
  // send back to the user. This may involve async database calls.
  // Calls either app.ask(), or app.tell(), or a related call.
  // Returns a Promise indicating success
}

chooseHandler( app )
  .then( handler => handler( app ) )
  .then( result  => sendResponse( app, result ) )
  .catch( err => {
    // Log the error or something
  } );