( projectId = 'YOUR_PROJECT_ID', sessionId = 'YOUR_SESSION_ID', query = 'YOUR_QUERY', languageCode = 'YOUR_LANGUAGE_CODE', outputFile = 'YOUR_OUTPUT_FILE' )
| 14 | |
| 15 | 'use strict'; |
| 16 | async function main( |
| 17 | projectId = 'YOUR_PROJECT_ID', |
| 18 | sessionId = 'YOUR_SESSION_ID', |
| 19 | query = 'YOUR_QUERY', |
| 20 | languageCode = 'YOUR_LANGUAGE_CODE', |
| 21 | outputFile = 'YOUR_OUTPUT_FILE' |
| 22 | ) { |
| 23 | // [START dialogflow_detect_intent_with_texttospeech_response] |
| 24 | // Imports the Dialogflow client library |
| 25 | const dialogflow = require('@google-cloud/dialogflow').v2; |
| 26 | |
| 27 | // Instantiate a DialogFlow client. |
| 28 | const sessionClient = new dialogflow.SessionsClient(); |
| 29 | |
| 30 | /** |
| 31 | * TODO(developer): Uncomment the following lines before running the sample. |
| 32 | */ |
| 33 | // const projectId = 'ID of GCP project associated with your Dialogflow agent'; |
| 34 | // const sessionId = `user specific ID of session, e.g. 12345`; |
| 35 | // const query = `phrase(s) to pass to detect, e.g. I'd like to reserve a room for six people`; |
| 36 | // const languageCode = 'BCP-47 language code, e.g. en-US'; |
| 37 | // const outputFile = `path for audio output file, e.g. ./resources/myOutput.wav`; |
| 38 | |
| 39 | // Define session path |
| 40 | const sessionPath = sessionClient.projectAgentSessionPath( |
| 41 | projectId, |
| 42 | sessionId |
| 43 | ); |
| 44 | const fs = require('fs'); |
| 45 | const util = require('util'); |
| 46 | |
| 47 | async function detectIntentwithTTSResponse() { |
| 48 | // The audio query request |
| 49 | const request = { |
| 50 | session: sessionPath, |
| 51 | queryInput: { |
| 52 | text: { |
| 53 | text: query, |
| 54 | languageCode: languageCode, |
| 55 | }, |
| 56 | }, |
| 57 | outputAudioConfig: { |
| 58 | audioEncoding: 'OUTPUT_AUDIO_ENCODING_LINEAR_16', |
| 59 | }, |
| 60 | }; |
| 61 | sessionClient.detectIntent(request).then(responses => { |
| 62 | console.log('Detected intent:'); |
| 63 | const audioFile = responses[0].outputAudio; |
| 64 | util.promisify(fs.writeFile)(outputFile, audioFile, 'binary'); |
| 65 | console.log(`Audio content written to file: ${outputFile}`); |
| 66 | }); |
| 67 | } |
| 68 | detectIntentwithTTSResponse(); |
| 69 | // [END dialogflow_detect_intent_with_texttospeech_response] |
| 70 | } |
| 71 | |
| 72 | main(...process.argv.slice(2)); |
no test coverage detected