Harry Stuart
Harry Stuart

Reputation: 1929

Setting output contexts in Dialogflow

Using the C# client library for Dialogflow, I am trying to set the output context in a webhook response. However, the output context field is read only. This is my code:

WebhookResponse response = new WebhookResponse
   {
       FulfillmentText = "This is a test",
       OutputContexts = ... //Regardless of what I try and set OutputContexts to be, I get the error "property or indexer 'WebhookResponse.OutputContexts' cannot be assigned to -- it is read only"
   };

How do I set the output context?

Upvotes: 0

Views: 1245

Answers (2)

Jeroen Timmermans
Jeroen Timmermans

Reputation: 31

I know this is an old question but just in case someone has the same problem.

You can not assign a new list to OutputContexts, you have to add them to the list:

For example:

response.OutputContexts.Add(new Context
            {
                Name = $"{request.Session}/your_context",
                LifespanCount = 1
            });

Upvotes: 2

sid8491
sid8491

Reputation: 6800

I think the response json which you are forming is wrong.
Below is the correct json response which you need to send:

{
    "fulfillmentText = "This is a test",
    "outputContexts": [
        {
            "name": "projects/project_id/agent/sessions/session_id/contexts/your_context",
            "lifespanCount": 5,
            "parameters": {
                "foo": "bar",
                "foo1": "bar1"
            }
        }
    ],
    "followupEventInput": {
        "name": "even_name"
    }
}

Upvotes: 0

Related Questions