简体   繁体   English

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

[英]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.我认为这是因为通用意图 act​​ions.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.

相关问题 Dialogflow/Google Actions 实现(使用 Google App Engine 和存储)返回错误 4:DEADLINE EXCEEDED,尽管很简单 - Dialogflow/Google Actions fulfillment (using Google App Engine & Storage) returning error 4: DEADLINE EXCEEDED, despite being simple 如何使用对话流实现 node.js 在谷歌操作上实现二叉决策树? - How to implement a binary decision tree on google actions using dialogflow fulfillment node.js? google-actions-如何在nodejs中的Promise中使用mongo-on-google库在对话框流实现中进行mongodb查询? - How to use Promise in node js for mongodb query in dialogflow fulfillment with actions-on-google library? 如何在不使用 webhook 客户端的情况下设置 dialogflow fulfillment response json 上下文? - How to set dialogflow fulfillment response json context without using webhook client? 如何使用对话流实现从列表响应的上下文中提取参数 - how to extract parameters from the context for list response using dialogflow fulfillment 如何为 Actions on Google 使用异步履行 - How to use async fulfillment for Actions on Google Google上的操作不起作用,但它们在dialogflow中起作用 - actions on google not working but they does in dialogflow 关于dialogflow的意图不会在Google的操作上更新 - Intents on dialogflow not updating on actions on google 如何使用履行代码在 Dialogflow 中设置 agent.parameters? - How to set agent.parameters in Dialogflow using fulfillment code? 实现中的DialogFlow-未定义DeepLink - DialogFlow in Fulfillment - DeepLink is not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM