试图按照示例代码进行操作
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错误(206)格式错误的响应”
我的代码有什么问题,除了温度转换器Trivia似乎是较旧的版本,我在哪里可以获取更多示例以学习。
答案 0 :(得分:1)
您在代码中有多个对变量name
的引用,例如
const termRef = conv.contexts.get(name);
但您无处定义name
是什么。
设置上下文时,将其设置为字面名称为“ name”的上下文,但是您尝试使用存储在name
变量中的参数来设置它,尚未定义:
conv.contexts.set('name', 5, name);
我猜后者应该是类似的东西
conv.contexts.set('name', 5, contextParameters);
因为您定义了contextParameters
,但是从不使用它们。而且,您的意思是称呼上下文为“名称”,因为那是您在该调用中使用的名称。