Skip to content

Instantly share code, notes, and snippets.

@savelee
Created April 8, 2020 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save savelee/25fb2965a88bb18949ecd67b6bf7fc0e to your computer and use it in GitHub Desktop.
Save savelee/25fb2965a88bb18949ecd67b6bf7fc0e to your computer and use it in GitHub Desktop.
Dialogflow - Streaming
// 1)
async function detectIntentStream(audio, cb) {
// 2)
const stream = sessionClient.streamingDetectIntent()
.on('data', function(data){
// 3)
if (data.recognitionResult) {
console.log(
`Intermediate transcript:
${data.recognitionResult.transcript}`
);
} else {
console.log(`Detected intent:`);
cb(data);
}
})
// 4)
.on('error', (e) => {
console.log(e);
})
.on('end', () => {
console.log('on end');
});
// 5)
stream.write(request);
// 6)
await pump(
audio,
// 7)
new Transform({
objectMode: true,
transform: (obj, _, next) => {
next(null, { inputAudio: obj, outputAudioConfig: {
audioEncoding: `OUTPUT_AUDIO_ENCODING_LINEAR_16`
} });
}
}),
stream
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment