Skip to content

Commit ec65dff

Browse files
aribraygcf-owl-bot[bot]
authored and
Ace Nassri
committed
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 7fc4083 commit ec65dff

9 files changed

+601
-0
lines changed

dialogflow-cx/detect-intent-event.js

+102
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));
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));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
* Trigger intent programmatically rather than as a result of natural language processing
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, intentId, languageCode) {
25+
// [START dialogflow_cx_v3_detect_intent_with_intent_input_async]
26+
/**
27+
* TODO(developer): Uncomment these variables before running the sample.
28+
*/
29+
30+
/**
31+
* const projectId = 'your-project-id';
32+
* const location = 'location';
33+
* const agentId = 'your-agent-id';
34+
* const languageCode = 'your-language-code';
35+
*/
36+
37+
/**
38+
* The input specification. See https://cloud.google.com/dialogflow/cx/docs/reference/rest/v3beta1/ConversationTurn#QueryInput for information about query inputs.
39+
*/
40+
// const intentId = 'unique-identifier-of-the-intent-to-trigger';
41+
42+
// Imports the Cx library
43+
const {
44+
SessionsClient,
45+
IntentsClient,
46+
} = require('@google-cloud/dialogflow-cx');
47+
/**
48+
* Example for regional endpoint:
49+
* const location = 'us-central1'
50+
* const client = new SessionsClient({apiEndpoint: 'us-central1-dialogflow.googleapis.com'})
51+
*/
52+
// Instantiates a Sessions client
53+
const sessionsClient = new SessionsClient();
54+
55+
// Instantiates an Intents client
56+
const intentsClient = new IntentsClient();
57+
58+
async function detectIntentWithIntentInput() {
59+
const sessionId = Math.random().toString(36).substring(7);
60+
61+
// Creates session path
62+
const sessionPath = sessionsClient.projectLocationAgentSessionPath(
63+
projectId,
64+
location,
65+
agentId,
66+
sessionId
67+
);
68+
69+
// Creates intent path. Format: projects/<Project ID>/locations/<Location ID>/agents/<Agent ID>/intents/<Intent ID>
70+
const intentPath = intentsClient.intentPath(
71+
projectId,
72+
location,
73+
agentId,
74+
intentId
75+
);
76+
77+
// Construct detectIntent request
78+
const request = {
79+
session: sessionPath,
80+
queryInput: {
81+
intent: {
82+
intent: intentPath,
83+
},
84+
languageCode,
85+
},
86+
};
87+
88+
// Send request and receive response
89+
const [response] = await sessionsClient.detectIntent(request);
90+
91+
// Display the name of the detected intent
92+
console.log('Intent Name: \n');
93+
console.log(response.queryResult.intent.displayName);
94+
95+
// Agent responds with fulfillment message of the detected intent
96+
console.log('Agent Response: \n');
97+
console.log(response.queryResult.responseMessages[0].text.text[0]);
98+
}
99+
100+
detectIntentWithIntentInput();
101+
// [END dialogflow_cx_v3_detect_intent_with_intent_input_async]
102+
}
103+
104+
process.on('unhandledRejection', err => {
105+
console.error(err.message);
106+
process.exitCode = 1;
107+
});
108+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)