Link to home
Start Free TrialLog in
Avatar of Paul Konstanski
Paul KonstanskiFlag for United States of America

asked on

Correct PHP JSON Setup for DialogFlow

I am working with DialogFlow and attempting to set up a JSON object in PHP for use with DialogFlow fulfillment. The process is explained on this page.
https://dialogflow.com/docs/reference/api-v2/rest/Shared.Types/WebhookResponse

My php script looks like this:

$myObj->fulfillmentText = "This is my test message";
$myObj->fulfillmentMessages = json_encode(array("text" => "text response")); 
$myJSON = json_encode($myObj);
echo $myJSON;
exit;

Open in new window


If I send only the first parameter, "fulfillmentText" it works fine because that is sent as a string.
But the second parameter, "fulfillmentMessages" is supposed to be sent as an object, but I'm not doing something right.  I get back this message from DialogFlow.


Fulfillment Text
{
  "fulfillmentText": "Searchby Choice is searchby|theme :: email| :: ",
  "fulfillmentMessages": "{\"text\":\"text response\"}"
}

Fullfillment Response (the error code)
Webhook call failed. Error: Failed to parse webhook JSON response: Expect an array but found: "{\"text\":\"text response\"}".

So I am clearly not setting up the array/object correctly in my php code to be recognized as an "array" in DialogFlow.  So how do I send the parameter "text" as an object array? What do I need to change in my PHP.

This reference page shows the correct setup for this option in dialogflow.
https://dialogflow.com/docs/reference/api-v2/rest/Shared.Types/Message#Text

Thanks in advance for the assistance.
Avatar of David Favor
David Favor
Flag of United States of America image

Provide your actual/full code. The code above is only your test code version.

Provide the version where you set fulfillmentText + fulfillmentMessages (or that's what it sounds like your code is doing).

Although... seems like fulfillmentMessages will be returned from the call, rather than passed through the call.
Avatar of Paul Konstanski

ASKER

Providing additional code will only confuse the issue. The rest of my code works just fine.  The issue is with how to set up a JSON response for an object to be read correctly within DialogFlow.  

DialogFlow wants the JSON to be represented this way:
{
  "fulfillmentText": string,
  "fulfillmentMessages": [
    {
      object(Message)
    }
  ]
}

Open in new window


So when I just send the first part of the JSON object that is to be sent as a string, it works fine.
	$myObj->fulfillmentText = $msg;
	
	$myJSON = json_encode($myObj);
	echo $myJSON;
	exit;

Open in new window


But when I try to send the second part that is supposed to be sent as an object, it fails.

	$myObj->fulfillmentText = $msg;
	$myObj->fulfillmentMessages = json_encode(array("text" => "text response")); 
	
	$myJSON = json_encode($myObj);
	echo $myJSON;
	exit;

Open in new window


So my problem is how I am setting up the "object" to be sent to dialog flow.  It is this part of my code that is not set up correctly.
myObj->fulfillmentMessages = json_encode(array("text" => "text response")); 

Open in new window


So the core question how do I use PHP to end the correct format as indicated in this image below:

User generated image
Instead of:
$myObj->fulfillmentMessages = json_encode(array("text" => "text response"));

try:
$myObj->fulfillmentMessages = [ array("text" => "text response") ];
That gives me an error message of:

Webhook call failed. Error: Failed to parse webhook JSON response: Expect message object but got: "text response".
How about:
$myObj->fulfillmentMessages = [ json_encode(array("text" => "text response")) ];
In a dialog with another programmer, I found this solution:

	$myObj->fulfillmentText = "This is my test message";
	$myObj->fulfillmentMessages = array(
		array(
			"text" => array(
			"text" => array("text response")

			) 
		)
	);
	$myJSON = json_encode($myObj);
	echo $myJSON;
	exit;

Open in new window

Great.  Thanks for posting back.  You can accept your comment as the solution.
The most recent comment by hielo also gave an error:

Webhook call failed. Error: Failed to parse webhook JSON response: Expect message object but got: "{\"text\":\"text response\"}".

The solution I offered in previous comment works...
ASKER CERTIFIED SOLUTION
Avatar of Paul Konstanski
Paul Konstanski
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you all for your comments and questions. It helped me arrive at the solution.