Google上的操作-处理来自Dialogflow的轮播响应 [英] Actions on Google - handling carousel responses from dialogflow

查看:89
本文介绍了Google上的操作-处理来自Dialogflow的轮播响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用DialogFlow创建了一个简单的Google Assistant界面,其中包含多个我希望能够链接在一起的轮播。但是,每当我触摸轮播选项时,它总是转到指定了actions_intent_OPTION事件的第一个Intent。我可以使用语音命令进入所有屏幕,但是我不确定如何处理触摸命令以将用户发送到正确的Intent。



当前的代码webhook:

  const party ='party'; 
const cocktail ='cocktail';
const SELECTED_ITEM_RESPONSES = {
[party]:您选择了派对,
[cocktail]:您选择了鸡尾酒,
};

function carousel(agent){
//agent.add(`Item selected`);
app.intent('actions.intent.OPTION',(conv,params,option)=> {
let response ='您没有从列表或轮播中选择任何项目';
if(option&& SELECTED_ITEM_RESPONSES.hasOwnProperty(option)){
response = SELECTED_ITEM_RESPONSES [option];
} else {
response ='您从列表中选择了一个未知项目或轮播;
}
conv.ask(response);
});
}

如果我将agent.add()行留在其中,那么我会得到选定的项目 ...,但是如果我尝试使用app.intent代码,它说我只是得到一个空的语音回复。



我试图创建1个名为CarouselHandler的意图可以处理所有菜单选择。当该意图被事件击中时,我使用示例代码来调用carousel()函数。

  let intentMap = new Map (); 
intentMap.set(默认欢迎意图,欢迎);
intentMap.set(默认后备意图,后备);
intentMap.set('CarouselHandler',carousel);
agent.handleRequest(intentMap);


解决方案

您在这里有关于使用选项的几个问题。让我们尝试清除一些问题。



我可以为每个选项触发不同的Intent吗?



否。将选项报告给Dialogflow的方式是,所有选项都将触发相同的Intent。您负责查看发送的选项字符串,并根据需要调用另一个函数。



如前所述,您需要使用事件 actions_intent_OPTION





用于处理此问题的代码可能看起来像这样,尽管还有其他处理方法:

  app .intent('list.reply.click',(conv,params,option)=> {
//获取用户的选择
//将用户的选择与项目的每个键$ b进行比较$ b if(!option){
conv.ask('您没有从列表或轮播中选择任何项目');
} else if(option ==='OPTION_1'){
handleOption1(conv);
} else if(option ==='OPTION_2'){
handleOption2Or3(conv);
} else if(option ==='OPTION_3'){
处理eOption2Or3(conv);
} else {
conv.ask(您从列表中选择了未知项目,或轮播);
}
});

我可以为每个轮播触发不同的Intent吗?



是。为此,在发送轮播时,您将设置一个OutgoingContext并删除为轮播创建的所有其他OutgoingContext(将其寿命设置为0)。然后,您将创建一个具有此上下文作为IncomingContext的Intent。



如果您使用的是on-on-actions,则发送轮播的代码可能类似于以下内容谷歌图书馆

  conv.ask(这里是菜单2); 
conv.ask(新列表({
标题:菜单2,
项:{
OPTION_1:{
标题:选项1,
说明:描述1
},
OPTION_2:{
标题:选项2,
描述:描述2
},
OPTION_3:{
标题:选项3,
说明:描述3
},
}
});
conv.contexts.set( menu_2,99);
conv.contexts.delete( menu_1);
conv.contexts.delete( menu_3);
//也不要忘记添加建议

如果您使用的是dialogflow-fulfillment库,尽管有一些区别,但它是相似的:

  let conv = agent.conv(); 
conv.ask( here is menu 2);
conv.ask(new List({
title: Menu 2,
items:{
OPTION_1 :{{
标题:选项1,
说明:描述1
},
OPTION_2:{
标题: Opti on 2,
描述:描述2
},
OPTION_3:{
标题:选项3,
描述:描述3
},
}
});

agent.add(conv);
agent.setContext({name: menu_1,lifespan:0});
agent.setContext({name: menu_2,lifespan:99});
agent.setContext({name: menu_3,lifespan:0});

如果您使用的是



除了使用不同的Intent名称外,您用于处理此问题的代码与上面类似。



如果处理程序之间有重叠的选项,则它们可以调用相同的函数



我如何以相同的方式处理语音和选项响应?



AoG在某些情况下将使用语音响应来触发该选项。这就是别名的用途。但是,即使如此,如果您拥有可以捕获用户短语的Intent以及与Options一起使用的Intent,那么您所需要做的就是让实现代码调用相同的函数。



代码为什么不起作用?



该行

  app.intent('actions.intent.OPTION',(conv,params,option)=> {

可能不会执行您认为的操作,除非这是Dialogflow中的Intent 的名称,否则字符串 actions .intent.OPTION 不会在您的处理程序中显示。这也是您在Google动作库中注册Intent处理程序的方式。



您似乎也将通过 carousel()函数。请勿执行此操作(这也可能是为什么回复不回来的部分原因正确。)


I've created a simple Google Assistant interface using DialogFlow with several Carousels that I want to be able to chain together. Whenever I touch a carousel option though, it always goes to the first Intent that has the actions_intent_OPTION event specified. I can get to all of my screens using voice commands, but I'm not sure how to process the touch commands to send the user to right Intent.

Current code in webhook:

const party = 'party';
const cocktail = 'cocktail';
const SELECTED_ITEM_RESPONSES = {
  [party]: 'You selected party',
  [cocktail]: 'You selected cocktail',
};

function carousel(agent) {
    //agent.add(`Item selected`);
    app.intent('actions.intent.OPTION', (conv, params, option) => {
      let response = 'You did not select any item from the list or carousel';
      if (option && SELECTED_ITEM_RESPONSES.hasOwnProperty(option)) {
        response = SELECTED_ITEM_RESPONSES[option];
      } else {
        response = 'You selected an unknown item from the list or carousel';
      }
      conv.ask(response);
    });
}

If I leave the agent.add() line in, then I get "Item selected"... but if I try to use the app.intent code, it says I'm just getting an empty speech response.

I was trying to create 1 intent called CarouselHandler to process all the menu selections. I used the sample code to call the carousel() function when that intent gets hit by the event.

  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('CarouselHandler', carousel);
  agent.handleRequest(intentMap);

解决方案

You have several questions in here about using options. Let's try to clear a few things up.

Can I get a different Intent triggered for each option?

No. The way options are reported to Dialogflow is that all options will trigger the same Intent. You're responsible for looking at the option string sent and calling another function if you wish.

As you've noted, you need to create an Intent with the Event actions_intent_OPTION.

Your code to handle this might look something like this, although there are other ways to handle it:

app.intent('list.reply.click', (conv, params, option) => {
  // Get the user's selection
  // Compare the user's selections to each of the item's keys
  if (!option) {
    conv.ask('You did not select any item from the list or carousel');
  } else if (option === 'OPTION_1') {
    handleOption1( conv );
  } else if (option === 'OPTION_2') {
    handleOption2Or3( conv );
  } else if (option === 'OPTION_3') {
    handleOption2Or3( conv );
  } else {
    conv.ask('You selected an unknown item from the list, or carousel');
  }
});

Can I get a different Intent triggered for each carousel?

Yes. To do this, when you send the carousel you will set an OutgoingContext and delete any other OutgoingContexts you created for a carousel (set their lifespan to 0). Then you will create an Intent that has this Context as an IncomingContext.

The code to send a carousel might look something like this if you're using the actions-on-google library

conv.ask("Here is menu 2");
conv.ask(new List({
  title: "Menu 2",
  items: {
    "OPTION_1": {
      title: "Option 1",
      description: "Description 1"
    },
    "OPTION_2": {
      title: "Option 2",
      description: "Description 2"
    },
    "OPTION_3": {
      title: "Option 3",
      description: "Description 3"
    },
  }
});
conv.contexts.set("menu_2",99);
conv.contexts.delete("menu_1");
conv.contexts.delete("menu_3");
// Don't forget to add suggestions, too

If you're using the dialogflow-fulfillment library, it would be similar, although there are a few differences:

let conv = agent.conv();
conv.ask("Here is menu 2");
conv.ask(new List({
  title: "Menu 2",
  items: {
    "OPTION_1": {
      title: "Option 1",
      description: "Description 1"
    },
    "OPTION_2": {
      title: "Option 2",
      description: "Description 2"
    },
    "OPTION_3": {
      title: "Option 3",
      description: "Description 3"
    },
  }
});

agent.add(conv);
agent.setContext({name:"menu_1", lifespan:0});
agent.setContext({name:"menu_2", lifespan:99});
agent.setContext({name:"menu_3", lifespan:0});

If you were using multivocal, the response configuration might look something like this:

{
  Template: {
    Text: "Here is menu 2",
    Option: {
      Type: "carousel",
      Title: "Menu 2",
      Items: [
        {
          Title: "Option 1",
          Body: "Description 1"
        },
        {
          Title: "Option 2",
          Body: "Description 2"
        },
        {
          Title: "Option 3",
          Body: "Description 3"
        }
      ]
    }
  },
  Context: [
    {
      name: "menu_1",
      lifetime: 0
    },
    {
      name: "menu_2",
      lifetime: 99
    },
    {
      name: "menu_3",
      lifetime: 0
    }
  ]
}

The Intent that would capture this option suggestion might look something like this:

Your code to handle this would be similar as above, except using the different Intent name.

If there are overlapping options between the handlers, they could call the same function that actually does the work (again, as illustrated above).

How can I handle voice and option responses the same way?

AoG, in some cases, will use the voice response to trigger the option. This is what the aliases are for. But even beyond this, if you have Intents that catch phrases from the user and an Intent that works with the Options, all you need to do is have the fulfillment code call the same function.

Why doesn't the code work?

The line

app.intent('actions.intent.OPTION', (conv, params, option) => {

Probably doesn't do what you think it does. Unless this is the name for the Intent in Dialogflow, the string actions.intent.OPTION won't be seen in your handler. It is also how you register an Intent handler with the actions-on-google library.

It also looks like you're mixing the dialogflow-fulfillment library way of registering Intent handlers with the actions-on-google library way of registering Intent handlers through your carousel() function. Don't do this. (This may also be part of the cause about why replies aren't getting back correctly.)

这篇关于Google上的操作-处理来自Dialogflow的轮播响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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