0

I'm struggling to get my dialogflow Cx webhook response accepted by dialogflow. I am testing my agent from the dialogflow console and am successfully reaching my webhook endpoint and extracting the intent and switching to the correct place in my PHP code. I then build a simple text response and echo it. My response is always met with an error in the console debug window: "ErrorCode": "INVALID_ARGUMENT", "ErrorMessage": "Failed to parse webhook response:" My webhook code is:

header('Content-Type: application/json');
$jsonRequest = file_get_contents('php://input');
$data = json_decode($jsonRequest, true);
$intent = $data["intentInfo"]["displayName"];

file_put_contents("response1.log", print_r(json_encode($data), true));

switch ($intent){
      
case "myname":
$data = array (
  "fulfillment_response" => 
  array (
    "messages" => 
    array (
      0 => 
      array (
        "text" => 
        array (
          "text" => "Intent -> My name is Harry"
        ),
      ),
    ),
  )
);
}      
   echo json_encode($data);
   file_put_contents("response1.log", print_r(json_encode($data), true));
   die();

My response message json which is sent looks clean and is:

{
    "fulfillment_response": {
        "messages": [
            {
                "text": {
                    "text": "Intent -> My name is Harry"
                }
            }
        ]
    }
}

The error shown in the diloagflow console debug window is the following:

"Status": {
                "ErrorMessage": "Failed to parse webhook response: [{\"fulfillment_response\":{\"messages\":[{\"text\":{\"text\":\"Intent My name is Harry\"}}]}}]",
                "ErrorCode": "INVALID_ARGUMENT"              }
            }

OK, I have made some progress with this and the error has cleaned up considerably but it still looks like the quote characters " are being escaped \ for some reason. I have tried several flags with json_encode but to no avail. All suggestions are welcome...

TRB
  • 3
  • 2

1 Answers1

0

The main reason for this error is that you are returning the payload in wrong format please use the following and I hope it will work:

{
  "fulfillment_response": {
        "messages": [
            {
                "text": {
                    "text": [
                        "Webhook Response"
                    ]
                }
            }
        ]
    }
} 
Aymal
  • 191
  • 12
  • 1
    Thank you Aymal.... that worked! I adjusted my PHP code which builds the dialogflow response message to the following: $data = [ "fulfillment_response" => ["messages" => [["text" => ["text" => ["My name is Harry"]]]], ], ]; – TRB Sep 26 '22 at 23:42
  • Feel free to ask any questions thanks for marking the answer correct. – Aymal Sep 27 '22 at 01:29