简体   繁体   English

如何在对话流中触发对另一个意图的响应的意图

[英]How to trigger a intent on response of another intent in dialogflow

app.intent('Delivery', conv => {
  conv.ask("What a great day for Delivery!  We can provide more information on any of the categories shown below.");
  return axios.get(url).then((success) => {
    let items = {};
    for (let i of success.data.data) {
      let item = {};
      item.synonyms = [i.name];
      item.title = `${i.name}`;
      item.image = new Image({
        url: `${i.image}`,
        alt: 'Image alternate text',
      });
      item.description = `${i.description}`;
      items[i.id] = item;
    }
    conv.ask(new List({ title: 'Food Categories', items }));

  }).catch((error) => {
    conv.ask("Error in category");
  });
});

I have two intents in Dialogflow:我在 Dialogflow 中有两个意图:

  1. Delivery送货
  2. Product产品

In the delivery intent, there are categories coming from API dynamically.在交付意图中,有来自 API 的动态类别。

On the selection of a category, I'm just simply want to trigger the product intent, so that the user can select products from there.在选择一个类别时,我只是想触发产品意图,以便用户可以从那里获得 select 产品。

How I can trigger that product intent on the selection of category.我如何在选择类别时触发该产品意图。

Your best options is to move the product intent logic to its own class or function.您最好的选择是将产品意图逻辑移至其自己的 class 或 function。 By doing this you make the code available for other intents and you won't have to copy and paste any code.通过这样做,您可以使代码可用于其他意图,并且您不必复制和粘贴任何代码。 So it makes your code more flexible and easier to read.因此,它使您的代码更灵活,更易于阅读。

Whether you choose a function or class really is up to you.您选择 function 还是 class 真的取决于您。 If you are just creating simple responses a function is enough, but if you find yourself importing/requiring the same classes over and over again, a class might be better.如果您只是创建简单的响应,则 function 就足够了,但如果您发现自己一遍又一遍地导入/需要相同的类,则 class 可能会更好。

Here is an example of a welcome intent class in NodeJS with Typescript:这是 NodeJS 中带有 Typescript 的欢迎意图 class 的示例:

/**
 * Welcome intent class
 */
export default class WelcomeIntent {
  invoke(conv: DialogflowConversation): DialogflowConversation {

    const greeting = this.getTimeOfDayGreeting();
    const response = "Welcome to my test app. How can I help?"

    const hasScreen = this.contextService.getContext(conv, "actions_capability_screen_output");

    if (hasScreen) {
      conv.ask(new Suggestions("What can I do?"));
    }

    conv.ask(greeting);
    return conv.ask(response);
  }

  private getTimeOfDayGreeting(): string {
    const currentHour = new Date().getHours();

    if (currentHour >= 6 && currentHour < 12) {
      return "Good morning.";
    } else if (currentHour >= 12 && currentHour < 18) {
      return "Good afternoon.";
    } else if (currentHour >= 18 && currentHour <= 23) {
      return "Good evening.";
    } else {
      return "Hello.";
    }
  }
}

Each time I want to welcome the user I just return welcomeIntent.invoke() and it welcomes the user.每次我想欢迎用户时,我只返回welcomeIntent.invoke()并欢迎用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM