Skip to content

Commit d997ed8

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

20 files changed

Lines changed: 105 additions & 46 deletions

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

0 commit comments

Comments
 (0)