Search code examples
phpdialogflow-cx

com.google.apps.framework.request.NotFoundException: No DesignTimeAgent found for project


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();
}

Solution

  • There could be multiple reasons for the error message. They are as follows:

    • Your agent might not be linked to your project ID which you are accessing. Refer to this similar issue in GitHub and Stack Overflow.
    • If you have tried to change the location of the agent, I would suggest you export the agent first and then start again by creating a new agent, this time please make sure you are selecting the correct region and project ID. Because, the existing agent’s location doesn't change unless the agent is recreated. Quoting from this doc,

    “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:

      • Provide the region ID for the location URL path parameter.
      • Use [region-id]-dialogflow.googleapis.com for the hostname.

      For example:

      us-central1-dialogflow.googleapis.com/projects/{PROJECT_ID}/locations/us-central1/agents/{AGENT_ID}