带有输出上下文的 Dialogflow 响应崩溃 PHP/Symfony 服务器

Dialogflow response with output context crashes PHP/Symfony server

TL;DR

我的 PHP/Symfony 服务器在 Dialogflow 发送带有输出上下文的响应时崩溃,但在响应没有任何输出上下文时工作正常。 为什么会这样,如何防止我的服务器崩溃?

一些上下文

我正在处理一个几个月前离开公司的同事所做的旧项目。 该项目使用 Dialogflow 和 PHP/Symfony 服务器来创建聊天机器人。 早在 1 月份,该项目运行良好,但当我上周尝试对其进行测试时,我发现我们的服务器已无法挽回地从主机上移除。 我重新上传并重新安装了服务器代码,但我不能 100% 确定备份代码与托管代码完全相同。

正确行为

  1. 我发送"hey"到服务器
  2. 服务器将消息传输到 Dialogflow
  3. Dialogflow 确定意图是 "Welcome"
  4. Dialogflow 将 "Salutations" 发送回服务器
  5. 我收到回复"Salutations"

错误行为

  1. 我发送"help"到服务器
  2. 服务器将消息传输到 Dialogflow
  3. Dialogflow 确定意图是 "Help"
  4. Dialogflow 发回 “一些长文本” 到服务器
  5. 服务器崩溃和returns一般错误 500

如果我发送像 "blah blah blah".

这样的请求,默认回退意图也是如此

区别

欢迎意图不提供输出上下文,也不重置上下文。 帮助意图确实提供了输出上下文。 Fallback 意图不提供输出上下文,但会重置上下文。

我已验证,如果我提供带有欢迎意图的输出上下文,服务器会崩溃,如果我从帮助意图中删除输出上下文,一切正常。

问题

这个项目是怎么回事?为什么输出上下文使我的服务器崩溃?我该如何解决?

当然,我不能只从意图中删除输出上下文。

代码

<?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));
    }
}

欢迎对话框输出

{
  "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>"
  }
}

帮助对话框输出

{
  "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>"
  }
}

欢迎随时询问更多信息。

经过几天的忧郁,结果我的服务器缺少 google/protobuf 所需的 PHP 扩展 bcmath。我在 php.ini 中启用了它,一切正常。

您可以找到更多关于 Github 问题的信息:https://github.com/googleapis/google-cloud-php/issues/2120