Как установить выходные контексты при создании намерения в диалоговом потоке PHP? - PullRequest
1 голос
/ 13 января 2020

Я пытаюсь установить выходной контекст для определенного намерения через API создания намерения v2. Пожалуйста, проверьте мой код.

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

Я получаю сообщение об ошибке

{"message": "com.google.apps.framework.request.BadRequestException: имя ресурса не соответствует формату 'projects / {project_id} / agent / session / {session_id} / contexts / {context_id}' или 'projects / {project_id} / location / {location_id} / agent / session / / {session_id} / contexts / {context_id} '. "," code ": 3," status ":" INVALID_ARGUMENT "," details ": []}

Я считаю, что проблема связана с форматом хранения выходного контекста. Пожалуйста, помогите мне в этом.

1 Ответ

0 голосов
/ 20 апреля 2020

У меня была такая же проблема. Ответ заключается не в том, чтобы ввести пустое имя, а в том, чтобы строка определенного формата была сделана

, см. 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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...