通过Dialogflow实现在Google Actions上设置上下文

时间:2018-10-02 18:40:04

标签: node.js dialogflow actions-on-google google-assistant-sdk

我正在使用Dialogflow编辑器实现来在Google Assistant上建立对话,但是我不确定如何使用agent.setContext函数。 当我询问“关闭设备”时,第一步成功返回。例如,当用户用“ TV”响应时,就会发生此问题。系统返回另一个上下文,而忽略了我设置的上下文。 当用户直接询问“在厨房里关闭电视”时,系统也可以正常运行。因此,我认为实体定义正确。

对话1 (成功)

  • 在厨房里关闭电视

  • 对。关闭。

对话2 (失败)

  • 关闭我的设备//给出与会话1相同的意图

  • 确定,哪个设备?

  • 电视

  • “我的机器人”目前没有响应。请稍后再试。 //它抛出“必须设置'final_response'”。我认为这是由于通用意图action.intent.TEXT。

我的代码:

下面的代码嵌套在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;
}

所以我尝试了另一个版本,并且相同的问题仍然存在:

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 intent

上下文相同,但意图不同。 The contexts are the same, but the intent is different.

2 个答案:

答案 0 :(得分:1)

如果使用conv,您也可以这样尝试:

app.intent('<INTENT>', conv => {
  conv.ask('<RESPONSE>');
  const parameters = {'param1':param1, 'param2': param2}};
  conv.contexts.set('welcome-context', 5, parameters);
});

并像here一样访问它:

const conv.contexts.get(<context>).parameters[<param>];

答案 1 :(得分:0)

您也可以尝试这种方法,它对我也有用

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);