RandomAndy
RandomAndy

Reputation: 354

How to send FulfillmentMessages as part of Dialogflow v2 WebhookResponse?

I'm trying to send back fulfillment messages as part of Dialogflow's v2 API WebhookResponse.

This works:

Sending only a FulfillmentText as part of my response works fine (Testing the app in the Actions on Google Simulator replies with the proper FulfillmentText value) :

func random(c *gin.Context, wr dialogflow.WebhookRequest)  {
    log.Println("Random action detected")

    fullfillment := dialogflow.WebhookResponse{
        FulfillmentText: "foobar",
    }

    c.JSON(http.StatusOK, fullfillment)
}

The JSON being sent back:

{"fulfillment_text":"foobar"}

The response in the Simulator:

{
    "conversationToken": "[]",
    "finalResponse": {
    "richResponse": {
        "items": [
            {
                "simpleResponse": {
                    "textToSpeech": "foobar"
                }
            }
        ]
    }
},
    "responseMetadata": {
    "status": {
        "message": "Success (200)"
    },
    "queryMatchInfo": {
        "queryMatched": true,
            "intent": "24db2044-f2fb-4607-9897-1de757990622"
    }
}
}

This doesn't:

But as soon as I try to send back any actual messages (Text Message, Basic Card, Simple Response, etc.) as part of FulfillmentMessages, the test fails:

func random(c *gin.Context, wr dialogflow.WebhookRequest)  {
    log.Println("Random action detected")

    textMessage := dialogflow.Intent_Message_Text{
        Text: []string{"foo", "bar"},
    }

    fullfillment := dialogflow.WebhookResponse{
        FulfillmentText: "foobar",
        FulfillmentMessages: []*dialogflow.Intent_Message{
            {
                Message: &dialogflow.Intent_Message_Text_{
                    Text: &textMessage,
                },
            },
        },
    }

    c.JSON(http.StatusOK, fullfillment)
}

The JSON being sent back:

{
    "fulfillment_text":"foobar",
    "fulfillment_messages":[
        {
            "Message":{
                "Text":{
                    "text":[
                        "foo",
                        "bar"
                    ]
                }
            }
        }
    ]
}

The response in the Simulator:

{
    "responseMetadata": {
    "status": {
        "code": 10,
            "message": "Failed to parse Dialogflow response into AppResponse because of empty speech response",
            "details": [
            {
                "@type": "type.googleapis.com/google.protobuf.Value",
                "value": "{\"id\":\"917d8ac3-3f0f-4953-b556-4dec27b8bbb8\",\"timestamp\":\"2018-10-22T09:00:45.488Z\",\"lang\":\"en-us\",\"result\":{},\"alternateResult\":{},\"status\":{\"code\":206,\"errorType\":\"partial_content\",\"errorDetails\":\"Webhook call failed. Error: Failed to parse webhook JSON response: Cannot find field: Message in message google.cloud.dialogflow.v2.Intent.Message.\"},\"sessionId\":\"ABwppHHSbrqOCPRp_DAPDLepL6YjSNpbzQ61CIBDTMl99rtRqfaWq-y0HtExb1--k6bcaL4CACQMeiVF3p-x5qk\"}"
            }
        ]
    }
}
}

I am assuming that the JSON that my web service sends back is wrong, since it returns ... Cannot find field: Message ... as part of its response. I am using the proper Golang SDK for Dialogflow though (https://godoc.org/google.golang.org/genproto/googleapis/cloud/dialogflow/v2#WebhookResponse)

This was tested on Actions on Google's Simulator as well as running the actual Google Assistant on a Pixel 2.

Can anybody point me in the right direction what I am doing wrong?

Upvotes: 3

Views: 1719

Answers (1)

Maverick
Maverick

Reputation: 454

As you said, the issue is with the json structure of the response. Below is a working webhook response

{
"fulfillmentMessages": [
    {
        "text": {
            "text": [
                "foo",
                "bar"
            ]
        }
    }
],
"fulfillmentText": "foobar",
"source": "Test"
}
  1. Messages is not present in the actual structure.
  2. fullfillment_text should be fulfillmentText
  3. Outer Text should be text
  4. fullfillment_messages should be fulfillmentMessages

Upvotes: 1

Related Questions