I've been struggling to find out why the above error is thrown. I've read and tried all the solutions on google but the problem persists. This code snippet throws an error in the line:
$response = $sessionsClient->detectIntent($session, $queryInput);
Thank you for looking into this.
<?php
//namespace Google\Cloud\Samples\Auth;
namespace Google\Cloud\Dialogflow;
require 'vendor/autoload.php';
use Google\Cloud\Dialogflow\V2\EntityTypesClient;
use Google\Cloud\Dialogflow\V2\SessionsClient;
use Google\Cloud\Dialogflow\V2\TextInput;
use Google\Cloud\Dialogflow\V2\QueryInput;
$keyfile = "/var/www/html/newestproject-329521-ffa8cd057f77.json";
$projectId = 'newestproject-329521';
detect_intent_texts($projectId, "hi", "12345", $keyfile);
function detect_intent_texts($projectId, $text, $sessionId, $keyfile, $languageCode = 'en-US')
{
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $keyfile); //your path to file of cred
// new session
$test = array('credentials' => $keyfile, 'api_endpoint' => 'europe-west1-dialogflow.googleapis.com:443');
$sessionsClient = new SessionsClient($test);
$session = $sessionsClient->sessionName($projectId, $sessionId ?: uniqid());
printf('Session path: %s' . PHP_EOL, $session);
echo "h3";
// create text input
$textInput = new TextInput();
$textInput->setText($text);
$textInput->setLanguageCode($languageCode);
echo "h4";
// create query input
$queryInput = new QueryInput();
$queryInput->setText($textInput);
echo "h5";
// get response and relevant info
$response = $sessionsClient->detectIntent($session, $queryInput);
$sessionsClient->close();
}
There could be multiple reasons for the error message. They are as follows:
“Once an agent is created, its location cannot change. In order to change an agent's location, you must export and restore to a new agent with a different location.”
You might have missed to specify a region with the API request. For REST calls, you must do both the following:
For example:
us-central1-dialogflow.googleapis.com/projects/{PROJECT_ID}/locations/us-central1/agents/{AGENT_ID}