Paul Konstanski
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/doc s/referenc e/api-v2/r est/Shared .Types/Web hookRespon se
My php script looks like this:
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.
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/doc s/referenc e/api-v2/r est/Shared .Types/Mes sage#Text
Thanks in advance for the assistance.
https://dialogflow.com/doc
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;
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/doc
Thanks in advance for the assistance.
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:
So when I just send the first part of the JSON object that is to be sent as a string, it works fine.
But when I try to send the second part that is supposed to be sent as an object, it fails.
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.
So the core question how do I use PHP to end the correct format as indicated in this image below:
DialogFlow wants the JSON to be represented this way:
{
"fulfillmentText": string,
"fulfillmentMessages": [
{
object(Message)
}
]
}
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;
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;
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"));
So the core question how do I use PHP to end the correct format as indicated in this image below:
Instead of:
$myObj->fulfillmentMessage s = json_encode(array("text" => "text response"));
try:
$myObj->fulfillmentMessage s = [ array("text" => "text response") ];
$myObj->fulfillmentMessage
try:
$myObj->fulfillmentMessage
ASKER
That gives me an error message of:
Webhook call failed. Error: Failed to parse webhook JSON response: Expect message object but got: "text response".
Webhook call failed. Error: Failed to parse webhook JSON response: Expect message object but got: "text response".
How about:
$myObj->fulfillmentMessage s = [ json_encode(array("text" => "text response")) ];
$myObj->fulfillmentMessage
ASKER
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;
Great. Thanks for posting back. You can accept your comment as the solution.
ASKER
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...
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you all for your comments and questions. It helped me arrive at the solution.
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.