Harshit17
Harshit17

Reputation: 63

Dialogflow: Set multiple contexts using fulfillment library (node.js) in Dialogflow

I'm trying to set/change multiple contexts based on user's response. For this, I am trying to set output context by agent.setContext() using fulfillment library through inline editor (node.js). I want to delete contexts set by intent in dialogflow and add new context in Dialogflow's output contexts.

It works fine when setting one output context by calling with agent.setContext() in node.js but it throws error when I pass a list of multiple contexts in agent.setContext() as shown below:

Note: I have given 3 output contexts (context_name_1, context_name_2, context_name_3) in the Dialogflow Intent's Output Context

Code Snippet:

function intent_name(agent) {
    ... // defined text response for user
    ...
    agent.setContext([{
            'name':'context_name_1',
            'lifespan': 10
            }, 
          {
            'name':'context_name_2',
            'lifespan': 0
            }, 
          {
            'name':'context_name_3',
            'lifespan': 0
            },
          {
            'name':'context_name_4',
            'lifespan': 3
            }]);
    ....
    }

I get error as:

Error: context must be provided and must have a name
    at WebhookClient.setContext (/srv/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:343:13)
    at prepaidRecharge (/srv/index.js:45:15)
    at WebhookClient.handleRequest (/srv/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:303:44)
    at exports.dialogflowFirebaseFulfillment.functions.https.onRequest (/srv/index.js:92:9)
    at cloudFunction (/srv/node_modules/firebase-functions/lib/providers/https.js:57:9)
    at /worker/worker.js:783:7
    at /worker/worker.js:766:11
    at _combinedTickCallback (internal/process/next_tick.js:132:7)
    at process._tickDomainCallback (internal/process/next_tick.js:219:9)

Any help would be appreciated.

Upvotes: 4

Views: 1459

Answers (1)

Clynton Caines
Clynton Caines

Reputation: 41

Set them individually...

agent.setContext({ name: 'context_name_1', lifespan: 10, parameters: { city: 'Rome' }});
agent.setContext({ name: 'context_name_2', lifespan: 11, parameters: { continent: 'eu' }});

Or using the new way:

agent.context.set('context_name_1', 10, {'param1' : 'abc'});
agent.context.set('context_name_2', 11, {'param2' : 2});

Upvotes: 4

Related Questions