Node.js api.ai使用不同的输入循环相同的意图
我正在使用API.AI实现assistant应用程序,但现在我发现很难在相同的意图上循环以收集不同的用户输入(如果我的表达式错误,请更正我,我将详细解释)。问题是,我有一个元素列表,每次我都想为一个人指定一个元素(通过use input Assistant.getArgument()收集),但是,我希望它每次都能像“您想将元素X分配给谁?”这样对用户说话(X指列表中元素的名称).我目前的实现是,创建一个单独的函数,让它提问,然后使用while循环在另一个函数中执行collect输入/赋值,在while body的末尾调用ask函数,但它不起作用。AI给出的答案在响应中不可用。关于如何做,有什么想法吗?如果有什么问题,请告诉我不清楚 这里只是一个简短的代码片段,用于说明问题所在以及我想要实现的目标。我想让它在API.AI中询问4次,获取用户输入,并将它们全部存储到输出字符串中Node.js api.ai使用不同的输入循环相同的意图,node.js,dialogflow-es,actions-on-google,google-assist-api,Node.js,Dialogflow Es,Actions On Google,Google Assist Api,我正在使用API.AI实现assistant应用程序,但现在我发现很难在相同的意图上循环以收集不同的用户输入(如果我的表达式错误,请更正我,我将详细解释)。问题是,我有一个元素列表,每次我都想为一个人指定一个元素(通过use input Assistant.getArgument()收集),但是,我希望它每次都能像“您想将元素X分配给谁?”这样对用户说话(X指列表中元素的名称).我目前的实现是,创建一个单独的函数,让它提问,然后使用while循环在另一个函数中执行collect输入/赋值,在wh
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);
}
}
var输出=”;
职能(助理){
让get_name_input=assistant.getArgument('name');
输出=输出+获取名称+输入+';
}
功能测试重复(助理){
for(设i=0;i<4;i++){
助手。问(‘叫什么名字?’);
做某事(助手);
}
}
问题在于,为助手编程是一个事件驱动的系统(每个意图都是一个事件),您可以使用Assistant.ask()
或Assistant.tell()
在服务器上结束对事件的处理。这会将您的回复发送回用户。ask()
将等待另一个事件,而tell()
表示对话结束
这意味着您不能将ask()
放入循环中,也不能将结果存储在局部变量中,因为每个答案都将作为新事件返回给您(即,每次对webhook的新调用)
下面是一种方法。它由三部分组成:
name.entry
调用webhook并触发循环name\u loop
上下文处于活动状态时响应,以获取名称并使用相同的操作name.entry
将其发送到webhookname.entry
intent的代码片段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!` );
}
};
var loopAction=函数(助手){
常量上下文='name_loop';
常量参数='name';
常量值='索引';
const NUM_NAMES=4;
//获取包含循环计数器索引的上下文,这样我们就知道
//我们得到了哪个名字,我们经历了多少次循环。
var指数;
var context=assistant.getContext(context);
如果(!上下文){
//如果没有设置上下文,那么我们只是开始循环,所以
//需要初始化它。
指数=0;
}否则{
//上下文已设置,因此从中获取invex值
索引=上下文。参数[值];
//因为我们正在经历循环,这意味着我们被提示
//名字,所以你要知道名字。
var name=assistant.getArgument(PARAM);
//无论如何,把这一切都保存下来。
//我们可能想把它放回上下文中,或者保存到数据库中,
//或者别的什么,但是有一些事情需要注意:
//-我们无法将其保存在局部变量中-它们将超出范围
//在我们发送下一个回复之后。
//-我们不能直接将其保存在全局变量中-其他用户
//调用该操作将最终写入同一位置。
loopSave(索引、名称);
//递增计数器以请求下一个名称。
索引++;
}
如果(索引
在不涉及复杂问题的情况下,如果我正确理解您想要实现的目标,请允许我为您提供一个简单的解决方案
用户可以选择3只宠物,狗、猫和兔子。用户需要对它们进行不同的命名。您希望通过一个目的来实现它,比如说pet_name.name of The action pet.name
解决方案非常简单。按照这些意图创建3个参数(并通过选中该框使所有参数都成为“必需的”)。3个参数是dog\u name、cat\u name、rabbit\u name
现在启用实现该目的,并获取web钩子中的所有参数。现在您可以在输出文本中直接使用它们。例如:outputtext=$dog_name.“是您小狗的好名字。告诉我更多”;(您只能在action==“pet.name”时激活它).你能发布不起作用的代码和你的api.ai意图的任何屏幕截图吗?听起来你好像在试图从用户那里收集一些不同的值?你可能想看看我们的私人厨师视频,该视频展示了如何收集信息,以及来自相同意图的后续问题:@capital posted.@WaynePiekarski Yeah I a我已经看过了。不过我想问题不一样了。嗨,囚犯,我有点理解你的意思,我自己也试过了,但效果不太好。