1
votes

I am trying to set output context to a particular intent via v2 create intent API. Please check my code.

use Google\Cloud\Dialogflow\V2\SessionsClient;
use Google\Cloud\Dialogflow\V2\TextInput;
use Google\Cloud\Dialogflow\V2\QueryInput;
use Google\Cloud\Dialogflow\V2\IntentsClient;
use Google\Cloud\Dialogflow\V2\Intent_TrainingPhrase_Part;
use Google\Cloud\Dialogflow\V2\Intent_TrainingPhrase;
use Google\Cloud\Dialogflow\V2\Intent_Message_Text;
use Google\Cloud\Dialogflow\V2\Intent_Message;
use Google\Cloud\Dialogflow\V2\Intent;
use Google\Cloud\Dialogflow\V2\Context;
use Google\Cloud\Dialogflow\V2\ContextsClient;

private function intent_create(){
        putenv('GOOGLE_APPLICATION_CREDENTIALS='.getcwd() . '/strive_stage.json');
        $intentsClient = new IntentsClient();
        /** Create Intent **/
        $disaplayName = "Where is Goa";
        $utterances = ["Goa", "Where is Goa"];
        // prepare training phrases for intent
        $trainingPhrases = [];
        foreach ($utterances as $trainingPhrasePart) {
        $part = new Intent_TrainingPhrase_Part();
        $part->setText($trainingPhrasePart);
        // create new training phrase for each provided part
        $trainingPhrase = new Intent_TrainingPhrase();
        $trainingPhrase->setParts([$part]);
        $trainingPhrases[] = $trainingPhrase;
        }
        $messageTexts = 'Goa is in India.';
        // prepare messages for intent
        $text = new Intent_Message_Text();
        $text->setText([$messageTexts]);
        $message = new Intent_Message();
        $message->setText($text);
        $createIntentObject = $intentsClient->projectAgentName(env("DIALOG_FLOW_PROJECT_ID"));
        // prepare intent
        $intent = new Intent();
        $intent->setDisplayName($disaplayName);
        $intent->setTrainingPhrases($trainingPhrases);
        $intent->setMessages([$message]);
        $contexts = ['test'];
        foreach($contexts as $con){
            $contextObj = new Context();
            $contextObj->setName($con);
            $contextData[] = $contextObj;  
            $intent->setOutputContexts($contextData);
        }
        // $intent->getOutputContexts('test');
        //dd($intent);
        $response = $intentsClient->createIntent($createIntentObject, $intent);
        printf('Intent created: %s' . PHP_EOL, $response->getName());
     }

I am getting error message

{ "message": "com.google.apps.framework.request.BadRequestException: Resource name does not match format 'projects/{project_id}/agent/sessions/{session_id}/contexts/{context_id}' or 'projects/{project_id}/locations/{location_id}/agent/sessions/{session_id}/contexts/{context_id}'.", "code": 3, "status": "INVALID_ARGUMENT", "details": [] }

I believe the issue is with the format of storing the output context. Please help me on this.

2

2 Answers

1
votes

I had the same issue. The answer is to not put in the bare name, but to have a string of a certain format made

see http://googleapis.github.io/google-cloud-php/#/docs/google-cloud/v0.131.0/dialogflow/v2/intent?method=setOutputContexts

// $projectId your project id
// $sessionId can be anything, I use $sessionId = uniqid();
$uri = "projects/$projectId/agent/sessions/$sessionId/contexts/$con";
$contextObj->setName($uri);
0
votes

I can see that there are any response about this topic. I leave here my solution for the next generations

To create a output context you need to create the correct format, for this objetive you can use Context class Google\Cloud\Dialogflow\V2\Context

private function parseoOutputContexts($contexts, $project_id, $lifespan = 5)
    {
        $newContexts = array();
 
        foreach ($contexts as $context) {

           $newContexts[] = new Context(
               [
                   'name' => 'projects/' . $project_id . '/agent/sessions/-/contexts/' . $context,
                   'lifespan_count' => $lifespan
               ]
           );
        }
       
        return $newContexts;
    }

And you can use this function to finnaly add the output context to the object Intent.

$dialogflow_intent = new Intent();

$output_contexts = ['output_context_1'. 'output_context_2'];
$output_contexts = $this->parseOutputContexts($output_contexts, '[YOUR_PROJECT_ID]');
$dialogflow_intent->setOutputContexts($output_contexts);