I am trying to set the context with the following code.
$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,
]
];
Receiving the following error
{ "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": [] }
Has anyone got it working? There is no mention of passing Context through request in docs or in sample code.
Solved, need to format the intent name with Google\Cloud\Dialogflow\V2\ContextsClient::contextName
before using setIntent
Working Code
$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,
]
];