bruno
bruno

Reputation: 91

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. When I ask "Turn off my device" the first step returns successfully. The problem happens when the user responds with "TV", for example. 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):

Conversation 2 (fail):

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

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 intent

The contexts are the same, but the intent is different. The contexts are the same, but the intent is different.

Upvotes: 1

Views: 2930

Answers (2)

Vikas Patidar
Vikas Patidar

Reputation: 213

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

Upvotes: 0

Abhinav Tyagi
Abhinav Tyagi

Reputation: 5266

If using conv, you may also try like this:

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

Upvotes: 2

Related Questions