|
| 1 | +// Copyright 2020 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 | +// http://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 | +'use strict'; |
| 16 | + |
| 17 | +async function main( |
| 18 | + projectId, |
| 19 | + location, |
| 20 | + agentId, |
| 21 | + audioFileName, |
| 22 | + encoding, |
| 23 | + sampleRateHertz, |
| 24 | + languageCode |
| 25 | +) { |
| 26 | + // [START dialogflow_cx_detect_intent_audio] |
| 27 | + /** |
| 28 | + * TODO(developer): Uncomment these variables before running the sample. |
| 29 | + */ |
| 30 | + // const projectId = 'my-project'; |
| 31 | + // const location = 'global'; |
| 32 | + // const agentId = 'my-agent'; |
| 33 | + // const audioFileName = '/path/to/audio.raw'; |
| 34 | + // const encoding = 'AUDIO_ENCODING_LINEAR_16'; |
| 35 | + // const sampleRateHertz = 16000; |
| 36 | + // const languageCode = 'en' |
| 37 | + |
| 38 | + // Imports the Google Cloud Some API library |
| 39 | + const {SessionsClient} = require('@google-cloud/dialogflow-cx'); |
| 40 | + /** |
| 41 | + * Example for regional endpoint: |
| 42 | + * const location = 'us-central1' |
| 43 | + * const client = new SessionsClient({apiEndpoint: 'us-central1-dialogflow.googleapis.com'}) |
| 44 | + */ |
| 45 | + const client = new SessionsClient(); |
| 46 | + |
| 47 | + const fs = require('fs'); |
| 48 | + const util = require('util'); |
| 49 | + |
| 50 | + async function detectIntentAudio() { |
| 51 | + const sessionId = Math.random().toString(36).substring(7); |
| 52 | + const sessionPath = client.projectLocationAgentSessionPath( |
| 53 | + projectId, |
| 54 | + location, |
| 55 | + agentId, |
| 56 | + sessionId |
| 57 | + ); |
| 58 | + console.info(sessionPath); |
| 59 | + |
| 60 | + // Read the content of the audio file and send it as part of the request. |
| 61 | + const readFile = util.promisify(fs.readFile); |
| 62 | + const inputAudio = await readFile(audioFileName); |
| 63 | + |
| 64 | + const request = { |
| 65 | + session: sessionPath, |
| 66 | + queryInput: { |
| 67 | + audio: { |
| 68 | + config: { |
| 69 | + audioEncoding: encoding, |
| 70 | + sampleRateHertz: sampleRateHertz, |
| 71 | + }, |
| 72 | + audio: inputAudio, |
| 73 | + }, |
| 74 | + languageCode, |
| 75 | + }, |
| 76 | + }; |
| 77 | + const [response] = await client.detectIntent(request); |
| 78 | + console.log(`User Query: ${response.queryResult.transcript}`); |
| 79 | + for (const message of response.queryResult.responseMessages) { |
| 80 | + if (message.text) { |
| 81 | + console.log(`Agent Response: ${message.text.text}`); |
| 82 | + } |
| 83 | + } |
| 84 | + if (response.queryResult.match.intent) { |
| 85 | + console.log( |
| 86 | + `Matched Intent: ${response.queryResult.match.intent.displayName}` |
| 87 | + ); |
| 88 | + } |
| 89 | + console.log( |
| 90 | + `Current Page: ${response.queryResult.currentPage.displayName}` |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + detectIntentAudio(); |
| 95 | + // [END dialogflow_cx_detect_intent_audio] |
| 96 | +} |
| 97 | + |
| 98 | +main(...process.argv.slice(2)); |
| 99 | +process.on('unhandledRejection', err => { |
| 100 | + console.error(err.message); |
| 101 | + process.exitCode = 1; |
| 102 | +}); |
0 commit comments