【问题标题】:How to get custom payload response from dialogflow to API in PHP如何在 PHP 中获取从对话框流到 API 的自定义有效负载响应
【发布时间】:2021-11-30 18:11:16
【问题描述】:

仅获取意图的文本响应,但 api 响应中缺少自定义负载响应。

Dialogflow 原始响应为 Image

{
"allRequiredParamsPresent": true,
"fulfillmentText": "Thanks Dhruva, are you ?",
"fulfillmentMessages": [
  {
    "text": {
      "text": [
        "Thanks Dhruva, are you ?"
      ]
    }
  },
  {
    "payload": {
      "richContent": [
        [
          {
            "type": "chips",
            "options": [
              {
                "text": "permanent employee"
              },
              {
                "text": "probation employee"
              }
            ]
          }
        ]
      ]
    }
  }
]
}

Api 只接收文本而不是自定义负载 Image

从这个post 获得参考,但也无法修复,任何帮助或指出正确的方向将不胜感激。

【问题讨论】:

  • 您是否在 PHP 代码中使用任何 Dialogflow 库?
  • 是的,使用 ~composer 安装 Google Cloud 客户端库的 dialogflow 组件需要 google/cloud-dialogflow

标签: php api dialogflow-es dialogflow-es-fulfillment


【解决方案1】:

这就是我在 php 关联数组中获取自定义有效负载的方法

        $textInput = new TextInput();
        $textInput->setText($request->query("message","message that has custom payload response"));
        $textInput->setLanguageCode("en-US");

        //Putting it in query input
        $queryInput = new QueryInput();
        $queryInput->setText($textInput);
        $response = $sessionClient->detectIntent($session, $queryInput);
        $queryResult = $response->getQueryResult();

        //Beginning of solution
        $fulfillmentMessages = $queryResult->getFulfillmentMessages();
        $customPayloads = []; //The result array

        foreach ($fulfillmentMessages as $fulfillmentMessage) {
            /* @var $fulfillmentMessage Message*/
            if($fulfillmentMessage->hasPayload()) {
                $customPayloads[] = json_decode($fulfillmentMessage->getPayload()
                    ->serializeToJsonString(),1);
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 2019-12-25
    • 1970-01-01
    相关资源
    最近更新 更多