Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade dialogflow samples to new client surface #1951

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dialogflow/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require": {
"google/cloud-dialogflow": "^1.0",
"google/cloud-dialogflow": "^1.10",
"symfony/console": "^5.0"
},
"autoload": {
Expand Down
4 changes: 2 additions & 2 deletions dialogflow/dialogflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

namespace Google\Cloud\Samples\Dialogflow;

use Google\Cloud\Dialogflow\V2\EntityType\Kind;
use Google\Cloud\Dialogflow\V2\SessionEntityType\EntityOverrideMode;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Google\Cloud\Dialogflow\V2\EntityType\Kind;
use Google\Cloud\Dialogflow\V2\SessionEntityType\EntityOverrideMode;

# includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';
Expand Down
8 changes: 6 additions & 2 deletions dialogflow/src/context_create.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
// [START dialogflow_create_context]
namespace Google\Cloud\Samples\Dialogflow;

use Google\Cloud\Dialogflow\V2\ContextsClient;
use Google\Cloud\Dialogflow\V2\Client\ContextsClient;
use Google\Cloud\Dialogflow\V2\Context;
use Google\Cloud\Dialogflow\V2\CreateContextRequest;

function context_create($projectId, $contextId, $sessionId, $lifespan = 1)
{
Expand All @@ -33,7 +34,10 @@ function context_create($projectId, $contextId, $sessionId, $lifespan = 1)
$context->setLifespanCount($lifespan);

// create context
$response = $contextsClient->createContext($parent, $context);
$createContextRequest = (new CreateContextRequest())
->setParent($parent)
->setContext($context);
$response = $contextsClient->createContext($createContextRequest);
printf('Context created: %s' . PHP_EOL, $response->getName());

$contextsClient->close();
Expand Down
7 changes: 5 additions & 2 deletions dialogflow/src/context_delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@
// [START dialogflow_delete_context]
namespace Google\Cloud\Samples\Dialogflow;

use Google\Cloud\Dialogflow\V2\ContextsClient;
use Google\Cloud\Dialogflow\V2\Client\ContextsClient;
use Google\Cloud\Dialogflow\V2\DeleteContextRequest;

function context_delete($projectId, $contextId, $sessionId)
{
$contextsClient = new ContextsClient();

$contextName = $contextsClient->contextName($projectId, $sessionId,
$contextId);
$contextsClient->deleteContext($contextName);
$deleteContextRequest = (new DeleteContextRequest())
->setName($contextName);
$contextsClient->deleteContext($deleteContextRequest);
printf('Context deleted: %s' . PHP_EOL, $contextName);

$contextsClient->close();
Expand Down
7 changes: 5 additions & 2 deletions dialogflow/src/context_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@
// [START dialogflow_list_contexts]
namespace Google\Cloud\Samples\Dialogflow;

use Google\Cloud\Dialogflow\V2\ContextsClient;
use Google\Cloud\Dialogflow\V2\Client\ContextsClient;
use Google\Cloud\Dialogflow\V2\ListContextsRequest;

function context_list($projectId, $sessionId)
{
// get contexts
$contextsClient = new ContextsClient();
$parent = $contextsClient->sessionName($projectId, $sessionId);
$contexts = $contextsClient->listContexts($parent);
$listContextsRequest = (new ListContextsRequest())
->setParent($parent);
$contexts = $contextsClient->listContexts($listContextsRequest);

printf('Contexts for session %s' . PHP_EOL, $parent);
foreach ($contexts->iterateAllElements() as $context) {
Expand Down
9 changes: 7 additions & 2 deletions dialogflow/src/detect_intent_audio.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
// [START dialogflow_detect_intent_audio]
namespace Google\Cloud\Samples\Dialogflow;

use Google\Cloud\Dialogflow\V2\SessionsClient;
use Google\Cloud\Dialogflow\V2\AudioEncoding;
use Google\Cloud\Dialogflow\V2\Client\SessionsClient;
use Google\Cloud\Dialogflow\V2\DetectIntentRequest;
use Google\Cloud\Dialogflow\V2\InputAudioConfig;
use Google\Cloud\Dialogflow\V2\QueryInput;

Expand Down Expand Up @@ -49,7 +50,11 @@ function detect_intent_audio($projectId, $path, $sessionId, $languageCode = 'en-
$queryInput->setAudioConfig($audioConfig);

// get response and relevant info
$response = $sessionsClient->detectIntent($session, $queryInput, ['inputAudio' => $inputAudio]);
$detectIntentRequest = (new DetectIntentRequest())
->setSession($session)
->setQueryInput($queryInput)
->setInputAudio($inputAudio);
$response = $sessionsClient->detectIntent($detectIntentRequest);
$queryResult = $response->getQueryResult();
$queryText = $queryResult->getQueryText();
$intent = $queryResult->getIntent();
Expand Down
2 changes: 1 addition & 1 deletion dialogflow/src/detect_intent_stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
// [START dialogflow_detect_intent_streaming]
namespace Google\Cloud\Samples\Dialogflow;

use Google\Cloud\Dialogflow\V2\SessionsClient;
use Google\Cloud\Dialogflow\V2\AudioEncoding;
use Google\Cloud\Dialogflow\V2\Client\SessionsClient;
use Google\Cloud\Dialogflow\V2\InputAudioConfig;
use Google\Cloud\Dialogflow\V2\QueryInput;
use Google\Cloud\Dialogflow\V2\StreamingDetectIntentRequest;
Expand Down
10 changes: 7 additions & 3 deletions dialogflow/src/detect_intent_texts.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
// [START dialogflow_detect_intent_text]
namespace Google\Cloud\Samples\Dialogflow;

use Google\Cloud\Dialogflow\V2\SessionsClient;
use Google\Cloud\Dialogflow\V2\TextInput;
use Google\Cloud\Dialogflow\V2\Client\SessionsClient;
use Google\Cloud\Dialogflow\V2\DetectIntentRequest;
use Google\Cloud\Dialogflow\V2\QueryInput;
use Google\Cloud\Dialogflow\V2\TextInput;

/**
* Returns the result of detect intent with texts as inputs.
Expand All @@ -46,7 +47,10 @@ function detect_intent_texts($projectId, $texts, $sessionId, $languageCode = 'en
$queryInput->setText($textInput);

// get response and relevant info
$response = $sessionsClient->detectIntent($session, $queryInput);
$detectIntentRequest = (new DetectIntentRequest())
->setSession($session)
->setQueryInput($queryInput);
$response = $sessionsClient->detectIntent($detectIntentRequest);
$queryResult = $response->getQueryResult();
$queryText = $queryResult->getQueryText();
$intent = $queryResult->getIntent();
Expand Down
8 changes: 6 additions & 2 deletions dialogflow/src/entity_create.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
// [START dialogflow_create_entity]
namespace Google\Cloud\Samples\Dialogflow;

use Google\Cloud\Dialogflow\V2\EntityTypesClient;
use Google\Cloud\Dialogflow\V2\BatchCreateEntitiesRequest;
use Google\Cloud\Dialogflow\V2\Client\EntityTypesClient;
use Google\Cloud\Dialogflow\V2\EntityType\Entity;

/**
Expand All @@ -42,7 +43,10 @@ function entity_create($projectId, $entityTypeId, $entityValue, $synonyms = [])
$entity->setSynonyms($synonyms);

// create entity
$response = $entityTypesClient->batchCreateEntities($parent, [$entity]);
$batchCreateEntitiesRequest = (new BatchCreateEntitiesRequest())
->setParent($parent)
->setEntities([$entity]);
$response = $entityTypesClient->batchCreateEntities($batchCreateEntitiesRequest);
printf('Entity created: %s' . PHP_EOL, $response->getName());

$entityTypesClient->close();
Expand Down
8 changes: 6 additions & 2 deletions dialogflow/src/entity_delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
// [START dialogflow_delete_entity]
namespace Google\Cloud\Samples\Dialogflow;

use Google\Cloud\Dialogflow\V2\EntityTypesClient;
use Google\Cloud\Dialogflow\V2\BatchDeleteEntitiesRequest;
use Google\Cloud\Dialogflow\V2\Client\EntityTypesClient;

/**
* Delete entity with the given entity type and entity value.
Expand All @@ -29,7 +30,10 @@ function entity_delete($projectId, $entityTypeId, $entityValue)

$parent = $entityTypesClient->entityTypeName($projectId,
$entityTypeId);
$entityTypesClient->batchDeleteEntities($parent, [$entityValue]);
$batchDeleteEntitiesRequest = (new BatchDeleteEntitiesRequest())
->setParent($parent)
->setEntityValues([$entityValue]);
$entityTypesClient->batchDeleteEntities($batchDeleteEntitiesRequest);
printf('Entity deleted: %s' . PHP_EOL, $entityValue);

$entityTypesClient->close();
Expand Down
7 changes: 5 additions & 2 deletions dialogflow/src/entity_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
// [START dialogflow_list_entities]
namespace Google\Cloud\Samples\Dialogflow;

use Google\Cloud\Dialogflow\V2\EntityTypesClient;
use Google\Cloud\Dialogflow\V2\Client\EntityTypesClient;
use Google\Cloud\Dialogflow\V2\GetEntityTypeRequest;

function entity_list($projectId, $entityTypeId)
{
Expand All @@ -27,7 +28,9 @@ function entity_list($projectId, $entityTypeId)
// prepare
$parent = $entityTypesClient->entityTypeName($projectId,
$entityTypeId);
$entityType = $entityTypesClient->getEntityType($parent);
$getEntityTypeRequest = (new GetEntityTypeRequest())
->setName($parent);
$entityType = $entityTypesClient->getEntityType($getEntityTypeRequest);

// get entities
$entities = $entityType->getEntities();
Expand Down
8 changes: 6 additions & 2 deletions dialogflow/src/entity_type_create.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
// [START dialogflow_create_entity_type]
namespace Google\Cloud\Samples\Dialogflow;

use Google\Cloud\Dialogflow\V2\EntityTypesClient;
use Google\Cloud\Dialogflow\V2\Client\EntityTypesClient;
use Google\Cloud\Dialogflow\V2\CreateEntityTypeRequest;
use Google\Cloud\Dialogflow\V2\EntityType;
use Google\Cloud\Dialogflow\V2\EntityType\Kind;

Expand All @@ -36,7 +37,10 @@ function entity_type_create($projectId, $displayName, $kind = Kind::KIND_MAP)
$entityType->setKind($kind);

// create entity type
$response = $entityTypesClient->createEntityType($parent, $entityType);
$createEntityTypeRequest = (new CreateEntityTypeRequest())
->setParent($parent)
->setEntityType($entityType);
$response = $entityTypesClient->createEntityType($createEntityTypeRequest);
printf('Entity type created: %s' . PHP_EOL, $response->getName());

$entityTypesClient->close();
Expand Down
7 changes: 5 additions & 2 deletions dialogflow/src/entity_type_delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
// [START dialogflow_delete_entity_type]
namespace Google\Cloud\Samples\Dialogflow;

use Google\Cloud\Dialogflow\V2\EntityTypesClient;
use Google\Cloud\Dialogflow\V2\Client\EntityTypesClient;
use Google\Cloud\Dialogflow\V2\DeleteEntityTypeRequest;

/**
* Delete entity type with the given entity type name.
Expand All @@ -29,7 +30,9 @@ function entity_type_delete($projectId, $entityTypeId)

$parent = $entityTypesClient->entityTypeName($projectId,
$entityTypeId);
$entityTypesClient->deleteEntityType($parent);
$deleteEntityTypeRequest = (new DeleteEntityTypeRequest())
->setName($parent);
$entityTypesClient->deleteEntityType($deleteEntityTypeRequest);
printf('Entity type deleted: %s' . PHP_EOL, $parent);

$entityTypesClient->close();
Expand Down
7 changes: 5 additions & 2 deletions dialogflow/src/entity_type_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@
// [START dialogflow_list_entity_types]
namespace Google\Cloud\Samples\Dialogflow;

use Google\Cloud\Dialogflow\V2\EntityTypesClient;
use Google\Cloud\Dialogflow\V2\Client\EntityTypesClient;
use Google\Cloud\Dialogflow\V2\ListEntityTypesRequest;

function entity_type_list($projectId)
{
// get entity types
$entityTypesClient = new EntityTypesClient();
$parent = $entityTypesClient->agentName($projectId);
$entityTypes = $entityTypesClient->listEntityTypes($parent);
$listEntityTypesRequest = (new ListEntityTypesRequest())
->setParent($parent);
$entityTypes = $entityTypesClient->listEntityTypes($listEntityTypesRequest);

foreach ($entityTypes->iterateAllElements() as $entityType) {
// print relevant info
Expand Down
16 changes: 10 additions & 6 deletions dialogflow/src/intent_create.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
// [START dialogflow_create_intent]
namespace Google\Cloud\Samples\Dialogflow;

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\Client\IntentsClient;
use Google\Cloud\Dialogflow\V2\CreateIntentRequest;
use Google\Cloud\Dialogflow\V2\Intent;
use Google\Cloud\Dialogflow\V2\Intent\Message;
use Google\Cloud\Dialogflow\V2\Intent\Message\Text;
use Google\Cloud\Dialogflow\V2\Intent\TrainingPhrase;
use Google\Cloud\Dialogflow\V2\Intent\TrainingPhrase\Part;

/**
* Create an intent of the given intent type.
Expand Down Expand Up @@ -61,7 +62,10 @@ function intent_create($projectId, $displayName, $trainingPhraseParts = [],
->setMessages([$message]);

// create intent
$response = $intentsClient->createIntent($parent, $intent);
$createIntentRequest = (new CreateIntentRequest())
->setParent($parent)
->setIntent($intent);
$response = $intentsClient->createIntent($createIntentRequest);
printf('Intent created: %s' . PHP_EOL, $response->getName());

$intentsClient->close();
Expand Down
7 changes: 5 additions & 2 deletions dialogflow/src/intent_delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
// [START dialogflow_delete_intent]
namespace Google\Cloud\Samples\Dialogflow;

use Google\Cloud\Dialogflow\V2\IntentsClient;
use Google\Cloud\Dialogflow\V2\Client\IntentsClient;
use Google\Cloud\Dialogflow\V2\DeleteIntentRequest;

/**
* Delete intent with the given intent type and intent value.
Expand All @@ -27,8 +28,10 @@ function intent_delete($projectId, $intentId)
{
$intentsClient = new IntentsClient();
$intentName = $intentsClient->intentName($projectId, $intentId);
$deleteIntentRequest = (new DeleteIntentRequest())
->setName($intentName);

$intentsClient->deleteIntent($intentName);
$intentsClient->deleteIntent($deleteIntentRequest);
printf('Intent deleted: %s' . PHP_EOL, $intentName);

$intentsClient->close();
Expand Down
7 changes: 5 additions & 2 deletions dialogflow/src/intent_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@
// [START dialogflow_list_intents]
namespace Google\Cloud\Samples\Dialogflow;

use Google\Cloud\Dialogflow\V2\IntentsClient;
use Google\Cloud\Dialogflow\V2\Client\IntentsClient;
use Google\Cloud\Dialogflow\V2\ListIntentsRequest;

function intent_list($projectId)
{
// get intents
$intentsClient = new IntentsClient();
$parent = $intentsClient->agentName($projectId);
$intents = $intentsClient->listIntents($parent);
$listIntentsRequest = (new ListIntentsRequest())
->setParent($parent);
$intents = $intentsClient->listIntents($listIntentsRequest);

foreach ($intents->iterateAllElements() as $intent) {
// print relevant info
Expand Down
13 changes: 8 additions & 5 deletions dialogflow/src/session_entity_type_create.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
// [START dialogflow_create_session_entity_type]
namespace Google\Cloud\Samples\Dialogflow;

use Google\Cloud\Dialogflow\V2\SessionEntityType\EntityOverrideMode;
use Google\Cloud\Dialogflow\V2\SessionEntityTypesClient;
use Google\Cloud\Dialogflow\V2\SessionEntityType;
use Google\Cloud\Dialogflow\V2\Client\SessionEntityTypesClient;
use Google\Cloud\Dialogflow\V2\CreateSessionEntityTypeRequest;
use Google\Cloud\Dialogflow\V2\EntityType\Entity;
use Google\Cloud\Dialogflow\V2\SessionEntityType;
use Google\Cloud\Dialogflow\V2\SessionEntityType\EntityOverrideMode;

/**
* Create a session entity type with the given display name.
Expand Down Expand Up @@ -52,8 +53,10 @@ function session_entity_type_create($projectId, $displayName, $values,
->setEntities($entities);

// create session entity type
$response = $sessionEntityTypesClient->createSessionEntityType($parent,
$sessionEntityType);
$createSessionEntityTypeRequest = (new CreateSessionEntityTypeRequest())
->setParent($parent)
->setSessionEntityType($sessionEntityType);
$response = $sessionEntityTypesClient->createSessionEntityType($createSessionEntityTypeRequest);
printf('Session entity type created: %s' . PHP_EOL, $response->getName());

$sessionEntityTypesClient->close();
Expand Down
7 changes: 5 additions & 2 deletions dialogflow/src/session_entity_type_delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
// [START dialogflow_delete_session_entity_type]
namespace Google\Cloud\Samples\Dialogflow;

use Google\Cloud\Dialogflow\V2\SessionEntityTypesClient;
use Google\Cloud\Dialogflow\V2\Client\SessionEntityTypesClient;
use Google\Cloud\Dialogflow\V2\DeleteSessionEntityTypeRequest;

/**
* Delete a session entity type with the given display name.
Expand All @@ -29,7 +30,9 @@ function session_entity_type_delete($projectId, $displayName, $sessionId)

$sessionEntityTypeName = $sessionEntityTypesClient
->sessionEntityTypeName($projectId, $sessionId, $displayName);
$sessionEntityTypesClient->deleteSessionEntityType($sessionEntityTypeName);
$deleteSessionEntityTypeRequest = (new DeleteSessionEntityTypeRequest())
->setName($sessionEntityTypeName);
$sessionEntityTypesClient->deleteSessionEntityType($deleteSessionEntityTypeRequest);
printf('Session entity type deleted: %s' . PHP_EOL, $sessionEntityTypeName);

$sessionEntityTypesClient->close();
Expand Down
Loading
Loading