Skip to content

Commit d997ed8

Browse files
authored
chore: upgrade dialogflow samples to new client surface (GoogleCloudPlatform#1951)
1 parent 6d1cbe1 commit d997ed8

20 files changed

+105
-46
lines changed

dialogflow/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-dialogflow": "^1.0",
3+
"google/cloud-dialogflow": "^1.10",
44
"symfony/console": "^5.0"
55
},
66
"autoload": {

dialogflow/dialogflow.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
namespace Google\Cloud\Samples\Dialogflow;
1919

20+
use Google\Cloud\Dialogflow\V2\EntityType\Kind;
21+
use Google\Cloud\Dialogflow\V2\SessionEntityType\EntityOverrideMode;
2022
use Symfony\Component\Console\Application;
2123
use Symfony\Component\Console\Command\Command;
2224
use Symfony\Component\Console\Input\InputArgument;
2325
use Symfony\Component\Console\Input\InputOption;
24-
use Google\Cloud\Dialogflow\V2\EntityType\Kind;
25-
use Google\Cloud\Dialogflow\V2\SessionEntityType\EntityOverrideMode;
2626

2727
# includes the autoloader for libraries installed with composer
2828
require __DIR__ . '/vendor/autoload.php';

dialogflow/src/context_create.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
// [START dialogflow_create_context]
1919
namespace Google\Cloud\Samples\Dialogflow;
2020

21-
use Google\Cloud\Dialogflow\V2\ContextsClient;
21+
use Google\Cloud\Dialogflow\V2\Client\ContextsClient;
2222
use Google\Cloud\Dialogflow\V2\Context;
23+
use Google\Cloud\Dialogflow\V2\CreateContextRequest;
2324

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

3536
// create context
36-
$response = $contextsClient->createContext($parent, $context);
37+
$createContextRequest = (new CreateContextRequest())
38+
->setParent($parent)
39+
->setContext($context);
40+
$response = $contextsClient->createContext($createContextRequest);
3741
printf('Context created: %s' . PHP_EOL, $response->getName());
3842

3943
$contextsClient->close();

dialogflow/src/context_delete.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@
1818
// [START dialogflow_delete_context]
1919
namespace Google\Cloud\Samples\Dialogflow;
2020

21-
use Google\Cloud\Dialogflow\V2\ContextsClient;
21+
use Google\Cloud\Dialogflow\V2\Client\ContextsClient;
22+
use Google\Cloud\Dialogflow\V2\DeleteContextRequest;
2223

2324
function context_delete($projectId, $contextId, $sessionId)
2425
{
2526
$contextsClient = new ContextsClient();
2627

2728
$contextName = $contextsClient->contextName($projectId, $sessionId,
2829
$contextId);
29-
$contextsClient->deleteContext($contextName);
30+
$deleteContextRequest = (new DeleteContextRequest())
31+
->setName($contextName);
32+
$contextsClient->deleteContext($deleteContextRequest);
3033
printf('Context deleted: %s' . PHP_EOL, $contextName);
3134

3235
$contextsClient->close();

dialogflow/src/context_list.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@
1818
// [START dialogflow_list_contexts]
1919
namespace Google\Cloud\Samples\Dialogflow;
2020

21-
use Google\Cloud\Dialogflow\V2\ContextsClient;
21+
use Google\Cloud\Dialogflow\V2\Client\ContextsClient;
22+
use Google\Cloud\Dialogflow\V2\ListContextsRequest;
2223

2324
function context_list($projectId, $sessionId)
2425
{
2526
// get contexts
2627
$contextsClient = new ContextsClient();
2728
$parent = $contextsClient->sessionName($projectId, $sessionId);
28-
$contexts = $contextsClient->listContexts($parent);
29+
$listContextsRequest = (new ListContextsRequest())
30+
->setParent($parent);
31+
$contexts = $contextsClient->listContexts($listContextsRequest);
2932

3033
printf('Contexts for session %s' . PHP_EOL, $parent);
3134
foreach ($contexts->iterateAllElements() as $context) {

dialogflow/src/detect_intent_audio.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
// [START dialogflow_detect_intent_audio]
1919
namespace Google\Cloud\Samples\Dialogflow;
2020

21-
use Google\Cloud\Dialogflow\V2\SessionsClient;
2221
use Google\Cloud\Dialogflow\V2\AudioEncoding;
22+
use Google\Cloud\Dialogflow\V2\Client\SessionsClient;
23+
use Google\Cloud\Dialogflow\V2\DetectIntentRequest;
2324
use Google\Cloud\Dialogflow\V2\InputAudioConfig;
2425
use Google\Cloud\Dialogflow\V2\QueryInput;
2526

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

5152
// get response and relevant info
52-
$response = $sessionsClient->detectIntent($session, $queryInput, ['inputAudio' => $inputAudio]);
53+
$detectIntentRequest = (new DetectIntentRequest())
54+
->setSession($session)
55+
->setQueryInput($queryInput)
56+
->setInputAudio($inputAudio);
57+
$response = $sessionsClient->detectIntent($detectIntentRequest);
5358
$queryResult = $response->getQueryResult();
5459
$queryText = $queryResult->getQueryText();
5560
$intent = $queryResult->getIntent();

dialogflow/src/detect_intent_stream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
// [START dialogflow_detect_intent_streaming]
1919
namespace Google\Cloud\Samples\Dialogflow;
2020

21-
use Google\Cloud\Dialogflow\V2\SessionsClient;
2221
use Google\Cloud\Dialogflow\V2\AudioEncoding;
22+
use Google\Cloud\Dialogflow\V2\Client\SessionsClient;
2323
use Google\Cloud\Dialogflow\V2\InputAudioConfig;
2424
use Google\Cloud\Dialogflow\V2\QueryInput;
2525
use Google\Cloud\Dialogflow\V2\StreamingDetectIntentRequest;

dialogflow/src/detect_intent_texts.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
// [START dialogflow_detect_intent_text]
1919
namespace Google\Cloud\Samples\Dialogflow;
2020

21-
use Google\Cloud\Dialogflow\V2\SessionsClient;
22-
use Google\Cloud\Dialogflow\V2\TextInput;
21+
use Google\Cloud\Dialogflow\V2\Client\SessionsClient;
22+
use Google\Cloud\Dialogflow\V2\DetectIntentRequest;
2323
use Google\Cloud\Dialogflow\V2\QueryInput;
24+
use Google\Cloud\Dialogflow\V2\TextInput;
2425

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

4849
// get response and relevant info
49-
$response = $sessionsClient->detectIntent($session, $queryInput);
50+
$detectIntentRequest = (new DetectIntentRequest())
51+
->setSession($session)
52+
->setQueryInput($queryInput);
53+
$response = $sessionsClient->detectIntent($detectIntentRequest);
5054
$queryResult = $response->getQueryResult();
5155
$queryText = $queryResult->getQueryText();
5256
$intent = $queryResult->getIntent();

dialogflow/src/entity_create.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
// [START dialogflow_create_entity]
1919
namespace Google\Cloud\Samples\Dialogflow;
2020

21-
use Google\Cloud\Dialogflow\V2\EntityTypesClient;
21+
use Google\Cloud\Dialogflow\V2\BatchCreateEntitiesRequest;
22+
use Google\Cloud\Dialogflow\V2\Client\EntityTypesClient;
2223
use Google\Cloud\Dialogflow\V2\EntityType\Entity;
2324

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

4445
// create entity
45-
$response = $entityTypesClient->batchCreateEntities($parent, [$entity]);
46+
$batchCreateEntitiesRequest = (new BatchCreateEntitiesRequest())
47+
->setParent($parent)
48+
->setEntities([$entity]);
49+
$response = $entityTypesClient->batchCreateEntities($batchCreateEntitiesRequest);
4650
printf('Entity created: %s' . PHP_EOL, $response->getName());
4751

4852
$entityTypesClient->close();

dialogflow/src/entity_delete.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
// [START dialogflow_delete_entity]
1919
namespace Google\Cloud\Samples\Dialogflow;
2020

21-
use Google\Cloud\Dialogflow\V2\EntityTypesClient;
21+
use Google\Cloud\Dialogflow\V2\BatchDeleteEntitiesRequest;
22+
use Google\Cloud\Dialogflow\V2\Client\EntityTypesClient;
2223

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

3031
$parent = $entityTypesClient->entityTypeName($projectId,
3132
$entityTypeId);
32-
$entityTypesClient->batchDeleteEntities($parent, [$entityValue]);
33+
$batchDeleteEntitiesRequest = (new BatchDeleteEntitiesRequest())
34+
->setParent($parent)
35+
->setEntityValues([$entityValue]);
36+
$entityTypesClient->batchDeleteEntities($batchDeleteEntitiesRequest);
3337
printf('Entity deleted: %s' . PHP_EOL, $entityValue);
3438

3539
$entityTypesClient->close();

dialogflow/src/entity_list.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
// [START dialogflow_list_entities]
1919
namespace Google\Cloud\Samples\Dialogflow;
2020

21-
use Google\Cloud\Dialogflow\V2\EntityTypesClient;
21+
use Google\Cloud\Dialogflow\V2\Client\EntityTypesClient;
22+
use Google\Cloud\Dialogflow\V2\GetEntityTypeRequest;
2223

2324
function entity_list($projectId, $entityTypeId)
2425
{
@@ -27,7 +28,9 @@ function entity_list($projectId, $entityTypeId)
2728
// prepare
2829
$parent = $entityTypesClient->entityTypeName($projectId,
2930
$entityTypeId);
30-
$entityType = $entityTypesClient->getEntityType($parent);
31+
$getEntityTypeRequest = (new GetEntityTypeRequest())
32+
->setName($parent);
33+
$entityType = $entityTypesClient->getEntityType($getEntityTypeRequest);
3134

3235
// get entities
3336
$entities = $entityType->getEntities();

dialogflow/src/entity_type_create.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
// [START dialogflow_create_entity_type]
1919
namespace Google\Cloud\Samples\Dialogflow;
2020

21-
use Google\Cloud\Dialogflow\V2\EntityTypesClient;
21+
use Google\Cloud\Dialogflow\V2\Client\EntityTypesClient;
22+
use Google\Cloud\Dialogflow\V2\CreateEntityTypeRequest;
2223
use Google\Cloud\Dialogflow\V2\EntityType;
2324
use Google\Cloud\Dialogflow\V2\EntityType\Kind;
2425

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

3839
// create entity type
39-
$response = $entityTypesClient->createEntityType($parent, $entityType);
40+
$createEntityTypeRequest = (new CreateEntityTypeRequest())
41+
->setParent($parent)
42+
->setEntityType($entityType);
43+
$response = $entityTypesClient->createEntityType($createEntityTypeRequest);
4044
printf('Entity type created: %s' . PHP_EOL, $response->getName());
4145

4246
$entityTypesClient->close();

dialogflow/src/entity_type_delete.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
// [START dialogflow_delete_entity_type]
1919
namespace Google\Cloud\Samples\Dialogflow;
2020

21-
use Google\Cloud\Dialogflow\V2\EntityTypesClient;
21+
use Google\Cloud\Dialogflow\V2\Client\EntityTypesClient;
22+
use Google\Cloud\Dialogflow\V2\DeleteEntityTypeRequest;
2223

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

3031
$parent = $entityTypesClient->entityTypeName($projectId,
3132
$entityTypeId);
32-
$entityTypesClient->deleteEntityType($parent);
33+
$deleteEntityTypeRequest = (new DeleteEntityTypeRequest())
34+
->setName($parent);
35+
$entityTypesClient->deleteEntityType($deleteEntityTypeRequest);
3336
printf('Entity type deleted: %s' . PHP_EOL, $parent);
3437

3538
$entityTypesClient->close();

dialogflow/src/entity_type_list.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@
1818
// [START dialogflow_list_entity_types]
1919
namespace Google\Cloud\Samples\Dialogflow;
2020

21-
use Google\Cloud\Dialogflow\V2\EntityTypesClient;
21+
use Google\Cloud\Dialogflow\V2\Client\EntityTypesClient;
22+
use Google\Cloud\Dialogflow\V2\ListEntityTypesRequest;
2223

2324
function entity_type_list($projectId)
2425
{
2526
// get entity types
2627
$entityTypesClient = new EntityTypesClient();
2728
$parent = $entityTypesClient->agentName($projectId);
28-
$entityTypes = $entityTypesClient->listEntityTypes($parent);
29+
$listEntityTypesRequest = (new ListEntityTypesRequest())
30+
->setParent($parent);
31+
$entityTypes = $entityTypesClient->listEntityTypes($listEntityTypesRequest);
2932

3033
foreach ($entityTypes->iterateAllElements() as $entityType) {
3134
// print relevant info

dialogflow/src/intent_create.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
// [START dialogflow_create_intent]
1919
namespace Google\Cloud\Samples\Dialogflow;
2020

21-
use Google\Cloud\Dialogflow\V2\IntentsClient;
22-
use Google\Cloud\Dialogflow\V2\Intent\TrainingPhrase\Part;
23-
use Google\Cloud\Dialogflow\V2\Intent\TrainingPhrase;
24-
use Google\Cloud\Dialogflow\V2\Intent\Message\Text;
25-
use Google\Cloud\Dialogflow\V2\Intent\Message;
21+
use Google\Cloud\Dialogflow\V2\Client\IntentsClient;
22+
use Google\Cloud\Dialogflow\V2\CreateIntentRequest;
2623
use Google\Cloud\Dialogflow\V2\Intent;
24+
use Google\Cloud\Dialogflow\V2\Intent\Message;
25+
use Google\Cloud\Dialogflow\V2\Intent\Message\Text;
26+
use Google\Cloud\Dialogflow\V2\Intent\TrainingPhrase;
27+
use Google\Cloud\Dialogflow\V2\Intent\TrainingPhrase\Part;
2728

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

6364
// create intent
64-
$response = $intentsClient->createIntent($parent, $intent);
65+
$createIntentRequest = (new CreateIntentRequest())
66+
->setParent($parent)
67+
->setIntent($intent);
68+
$response = $intentsClient->createIntent($createIntentRequest);
6569
printf('Intent created: %s' . PHP_EOL, $response->getName());
6670

6771
$intentsClient->close();

dialogflow/src/intent_delete.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
// [START dialogflow_delete_intent]
1919
namespace Google\Cloud\Samples\Dialogflow;
2020

21-
use Google\Cloud\Dialogflow\V2\IntentsClient;
21+
use Google\Cloud\Dialogflow\V2\Client\IntentsClient;
22+
use Google\Cloud\Dialogflow\V2\DeleteIntentRequest;
2223

2324
/**
2425
* Delete intent with the given intent type and intent value.
@@ -27,8 +28,10 @@ function intent_delete($projectId, $intentId)
2728
{
2829
$intentsClient = new IntentsClient();
2930
$intentName = $intentsClient->intentName($projectId, $intentId);
31+
$deleteIntentRequest = (new DeleteIntentRequest())
32+
->setName($intentName);
3033

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

3437
$intentsClient->close();

dialogflow/src/intent_list.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@
1818
// [START dialogflow_list_intents]
1919
namespace Google\Cloud\Samples\Dialogflow;
2020

21-
use Google\Cloud\Dialogflow\V2\IntentsClient;
21+
use Google\Cloud\Dialogflow\V2\Client\IntentsClient;
22+
use Google\Cloud\Dialogflow\V2\ListIntentsRequest;
2223

2324
function intent_list($projectId)
2425
{
2526
// get intents
2627
$intentsClient = new IntentsClient();
2728
$parent = $intentsClient->agentName($projectId);
28-
$intents = $intentsClient->listIntents($parent);
29+
$listIntentsRequest = (new ListIntentsRequest())
30+
->setParent($parent);
31+
$intents = $intentsClient->listIntents($listIntentsRequest);
2932

3033
foreach ($intents->iterateAllElements() as $intent) {
3134
// print relevant info

dialogflow/src/session_entity_type_create.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
// [START dialogflow_create_session_entity_type]
1919
namespace Google\Cloud\Samples\Dialogflow;
2020

21-
use Google\Cloud\Dialogflow\V2\SessionEntityType\EntityOverrideMode;
22-
use Google\Cloud\Dialogflow\V2\SessionEntityTypesClient;
23-
use Google\Cloud\Dialogflow\V2\SessionEntityType;
21+
use Google\Cloud\Dialogflow\V2\Client\SessionEntityTypesClient;
22+
use Google\Cloud\Dialogflow\V2\CreateSessionEntityTypeRequest;
2423
use Google\Cloud\Dialogflow\V2\EntityType\Entity;
24+
use Google\Cloud\Dialogflow\V2\SessionEntityType;
25+
use Google\Cloud\Dialogflow\V2\SessionEntityType\EntityOverrideMode;
2526

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

5455
// create session entity type
55-
$response = $sessionEntityTypesClient->createSessionEntityType($parent,
56-
$sessionEntityType);
56+
$createSessionEntityTypeRequest = (new CreateSessionEntityTypeRequest())
57+
->setParent($parent)
58+
->setSessionEntityType($sessionEntityType);
59+
$response = $sessionEntityTypesClient->createSessionEntityType($createSessionEntityTypeRequest);
5760
printf('Session entity type created: %s' . PHP_EOL, $response->getName());
5861

5962
$sessionEntityTypesClient->close();

dialogflow/src/session_entity_type_delete.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
// [START dialogflow_delete_session_entity_type]
1919
namespace Google\Cloud\Samples\Dialogflow;
2020

21-
use Google\Cloud\Dialogflow\V2\SessionEntityTypesClient;
21+
use Google\Cloud\Dialogflow\V2\Client\SessionEntityTypesClient;
22+
use Google\Cloud\Dialogflow\V2\DeleteSessionEntityTypeRequest;
2223

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

3031
$sessionEntityTypeName = $sessionEntityTypesClient
3132
->sessionEntityTypeName($projectId, $sessionId, $displayName);
32-
$sessionEntityTypesClient->deleteSessionEntityType($sessionEntityTypeName);
33+
$deleteSessionEntityTypeRequest = (new DeleteSessionEntityTypeRequest())
34+
->setName($sessionEntityTypeName);
35+
$sessionEntityTypesClient->deleteSessionEntityType($deleteSessionEntityTypeRequest);
3336
printf('Session entity type deleted: %s' . PHP_EOL, $sessionEntityTypeName);
3437

3538
$sessionEntityTypesClient->close();

0 commit comments

Comments
 (0)