首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用不同的输入循环相同的意图的api.ai

使用不同的输入循环相同的意图的api.ai
EN

Stack Overflow用户
提问于 2017-08-15 02:28:27
回答 2查看 962关注 0票数 3

我正在使用API.AI实现助手应用程序,但现在我发现很难在相同的意图上循环来收集不同的用户输入(如果我的表达式错误,请纠正我,我会详细解释它)。问题是,我有一个元素列表,每次我想将一个元素分配给一个人(通过使用输入Assistant.getArgument()收集)时,我希望它每次都会向用户发出这样的信息:“您想将元素X分配给谁?”(X表示列表中元素的名称)。我目前的实现是,创建一个单独的函数,让它提出问题,然后使用while循环在另一个函数中执行收集输入/赋值,在while body的末尾调用ask函数,但它不起作用,因为API.AI在响应中给出了Not Available。你有什么建议吗?如果有什么不清楚的地方,请告诉我。

这里只是一个简短的代码片段,用来展示问题所在&我想要实现的目标。我想让它在API.AI中请求4次,获取用户输入,并将它们全部存储到输出字符串中。

代码语言:javascript
运行
复制
var output = '';

    function do_sth(assistant){
        let get_name_input = assistant.getArgument('name');
        output = output + get_name_input + '.';
    }

    function test_repeat(assistant){
        for(let i = 0; i < 4; i++){
            assistant.ask('What is the name?');
            do_sth(assistant);
        }
    }
EN

回答 2

Stack Overflow用户

发布于 2017-08-15 07:47:33

问题是,Assistant的编程是一个事件驱动系统(每个意图都是一个事件),您使用assistant.ask()assistant.tell()结束服务器上的事件处理。这会将您的回复发送回用户。然后,ask()将等待另一个事件,而tell()则指示对话已结束。

这意味着你不能把ask()放在一个循环中,也不能把结果存储在一个局部变量中,因为每个答案都会作为一个新事件返回给你(例如,每次都会有一个对你的webhook的新调用)。

这里有一种方法可以做到。它由三个部分组成:

  1. 一个intent (在我的屏幕截图中为name.init),用于首先使用操作name.entry调用webhook并触发循环。
  2. 一个intent (在我的屏幕截图中为name.loop),它在name_loop上下文处于活动状态时进行响应,以获取名称并使用处理name.entry intent的相同操作name.init代码片段将其发送到webhook。

代码

代码语言:javascript
运行
复制
var loopAction = function( assistant ){
  const CONTEXT = 'name_loop';
  const PARAM = 'name';
  const VALUE = 'index';
  const NUM_NAMES = 4;

  // Get the context, which contains the loop counter index, so we know
  // which name we're getting and how many times we've been through the loop.
  var index;
  var context = assistant.getContext( CONTEXT );

  if( !context ){
    // If no context was set, then we are just starting the loop, so we
    // need to initialize it.
    index = 0;

  } else {
    // The context is set, so get the invex value from it
    index = context.parameters[VALUE];

    // Since we are going through the loop, it means we were prompted for
    // the name, so get the name.
    var name = assistant.getArgument( PARAM );

    // Save this all, somehow.
    // We may want to put it back in a context, or save it in a database,
    // or something else, but there are things to be aware of:
    // - We can't save it in a local variable - they will go out of scope
    //   after we send the next reply.
    // - We can't directly save it in a global variable - other users who
    //   call the Action will end up writing to the same place.
    loopSave( index, name );

    // Increment the counter to ask for the next name.
    index++;
  }


  if( index < NUM_NAMES ){
    // We don't have all the names yet, ask for the next one

    // Build the outgoing context and store the new index value
    var contextValues = {};
    contextValues[VALUE] = index;

    // Save the context as part of what we send back to API.AI
    assistant.setContext( CONTEXT, 5, contextValues );

    // Ask for the name
    assistant.ask( `Please give me name ${index}` );

  } else {
    // We have reached the end of the loop counter.

    // Clear the context, making sure we don't continue through the loop 
    // (Not really needed in this case, since we're about to end the
    // conversation, but useful in other cases, and a good practice.)
    assistant.setContext( CONTEXT, 0 );

    // End the conversation
    assistant.tell( `I got all ${index}, thanks!` );
  }
};
票数 2
EN

Stack Overflow用户

发布于 2017-08-16 11:24:29

在不复杂的情况下,如果我正确地理解了您要实现的目标,让我为您提供一个简单的解决方案。

用户可以选择3只宠物,狗、猫和兔子。并被要求以不同的方式命名。你只想用一个目的来实现它,比如说pet_name。操作pet.name的名称。

解决方案非常简单。在这些意图中创建3个参数(并通过选中复选框使所有这些参数都是“必需的”)。3个参数分别是dog_name、cat_name、rabbit_name。

现在启用该意图的实现,并获取web钩子中的所有参数。现在,您可以直接在输出文本中使用它们。比如: outputtext =$dog_name。“给你的小狗起个好名字,告诉我更多”;(你只能在action=="pet.name“的时候激活它)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45680747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档