[英]Set context on Google Actions through Dialogflow fulfillment
I'm using the Dialogflow editor fulfillment to build a conversation on Google Assistant, but I'm not sure how to use the agent.setContext
function.我正在使用 Dialogflow 编辑器实现在 Google 助理上建立对话,但我不确定如何使用
agent.setContext
函数。 When I ask "Turn off my device" the first step returns successfully.当我询问“关闭我的设备”时,第一步成功返回。 The problem happens when the user responds with "TV", for example.
例如,当用户回复“TV”时就会出现问题。 The system returns with another context, ignoring the one I set.
系统返回另一个上下文,忽略我设置的上下文。 When the user directly asks "Turn off my TV in the kitchen" the system also works perfectly.
当用户直接询问“关闭厨房里的电视”时,系统也能完美运行。 Because of it I think that the entities are correctly defined.
正因为如此,我认为实体是正确定义的。
Conversation 1 (success) :对话 1 (成功) :
Turn off my TV in the kitchen关掉我厨房里的电视
Right.对。 Turning off.
关闭。
Conversation 2 (fail) :对话 2 (失败) :
Turn off my device //Gives the same intent that Conversation 1关闭我的设备 // 给出与对话 1 相同的意图
OK, which device?好的,哪个设备?
TV电视
"My bot" isn't responding right now. “我的机器人”现在没有响应。 Try again soon.
请稍后再试。 //It throws "'final_response' must be set".
//它抛出“必须设置'final_response'”。 I think it's because of generic intent actions.intent.TEXT.
我认为这是因为通用意图 actions.intent.TEXT。
My code:我的代码:
The code below is nested in exports.dialogflowFirebaseFulfillment = functions.https.onRequest
and the intent is called by intentMap.set('smarthome.device.switch.off', setDeviceOff);
下面的代码嵌套在
exports.dialogflowFirebaseFulfillment = functions.https.onRequest
,意图由intentMap.set('smarthome.device.switch.off', setDeviceOff);
const agent = new WebhookClient({ request: request, response: response });
function setDeviceOff(agent) {
const device = agent.parameters.device;
const room = agent.parameters.room;
const context= 'device-switch'; //I tried 'smarthome.device.switch.off' too
let resp = commandDevice(device, room, 'Off', agent, context);
return resp;
}
function commandDevice(device, room, cmd, agent, context) {
var conv = agent.conv();
if(device===''){
conv.ask("OK, which device?");
}else if (room===''){
conv.ask("Got it. of what room?");
}else{
conv.ask("Right. Turning off.");
}
agent.setContext({
name:context,
lifespan: 5, //Tried 0, 4 and 3 too
parameters:{
'device':device,
'room':room
}
});
agent.add(conv);
return true;
}
So I tried another version and the same problem persists:所以我尝试了另一个版本,同样的问题仍然存在:
const app = dialogflow();
app.intent('welcome', conv =>{
conv.ask("Welcome to auto obari. What can I do for you?");
});
app.intent('Default Fallback Intent',conv =>{
conv.ask("I didn't understand, can you try again?");
});
app.intent('smarthome.device.switch.off', (conv,{device, room})=> {
const context= 'device-switch';
if(device===''){
conv.ask("OK, which device?");
}else if (room===''){
conv.ask("Got it. of what room?");
}else{
conv.ask("Right. Turning off.");
}
const parameters = {'device':device, 'room': room};
conv.contexts.set(context, 5, parameters);
});
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app); //I tried exports.factsAboutGoogle, but it threw a error too.
The contexts are the same, but the intent is different.语境相同,但意图不同。
If using conv
, you may also try like this:如果使用
conv
,您也可以这样尝试:
app.intent('<INTENT>', conv => {
conv.ask('<RESPONSE>');
const parameters = {'param1':param1, 'param2': param2}};
conv.contexts.set('welcome-context', 5, parameters);
});
and access it like here :并像这里一样访问它:
const conv.contexts.get(<context>).parameters[<param>];
you can try this method too it worked for me你也可以试试这个方法它对我有用
const AppContexts = {CONTEXT_ONE: 'context-one'};
const app = dialogflow();
In current intent (for output context)
conv.contexts.set(AppContexts.CONTEXT_ONE, 1);
In next intent (for input context )
const context = conv.contexts.get(AppContexts.CONTEXT_ONE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.