Skip to content
This repository was archived by the owner on Jul 20, 2023. It is now read-only.

Commit ec35c45

Browse files
docs(samples): Add detectIntent samples (#263)
* add webhook-configure-session-parameter-enable-agent-response sample and test Change-Id: Ia8593160ed33060eb497a15723b21c1221ce55c9 * add webhook-configure-session-parameter-trigger-transition sample and test Change-Id: I118abb59182879b6969018da97d04eea0b8daeb0 * add webhook-configure-optional-or-required-form-parameters sample and test Change-Id: I7cbfeb11cece7ccf873dbc7a6dd7cff9ae264ffe * add configure-session-parameters sample and test Change-Id: Ib9f1a110473751508b50259c4696593580c64a91 * fix form parameter path Change-Id: I6411000a7d0240d7552d725c2fd1049be781a9ab * add webhook-validate-form-parameter sample and test Change-Id: I82097be3fc3f91651c88b99c7431ebb602c42c66 * add cx to region tag Change-Id: I6640604512c27a341ab8e26238bf8c3fbd1e77ef * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix test Change-Id: Icc70e18b40d8684c7909e8383b4c226fa94a162b * fix region tag Change-Id: I0fe3849c0eaf12eaf247088993898cbb47dace44 * add detect intent with sentiment analysis sample and test Change-Id: I99aa3985c64b6c80ec1a85591b1e49f381c379de * punctuation Change-Id: I2fe6f929f4c786595b4bbc4fbe422f2370d3adf0 * add detect intent with eventInput sample and test Change-Id: I32a5b04e975b8e1a4c7d92b328d3bdd1e1c70448 * fix region tag Change-Id: I3795770f8ef68e154f36057ca9aa1bbb2ad8075f * add detect intent with IntentInput sample and test Change-Id: Iea12505b745ea8ea3995fdca2381f6e4bf60d051 * add detect intent synthesize tts response sample and test Change-Id: Ie7d22212f6f8107a36f555e771ff409c2d1f0bf0 * add resource output file Change-Id: I3f0f78d5dbb30a0c50ecad3996332cbd6ff0b7b5 * fix formatting Change-Id: Ia651757249955d18613f844734674f1eea49ed82 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix test agent Change-Id: Ib75ccf20985dcfae3130619f69c5fd546b24ca1d * fix test Change-Id: I1423fdb6916fa50fe0f21481fb1964e08ab62080 * change function names Change-Id: Iaa02c248807b72fcff4f07b46f0f70fddf9d6e7a * change test Change-Id: If5ea5d044f040cdb0f3b121af5af960322a1cf5a * change test Change-Id: I9a6b457d1739b2f51de86b77d75d9d0ce2973b04 * change test Change-Id: I92f20ec34c132448a0375d0c88f64b705eb216f5 Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 2d8f0ab commit ec35c45

11 files changed

Lines changed: 677 additions & 0 deletions

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,12 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-dialogflow-
142142
| Create-page | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/create-page.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/create-page.js,samples/README.md) |
143143
| Delete-page | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/delete-page.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/delete-page.js,samples/README.md) |
144144
| Detect-intent-audio | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-audio.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/detect-intent-audio.js,samples/README.md) |
145+
| Detect-intent-event | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-event.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/detect-intent-event.js,samples/README.md) |
145146
| Detect-intent-streaming | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-streaming.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/detect-intent-streaming.js,samples/README.md) |
147+
| Detect-intent-synthesize-tts-response | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-synthesize-tts-response.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/detect-intent-synthesize-tts-response.js,samples/README.md) |
146148
| Detect-intent-text | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-text.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/detect-intent-text.js,samples/README.md) |
149+
| Detect-intent-with-intent-input | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-with-intent-input.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/detect-intent-with-intent-input.js,samples/README.md) |
150+
| Detect-intent-with-sentiment-analysis | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-with-sentiment-analysis.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/detect-intent-with-sentiment-analysis.js,samples/README.md) |
147151
| List-intents | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/list-intents.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/list-intents.js,samples/README.md) |
148152
| List-page | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/list-page.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/list-page.js,samples/README.md) |
149153
| List-testcase-results | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/list-testcase-results.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/list-testcase-results.js,samples/README.md) |

samples/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
* [Create-page](#create-page)
1717
* [Delete-page](#delete-page)
1818
* [Detect-intent-audio](#detect-intent-audio)
19+
* [Detect-intent-event](#detect-intent-event)
1920
* [Detect-intent-streaming](#detect-intent-streaming)
21+
* [Detect-intent-synthesize-tts-response](#detect-intent-synthesize-tts-response)
2022
* [Detect-intent-text](#detect-intent-text)
23+
* [Detect-intent-with-intent-input](#detect-intent-with-intent-input)
24+
* [Detect-intent-with-sentiment-analysis](#detect-intent-with-sentiment-analysis)
2125
* [List-intents](#list-intents)
2226
* [List-page](#list-page)
2327
* [List-testcase-results](#list-testcase-results)
@@ -115,6 +119,23 @@ __Usage:__
115119

116120

117121

122+
### Detect-intent-event
123+
124+
View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-event.js).
125+
126+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/detect-intent-event.js,samples/README.md)
127+
128+
__Usage:__
129+
130+
131+
`node samples/detect-intent-event.js`
132+
133+
134+
-----
135+
136+
137+
138+
118139
### Detect-intent-streaming
119140

120141
View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-streaming.js).
@@ -132,6 +153,23 @@ __Usage:__
132153

133154

134155

156+
### Detect-intent-synthesize-tts-response
157+
158+
View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-synthesize-tts-response.js).
159+
160+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/detect-intent-synthesize-tts-response.js,samples/README.md)
161+
162+
__Usage:__
163+
164+
165+
`node samples/detect-intent-synthesize-tts-response.js`
166+
167+
168+
-----
169+
170+
171+
172+
135173
### Detect-intent-text
136174

137175
View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-text.js).
@@ -149,6 +187,40 @@ __Usage:__
149187

150188

151189

190+
### Detect-intent-with-intent-input
191+
192+
View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-with-intent-input.js).
193+
194+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/detect-intent-with-intent-input.js,samples/README.md)
195+
196+
__Usage:__
197+
198+
199+
`node samples/detect-intent-with-intent-input.js`
200+
201+
202+
-----
203+
204+
205+
206+
207+
### Detect-intent-with-sentiment-analysis
208+
209+
View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-with-sentiment-analysis.js).
210+
211+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/detect-intent-with-sentiment-analysis.js,samples/README.md)
212+
213+
__Usage:__
214+
215+
216+
`node samples/detect-intent-with-sentiment-analysis.js`
217+
218+
219+
-----
220+
221+
222+
223+
152224
### List-intents
153225

154226
View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/list-intents.js).

samples/detect-intent-event.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
/**
17+
* Detects intent using EventInput
18+
*
19+
* See https://cloud.google.com/dialogflow/cx/docs/quick/api before running the code snippet.
20+
*/
21+
22+
'use strict';
23+
24+
function main(projectId, location, agentId, event, languageCode) {
25+
// [START dialogflow_cx_v3_detect_intent_event_input_async]
26+
/**
27+
* TODO(developer): Uncomment these variables before running the sample.
28+
*/
29+
/**
30+
* Required. The name of the session this query is sent to.
31+
* Format: `projects/<Project ID>/locations/<Location ID>/agents/<Agent
32+
* ID>/sessions/<Session ID>` or `projects/<Project ID>/locations/<Location
33+
* ID>/agents/<Agent ID>/environments/<Environment ID>/sessions/<Session ID>`.
34+
* If `Environment ID` is not specified, we assume default 'draft'
35+
* environment.
36+
* It's up to the API caller to choose an appropriate `Session ID`. It can be
37+
* a random number or some type of session identifiers (preferably hashed).
38+
* The length of the `Session ID` must not exceed 36 characters.
39+
* For more information, see the sessions
40+
* guide (https://cloud.google.com/dialogflow/cx/docs/concept/session).
41+
* Note: Always use agent versions for production traffic.
42+
* See Versions and
43+
* environments (https://cloud.google.com/dialogflow/cx/docs/concept/version).
44+
*/
45+
46+
/**
47+
* Optional. The parameters of this query.
48+
*/
49+
// const queryParams = {}
50+
/**
51+
* Required. The input specification. See https://cloud.google.com/dialogflow/cx/docs/reference/rest/v3beta1/ConversationTurn#QueryInput for information about query inputs.
52+
*/
53+
// const event = 'name-of-event-to-trigger';
54+
55+
// Imports the Cx library
56+
const {SessionsClient} = require('@google-cloud/dialogflow-cx');
57+
/**
58+
* Example for regional endpoint:
59+
* const location = 'us-central1'
60+
* const client = new SessionsClient({apiEndpoint: 'us-central1-dialogflow.googleapis.com'})
61+
*/
62+
// Instantiates a client
63+
const cxClient = new SessionsClient();
64+
65+
async function detectIntentWithEventInput() {
66+
const sessionId = Math.random().toString(36).substring(7);
67+
const sessionPath = cxClient.projectLocationAgentSessionPath(
68+
projectId,
69+
location,
70+
agentId,
71+
sessionId
72+
);
73+
74+
// Construct detect intent request
75+
const request = {
76+
session: sessionPath,
77+
queryInput: {
78+
event: {
79+
event: event,
80+
},
81+
languageCode,
82+
},
83+
};
84+
85+
// Send request and receive response
86+
const [response] = await cxClient.detectIntent(request);
87+
console.log(`Event Name: ${event}`);
88+
89+
// Response message from the triggered event
90+
console.log('Agent Response: \n');
91+
console.log(response.queryResult.responseMessages[0].text.text[0]);
92+
}
93+
94+
detectIntentWithEventInput();
95+
// [END dialogflow_cx_v3_detect_intent_event_input_async]
96+
}
97+
98+
process.on('unhandledRejection', err => {
99+
console.error(err.message);
100+
process.exitCode = 1;
101+
});
102+
main(...process.argv.slice(2));
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
/**
17+
* Detects intent and returns a synthesized Text-to-Speech (TTS) response
18+
19+
* See https://cloud.google.com/dialogflow/cx/docs/quick/api before running the code snippet.
20+
*/
21+
22+
'use strict';
23+
24+
function main(
25+
projectId,
26+
location,
27+
agentId,
28+
sessionId,
29+
query,
30+
languageCode,
31+
outputFile
32+
) {
33+
// [START dialogflow_cx_v3_detect_intent_synthesize_tts_response_async]
34+
35+
// Imports the Cx library
36+
const {SessionsClient} = require('@google-cloud/dialogflow-cx');
37+
38+
/**
39+
* TODO(developer): Uncomment the following lines before running the sample.
40+
*/
41+
// const projectId = 'ID of GCP project associated with your Dialogflow agent';
42+
// const sessionId = `user specific ID of session, e.g. 12345`;
43+
// const query = `phrase(s) to pass to detect, e.g. I'd like to reserve a room for six people`;
44+
// const languageCode = 'BCP-47 language code, e.g. en-US';
45+
// const outputFile = `path for audio output file, e.g. ./resources/myOutput.wav`;
46+
47+
// Instantiates a Sessions client
48+
const sessionsClient = new SessionsClient();
49+
50+
// Define session path
51+
const sessionPath = sessionsClient.projectLocationAgentSessionPath(
52+
projectId,
53+
location,
54+
agentId,
55+
sessionId
56+
);
57+
const fs = require('fs');
58+
const util = require('util');
59+
60+
async function detectIntentSynthesizeTTSResponse() {
61+
// Configuration of how speech should be synthesized. See https://cloud.google.com/dialogflow/cx/docs/reference/rest/v3/OutputAudioConfig#SynthesizeSpeechConfig
62+
const synthesizeSpeechConfig = {
63+
speakingRate: 1.25,
64+
pitch: 10.0,
65+
};
66+
67+
// Constructs the audio query request
68+
const request = {
69+
session: sessionPath,
70+
queryInput: {
71+
text: {
72+
text: query,
73+
},
74+
languageCode: languageCode,
75+
},
76+
outputAudioConfig: {
77+
audioEncoding: 'OUTPUT_AUDIO_ENCODING_LINEAR_16',
78+
synthesizeSpeechConfig: synthesizeSpeechConfig,
79+
},
80+
};
81+
82+
// Sends the detectIntent request
83+
const [response] = await sessionsClient.detectIntent(request);
84+
// Output audio configurations
85+
console.log(
86+
`Speaking Rate: ${response.outputAudioConfig.synthesizeSpeechConfig.speakingRate}`
87+
);
88+
console.log(
89+
`Pitch: ${response.outputAudioConfig.synthesizeSpeechConfig.pitch}`
90+
);
91+
92+
const audioFile = response.outputAudio;
93+
// Writes audio content to output file
94+
util.promisify(fs.writeFile)(outputFile, audioFile, 'binary');
95+
console.log(`Audio content written to file: ${outputFile}`);
96+
}
97+
detectIntentSynthesizeTTSResponse();
98+
// [END dialogflow_cx_v3_detect_intent_synthesize_tts_response_async]
99+
}
100+
101+
process.on('unhandledRejection', err => {
102+
console.error(err.message);
103+
process.exitCode = 1;
104+
});
105+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)