Dialogflow v2 PhP 在检测意图中设置上下文

Dialogflow v2 PhP Set Context in Detect Intent

我正在尝试使用以下代码设置上下文。

  $text = "Some Text";
    $sessionsClient = new SessionsClient(array('credentials' => base_path('/google-cloud.json')));
    $session = $sessionsClient->sessionName('[project-id]', uniqid());
    $textInput = new TextInput();
    $textInput->setText($text);

    $queryInput = new QueryInput();
    $queryInput->setText($textInput);
    $textInput->setLanguageCode('en-US');

    $queryParams = new QueryParameters();
    $contextInput = new Context();
    $contextInput->setLifespanCount(1);
    $contextInput->setName('Input_Text');
    $queryParams->setContexts(array($contextInput));
    $optionalsParams = array('queryParams' => $queryParams);

    $response = $sessionsClient->detectIntent($session, $queryInput, $optionalsParams);
    $queryResult = $response->getQueryResult();
    $queryText = $queryResult->getQueryText();
    $intent = $queryResult->getIntent();
    $displayName = $intent->getDisplayName();
    $confidence = $queryResult->getIntentDetectionConfidence();
    $fulfilmentText = $queryResult->getFulfillmentText();

    return [
        'data' => [
            'success' => true,
            'text' => $text,
            'queryResult' => $queryResult,
            'queryText' => $queryText,
            'intent' => $intent,
            'displayName' => $displayName,
            'confidence' => $confidence,
            'fulfilmentText' => $fulfilmentText,
        ]
    ];

收到以下错误

{ "message": "com.google.apps.framework.request.BadRequestException: [ResourceName error] Path 'Input_Text' does not match template 'projects\/{project_id=*}\/locations\/{location_id=*}\/agent\/environments\/{environment_id=*}\/users\/{user_id=*}\/sessions\/{session_id=*}\/contexts\/{context_id=*}'.", "code": 3, "status": "INVALID_ARGUMENT", "details": [] }

有人成功了吗?文档或示例代码中都没有提到通过请求传递上下文。

已解决,需要使用 Google\Cloud\Dialogflow\V2\ContextsClient::contextName 格式化意图名称 在使用 setIntent

之前

工作代码

   $text = "Some Text";
    $project_id = '[project-id]';
    $session_id = uniqid();
    $sessionsClient = new SessionsClient(array('credentials' => base_path('/google-cloud.json')));
    $session = $sessionsClient->sessionName($project_id, $session_id);
    $textInput = new TextInput();
    $textInput->setText($text);

    $queryInput = new QueryInput();
    $queryInput->setText($textInput);
    $textInput->setLanguageCode('en-US');

    $queryParams = new QueryParameters();
    $contextInput = new Context();
    $contextInput->setLifespanCount(1);
    $contextInput->setName(Google\Cloud\Dialogflow\V2\ContextsClient::contextName($project_id, $session_id, 'input_text'));
    $queryParams->setContexts(array($contextInput));
    $optionalsParams = array('queryParams' => $queryParams);

    $response = $sessionsClient->detectIntent($session, $queryInput, $optionalsParams);
    $queryResult = $response->getQueryResult();
    $queryText = $queryResult->getQueryText();
    $intent = $queryResult->getIntent();
    $displayName = $intent->getDisplayName();
    $confidence = $queryResult->getIntentDetectionConfidence();
    $fulfilmentText = $queryResult->getFulfillmentText();

    return [
        'data' => [
            'success' => true,
            'text' => $text,
            'queryResult' => $queryResult,
            'queryText' => $queryText,
            'intent' => $intent,
            'displayName' => $displayName,
            'confidence' => $confidence,
            'fulfilmentText' => $fulfilmentText,
        ]
    ];