简体   繁体   English

如何在 Google Dialogflow 中使用参数修改上下文

[英]How to use Parameters to modify Context in Google Dialogflow

We are running an experiment, where I need to manipulate the dialog flow responses based on the participant's ID.我们正在运行一个实验,我需要根据参与者的 ID 操作对话流响应。 My thought is there is a way to set the Output Context based on a parameter value.我的想法是有一种方法可以根据参数值设置输出上下文。

For example, we have a prompt that asks for the participant's id.例如,我们有一个询问参与者 ID 的提示。 This matches an intent with a "participantID" parameter.这匹配具有“participantID”参数的意图。 Now what I would like to do is set the output context to be the value of the "participantID" parameter.现在我想做的是将输出上下文设置为“participantID”参数的值。 Then I could set the input context to be a specific "participantID".然后我可以将输入上下文设置为特定的“参与者 ID”。

Any parameters set on an intent will be kept in the output context and recoverable in posterior follow-up intents within the lifespan of such context.在意图上设置的任何参数都将保留在输出上下文中,并可在此类上下文的生命周期内在后续的后续意图中恢复。

Through the Dialogflow UI you can reference them in the initial response as $<parameter-name> .通过 Dialogflow UI,您可以在初始响应中将它们引用为$<parameter-name>
For example, assuming your parameter name is id , the response can be: Welcome player $id,... .例如,假设您的参数名称是id ,则响应可以是: Welcome player $id,...

For values in the context in posterior follow-up intents use #<context-name>.<parameter-name> .对于后验后续意图中上下文中的值,请使用#<context-name>.<parameter-name>
For example, in a second follow-up which has parameter answer and input context id-followup use Your answer has been $answer, if you are not player #id-followup.id please let me know your actual id .例如,在具有参数answer和输入上下文id-followup的第二个后续操作中, Your answer has been $answer, if you are not player #id-followup.id please let me know your actual id

If you need to use the Fulfillment Webhook I recommend a structure like the one illustrated here , ie a structure like:如果您需要使用 Fulfillment Webhook,我推荐一种类似于此处所示结构的结构,即如下结构:

//The user inputs a participant ID (name of the parameter is id)
function answers(agent){
    const id = agent.parameters.id;
    agent.add(`So your id is ${id}`);
    agent.add(`Any extra questions...`);
    agent.setContext({
      name: 'question',
      lifespan: 5,  // Amount of follow-up intents this context will be kept
      parameters: {'cid': id},
    });
}
// Next functions that use the participant id as input context
function answers2(agent){
    // Get the context and from it extract the id value
    const cont = agent.getContext('question');
    const particular_id = cont.parameters.cid;
    // The rest of your code

Some documentation you may find useful includes Input and output contexts , Parameter reference ,...您可能会发现一些有用的文档包括输入和输出上下文参数参考、...

Depending on what version of dialogflow-fulfillment you are using, I'm talking about 0.6.1 here.根据您使用的 dialogflow-fulfillment 版本,我在这里谈论的是 0.6.1。 The syntax for setting context is:设置上下文的语法是:

 agent.context.set({
    name: "ctx_name",
    lifespan: 5,
    parameters: {'cid': id},
  });

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

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