如何为上下文构建实现?

How to build fulfillment for context?

尝试按照示例代码进行操作

app.intent('intent1', (conv) => {
  const lifespan = 5;
  const contextParameters = {
    color: 'red',
  };
  conv.contexts.set('context1', lifespan, contextParameters);
  // ...
});

app.intent('intent2', (conv) => {
  const context1 = conv.contexts.get('context1');
  const contextParameters = context1.parameters;
  // ...
});

app.intent('intent3', (conv) => {
  conv.contexts.delete('context1');
  // ...
});

这是我的代码...

app.intent('ask_recipe_intent', (conv, {name}) => {
  const term = name.toLowerCase();
  const termRef = collectionRef.doc(term);
  //Set the context to store the parameter information
  const lifespan =5;
  const contextParameters = {
    name: name,
  };
  conv.contexts.set('name', 5, name);

  return termRef.get()
    .then((snapshot) => {
      const {city, name, count} = snapshot.data();
      return termRef.update({count: count+1})
        .then(() => {
          conv.ask(`Here you go, ${name}, ${city}. ` +
            `do you want to check all the Ingredients needed?`);
        });
    }).catch((e) => {
      console.log('error:', e);
      conv.close('Sorry, I did not catch you, can you say again or try another word.');
    });
});

app.intent('yes_list_ingredient_intent', conv => {
  const termRef = conv.contexts.get(name);
  const contextParameters = name.parameters;
  conv.ask(`The ingredients of ${name} includes ${ingredientall}. ` +
            `do you want to add to shopping list or go to steps of cooking?`);
});

app.intent('no_steps2cook_intent', conv => {
  conv.contexts.delete(name);
  conv.ask(`The steps to cook for ${name} as following ${stepsall}. ` +
            `enjoy cooking`);
});

但是得到了"Webhook error (206) Malformed Response"

我的代码有什么问题,我在哪里可以得到更多的示例来学习,除了 Temperature Converter Trivia 似乎是旧版本。

您的代码中有多个对变量 name 的引用,例如

const termRef = conv.contexts.get(name);

但你没有定义 name 是什么。

当您设置上下文时,您将其设置为字面上命名为 "name" 的上下文,但您试图使用存储在 name 变量中的参数来设置它,并且未定义:

conv.contexts.set('name', 5, name);

我猜后者应该是这样的

conv.contexts.set('name', 5, contextParameters);

因为您定义了 contextParameters,但永远不要使用它们。你的意思是调用你的上下文 "name" 因为那是你在那个调用中使用的名称。