首页 > 解决方案 > 带有输出上下文的 Dialogflow 响应使 PHP/Symfony 服务器崩溃

问题描述

TL;博士

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

一些上下文

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

正确的行为

  1. 我向服务器发送“嘿”
  2. 服务器将消息传输到 Dialogflow
  3. Dialogflow 确定意图是“欢迎”
  4. Dialogflow 将“Salutations”发送回服务器
  5. 我收到回复“问候”

错误行为

  1. 我向服务器发送“帮助”
  2. 服务器将消息传输到 Dialogflow
  3. Dialogflow 确定意图是“帮助”
  4. Dialogflow 将“[一些长文本]”发送回服务器
  5. 服务器崩溃并返回一般错误 500

如果我发送类似"blah blah blah"的请求,默认的后备意图也是如此。

区别

Welcome 意图不提供输出上下文,也不重置上下文。帮助意图确实提供了输出上下文。回退意图不提供输出上下文,但会重置上下文。

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

问题

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

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

编码

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

随时询问其他信息。

标签: symfonydialogflow-es

解决方案


经过几天的忧郁,原来我的服务器bcmath缺少google/protobuf. 我在我的中启用了它,php.ini一切正常。

You can find a bit more information on the Github issue : https://github.com/googleapis/google-cloud-php/issues/2120


推荐阅读