Google-cloud-php: Dialogflow response with output context crashes PHP/Symfony server

Created on 17 Jul 2019  路  10Comments  路  Source: googleapis/google-cloud-php

Hi, this is my first bug report, please tell me if anything's wrong with it.
I'm not even sure if this is a bug or simply an error on my part but I've been trying to solve this for three days and I have run out of ideas.

Environment details

  • OS: Debian GNU/Linux 8 (jessie)
  • PHP version: 7.2.14 (cli)
  • Package name and version: google/cloud-dialogflow v0.8.4

Steps to reproduce

  1. Create a basic assistant in Dialogflow
  2. Add an intent "Welcome" with no output context
  3. Add an intent "Help" with an output context
  4. Add an intent "Fallback" which resets the context
  5. Train the assistant and set it up for use (check that the assistant works fine with the Dialogflow console)
  6. Create a basic PHP/Symfony project using the Google Dialogflow SDK (see code below)
  7. Test the "Welcome" intent : everything works fine
  8. Test the "Help" intent : server error 500
  9. Test the "Fallback" intent : server error 500

Code example

<?php

namespace AppBundle\Controller;

use Ramsey\Uuid\Uuid;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Google\Cloud\Dialogflow\V2\SessionsClient;
use Google\Cloud\Dialogflow\V2\TextInput;
use Google\Cloud\Dialogflow\V2\QueryInput;

class DefaultController extends Controller
{
    /**
     * @Route("/messenger", name="homepage")
     *
     */
    public function indexAction(Request $request)
    {
        return $this->render('@App/messenger.twig', array());
    }

    /**
     * @param Request $request
     *
     * @Route("/messenger/call/{intent}", options={"expose"=true}, name="call_assistant")
     * @throws \Google\ApiCore\ValidationException
     * @throws \Google\ApiCore\ApiException
     * @throws \Exception
     */
    public function callAssistantAction(Request $request, $intent) {
        $sessionDialogFlow = $this->get('session')->get('dialogFlow_session');
        if ($sessionDialogFlow === null) {
            $sessionDialogFlow = Uuid::uuid4()->toString();
            $this->get('session')->set('dialogFlow_session', $sessionDialogFlow);
        }

        $sessionClient = new SessionsClient(array(
            'credentials' => realpath($this->getParameter('kernel.root_dir'). "/../web/authentDialogFlow.json")
        ));

        $session = $sessionClient->sessionName("<my session name>", $sessionDialogFlow);
        $textInput = new TextInput();
        $textInput->setText($intent);
        $textInput->setLanguageCode('fr-FR');

        $queryInput = new QueryInput();
        $queryInput->setText($textInput);

        $response = $sessionClient->detectIntent($session, $queryInput); // <- this line is where the crash happens
        $queryResult = $response->getQueryResult();
        $fulfillmentText = $queryResult->getFulfillmentText();

        return new JsonResponse(array("response" => $fulfillmentText));
    }
}

Output for intent "Welcome"

{
  "queryText": "hey",
  "action": "input.welcome",
  "parameters": {},
  "fulfillmentText": "Salutations !",
  "fulfillmentMessages": [
    {
      "text": {
        "text": [
          "Salutations !"
        ]
      },
      "lang": "fr"
    }
  ],
  "intent": {
    "id": "<id>",
    "displayName": "Default Welcome Intent",
    "priority": 500000,
    "events": [
      "WELCOME"
    ],
    "action": "input.welcome",
    "messages": [
      {
        "text": {
          "text": [
            "Hi! How are you doing?",
            "Hello! How can I help you?",
            "Good day! What can I do for you today?",
            "Greetings! How can I assist?"
          ]
        },
        "lang": "en"
      },
      {
        "text": {
          "text": [
            "Salut !",
            "Bonjour !",
            "Salutations !",
            "Bienvenue !"
          ]
        },
        "lang": "fr"
      }
    ]
  },
  "intentDetectionConfidence": 1,
  "languageCode": "fr",
  "slotfillingMetadata": {
    "allRequiredParamsPresent": true
  },
  "id": "<id>",
  "sessionId": "<id>",
  "timestamp": "2019-07-15T07:41:28.778Z",
  "source": "agent",
  "webhookStatus": {
    "webhookEnabledForAgent": true
  },
  "agentEnvironmentId": {
    "agentId": "<id>",
    "cloudProjectId": "<id>"
  }
}

Output for intent "Help"

{
  "queryText": "aide",
  "parameters": {},
  "fulfillmentText": "<long text>",
  "fulfillmentMessages": [
    {
      "text": {
        "text": [
          "<long text>"
        ]
      },
      "lang": "fr"
    }
  ],
  "intent": {
    "id": "<id>",
    "displayName": "Help",
    "priority": 500000,
    "messages": [
      {
        "text": {
          "text": [
            "<long text>"
          ]
        },
        "lang": "fr"
      }
    ]
  },
  "intentDetectionConfidence": 1,
  "languageCode": "fr",
  "slotfillingMetadata": {
    "allRequiredParamsPresent": true
  },
  "id": "<id>",
  "sessionId": "<id>",
  "timestamp": "2019-07-15T13:05:25.255Z",
  "source": "agent",
  "webhookStatus": {
    "webhookEnabledForAgent": true
  },
  "agentEnvironmentId": {
    "agentId": "<id>",
    "cloudProjectId": "<id>"
  }
}

Stackoverflow link

https://stackoverflow.com/questions/57042124/dialogflow-response-with-output-context-crashes-php-symfony-server

(Alas no one answered yet)

Thanks for your help !

dialogflow question

Most helpful comment

Hi @jdpedrie ,
Thank you so much for the tip. bcmath was pre-installed on the server but not enabled. I added it to my php.ini and it's finally working !!

All 10 comments

Thanks for the report @Mania-1024, let's see what we can do to get you back on track.

Would you be able to share the error output you get when the application crashes?

Hi @dwsupplee ,
Unfortunately the only output I get is very generic :

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8" />
    <title>An Error Occurred: Internal Server Error</title>
</head>

<body>
    <h1>Oops! An Error Occurred</h1>
    <h2>The server returned a "500 Internal Server Error".</h2>

    <div>
        Something is broken. Please let us know what you were doing when this error occurred. We will fix it as soon as possible. Sorry for any inconvenience caused.
    </div>
</body>

</html>

I can't seem to catch the error with a try/catch block either, the catch block seems ignored, maybe because it's an http request... (I'm not used to PHP or Symfony though) And I didn't find any useful log on my server either.

Is there any solution for this. I am facing the same issue here. It works fine when call it locally, but just not printing out anything on the website when put it into the server.

Any update ? :( This is a really big problem for me... I can't figure out why it doesn't work, it makes no sense.

@Mania-1024, @boshng95 - taking a look today.

I set up a fresh symfony project and attempted to replicate this, but was not able to.

Would you be able to wrap the network request in a try/catch block so you could capture the error message to help give me a little more to go off of?

try {
    $response = $sessionClient->detectIntent($session, $queryInput);
} catch (\Exception $ex) {
    // capture the exception here
}

@dwsupplee Thank you for your time :)
I had issues logging the error because as a total rookie, I was still working in the production environment.
So, here is the caught error :

<br />
<b>Fatal error</b>: Uncaught Symfony\Component\Debug\Exception\UndefinedFunctionException: Attempted to call function &quot;bccomp&quot; from namespace &quot;Google\Protobuf\Internal&quot;. in <my root directory>/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:900
Stack trace: #0 <my root directory>/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php(1218): Google\Protobuf\Internal\Message-&gt;convertJsonValueToProtoValue(5, Object(Google\Protobuf\Internal\FieldDescriptor)) #1 <my root directory>/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php(1167):
Google\Protobuf\Internal\Message-&gt;mergeFromArrayJsonImpl(Array) #2 <my root directory>/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php(815): Google\Protobuf\Internal\Message-&gt;mergeFromJsonArray(Array) #3 <my root directory>/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php(1211):
Google\Protobuf\Internal\Message-&gt;convertJsonValueToProtoValue(Array, Object(Google\Protobuf\Internal\FieldDes in <b><my root directory>/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/Resources/views/layout.html.twig</b> on line <b>15</b><br
/>

Does that ring a bell to you ?

Hi @Mania-1024,

It looks like your server is missing the bcmath PHP extension. In Debian, you should be able to install it via apt-get install php-bcmath.

Hi @jdpedrie ,
Thank you so much for the tip. bcmath was pre-installed on the server but not enabled. I added it to my php.ini and it's finally working !!

Great!

For future reference, we're tracking this issue here.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dwsupplee picture dwsupplee  路  7Comments

codeflorist picture codeflorist  路  5Comments

Najtmare picture Najtmare  路  6Comments

Google-K picture Google-K  路  6Comments

lakano picture lakano  路  5Comments