从Dialogflow创建慢速回复 [英] Creating a slow reply from Dialogflow

查看:62
本文介绍了从Dialogflow创建慢速回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Dialogflow Webhook,该Webhook可以缓慢响应用户,因此更像是有人在另一端并且需要几秒钟的回复.

I want to create a Dialogflow webhook that responds to the user slowly, so it more feels like someone is on the other end and takes a few seconds to reply.

我正在使用内置的代码编辑器,并且可以创建一个Intent处理程序(请参见代码),但我只是不知道如何使它响应速度较慢.

I'm using the built-in code editor, and can create an Intent handler (see code), but I just don't know how to get it to reply slower.

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });

  function welcome (agent) {
    agent.add(`I'm replying too quickly!`);
  }

  function fallback (agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
  }

  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  agent.handleRequest(intentMap);
});

推荐答案

老实说,需要慢一点的回复是一件不希望的事情.但是最简单的方法是使用 setTimeout()并延迟一点时间.(不要延迟太久-超过5或10秒,Dialogflow就会超时.)

Needing to reply slower is rarely a desired thing, to be honest. But the easiest way to do so is to use setTimeout() and delay for a little bit. (Don't delay too long - more than 5 or 10 seconds and Dialogflow will timeout.)

使用 setTimeout()的要点是,处理程序将需要返回Promise.因此,您需要将对 setTimeout() agent.add()的调用包装在Promise处理程序中.执行此操作的函数可能类似于:

The catch with using setTimeout(), however, is that the handler will need to return a Promise. So you'll need to wrap the call to setTimeout() and the agent.add() in a Promise handler. A function that does this might look something like:

function respondSlowly( agent, msg, ms ){
  return new Promise( resolve => {
    setTimeout( () => {
      agent.add( msg );
      resolve();
    }, ms );
  });
}

然后,您将从处理程序中调用此方法,提供代理,消息以及等待回复的毫秒数:

You would then call this from your handler, providing the agent, the message, and how many milliseconds to wait to reply:

function welcome( agent ){
  return respondSlowly( agent, `Hi there, slowly`, 2000 );  // Wait 2 seconds to reply
}

这篇关于从Dialogflow创建慢速回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆