Error in Google Assistant, while error in Dialogflow

I created a chatbot that informs the user about the members of my (extended) family and where they live. I created a small MySQL database that stores this data, and I retrieve it using a PHP script when appropriate, depending on the user interaction with the chatbot.

My chatbot contains two intentions in addition to Default Fallback Intentand Default Welcome Intent:

  • Names
  • Location_context

The first intention ( Names) is learned in phrases such as "Who is John Smith?" and has an output context (called contextwith a duration of 10 questions). A possible answer to this question is: "John is my uncle." The second intention ( Location_context) is learned in phrases such as "Where does he live?". and has an input context (from Names). A possible answer to this question is: "John lives in New York."

The ledge Namescontains two parameters:

  • parameter names: first name, last name.
  • : @ sys.given-name, @ sys.last-name.
  • value: $ given-name, $ last-name.

These two parameters represent the fully qualified name specified by the user. The hold Location_contextdoes not contain any parameters.

The PHP script looks like this:

<?php

$dbServername = '******************';
$dbUsername = '******************';
$dbPassword = '******************';
$dbName = '******************';
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

// error_reporting(E_ALL);
// ini_set('display_errors', 'on');

header('Content-Type: application/json');
$method = $_SERVER['REQUEST_METHOD'];

if($method == 'POST'){
    $requestBody = file_get_contents('php://input');
    $json = json_decode($requestBody);

    $action = $json->result->action;
    $first_name = $json->result->contexts[0]->parameters->{'given-name'};
    $last_name = $json->result->contexts[0]->parameters->{'last-name'};
    $lifespan = $json->result->contexts[0]->lifespan;

    $sql = "SELECT * FROM family WHERE name LIKE '%$first_name%$last_name%';";
    $result = mysqli_query($conn, $sql);
    $resultCheck = mysqli_num_rows($result);
    if ($resultCheck > 0) {
       while ($row = mysqli_fetch_assoc($result)) {
            $person = $row;
       }

       switch ($action) {
           case 'Name':
               $speech= "$first_name is my" . $person["name"] . ".";
               break;  
           case 'Location':
               $speech = "$first_name is living in {$person["location"]}.";
               break;
           default:
               $speech = "Please ask me something more relevant to my family";
               break;
       } 
    }
    else {

        $speech = "Sorry, $first_name $last_name is not a member of my family.";

    }

    $response = new \stdClass();
    $response->speech = $speech;
    $response->displayText = $speech;
    $response->source = "agent";
    echo json_encode($response);
}
else
{
    echo "Method not allowed";
}
?>

Dialogflow, , . " ?" : " - ". : " ?" : " -". json Dialogflow :

{
  "id": "*****************************",
  "timestamp": "2018-04-04T08:26:39.993Z",
  "lang": "en",
  "result": {
    "source": "agent",
    "resolvedQuery": "Where is he living"
    "action": "Location",
    "actionIncomplete": false,
    "parameters": {},
    "contexts": [
      {
        "name": "context",
        "parameters": {
          "given-name.original": "John",
          "last-name.original": "Smith",
          "given-name": "John",
          "last-name": "Smith"
        },
        "lifespan": 9
      }
    ],
    "metadata": {
      "intentId": "*****************************",
      "webhookUsed": "true",
      "webhookForSlotFillingUsed": "false",
      "webhookResponseTime": 93,
      "intentName": "Location_context"
    },
    "fulfillment": {
      "speech": "John is living in New York.",
      "displayText": "John is living in New York.",
      "messages": [
        {
          "type": 0,
          "speech": "John is living in New York."
        }
      ]
    },
    "score": 1
  },
  "status": {
    "code": 200,
    "errorType": "success",
    "webhookTimedOut": false
  },
  "sessionId": "*****************************"
}

, ( Talk to my test app) Google, , " -". . . -, $first_name ( ) "-" - , . , $first_name $last_name ( ) mysql - .

, json Google Assistant, Dialogflow. , Google Assistant $lifespan 0 ( , ) $first_name $last_name json- , Dialoglow , , , json, . Google Assistant actions_capability_screen_output $json->result->contexts[0]->name , , , Dialogflow $json->result->contexts[0]->name context ( ).

, json contexts Google Assistant :

"contexts": [
          {
            "name": "actions_capability_screen_output",
            "parameters": {},
            "lifespan": 0
          }
        ]

, , json DIalogflow contexts:

"contexts": [
      {
        "name": "context",
        "parameters": {
          "given-name.original": "John",
          "last-name.original": "Smith",
          "given-name": "John",
          "last-name": "Smith"
        },
        "lifespan": 9
      }
    ]

Google Assistant context json, Dialoglow?

json Google Assistant, Dialogflow?

+4
3

. .

?

ini_set('display_errors', 'on'); , Dialogflow. Dialogflow, Google, , .

, ?

, - error_log(). , , JSON, Dialogflow , , .

-

error_log( $request_body );

, JSON, Dialogflow. (, , HTTP error_log, ), , .

, , . JSON ?

Google , , . ( ), . , originalRequest, JSON.

- , , . .

, Dialogflow -, Google Actions?

. "" "agentToAssistantDebug", . , , .

context Google?

, . , , , context[0] "". context, -

error_log( $json->result->context );

, , . actions_capability_screen_output, Google, , , . , actions_capability_audio_output, , . context, .

given-name last-name?

, .

, - , , .

, . (, .)

, , ?

context , , . .

Google 0?

. Google ( ), , AoG, 0 ( , , ). 0, (, ).

?

- , .

+2

- POST -

, , Expected BEGIN_OBJECT but was STRING at line 2 column 1 path $., :

error_reporting(E_ALL);
ini_set('display_errors', 'on');

, , , " -", " -" - / . , . - , - Google Assistant , Names Location_context, Dialogflow . - $first_name $last_name ( " ?" ), / .

.

- POST IT -

, Location_context:

  • : -, .
  • : @sys.given-name, @sys.last-name.
  • : # context.given-name, # context.last-name.

given-name last-name Names Location_context.

, json Google Assistant contexts :

"contexts": [
      {
        "name": "actions_capability_screen_output",
        "parameters": {
          "given-name.original": "John",
          "last-name.original": "Smith",
          "given-name": "John",
          "last-name": "Smith"
        },
        "lifespan": 0
      }
    ]

( json Google Assistant, , json )

, PHP script ( " -" ) ( " ?" ).

, , Dialogflow - Location_context...

, Google Assistant , lifespan 0 ( , )...

+1

PHP, . , , Dialogflow JSON, .

:

Expected BEGIN_OBJECT but was STRING at line 2 column 1 path $.

, JSON, Dialogflow, JSON.

It seems that you are already doing this at the top of your PHP answer, but maybe you should check that the JSON response is not unusual, which will lead to this error above.

0
source

Source: https://habr.com/ru/post/1695686/


All Articles