Saturday 25 June 2022

Audio response in Amazon Lex

I have created a chatbot with Amazon Lex and I have created Rest API using Node.js with help of official doc. It's working perfectly fine. I'm receiving audioStream in response. It is an object. I don't know how to play that. Please help me out.

Code

const { LexRuntimeV2Client, RecognizeUtteranceCommand} = require("@aws-sdk/client-lex-runtime-v2");
const client = new LexRuntimeV2Client({ region: 'us-east-1' });


getResponse = async () => {
     const lexparams =
    {
        "botAliasId": "XXXXXXXXXXX",
        "botId": "XXXXXXXXXX",
        "localeId": "en_US",
        "inputStream": <blob XX XX XX ....>,
        "requestContentType": "audio/x-l16; sample-rate=16000; channel-count=1",
        "sessionId": "XXXXXXXXXXXX",
        "responseContentType": "audio/mpeg"
    }

    const command = new RecognizeUtteranceCommand  (lexparams);
    const response = await client.send(command);
    console.log(response);
}

getResponse();

Response

    "messages": [
      {
       "content": "How was the day ?",
       "contentType": "PlainText"
      }
    ],
audioStream: Http2Stream {
    id: 1,
    closed: false,
    destroyed: false,
    state: {
      state: 5,
      weight: 16,
      sumDependencyWeight: 0,
      localClose: 1,
      remoteClose: 0,
      localWindowSize: 65535
    },
    readableState: ReadableState {
      objectMode: false,
      highWaterMark: 16384,
      buffer: BufferList { head: null, tail: null, length: 0 },
      length: 0,
      pipes: [],
      flowing: null,
      ended: false,
      endEmitted: false,
      reading: false,
      constructed: true,
      sync: true,
      needReadable: false,
      emittedReadable: false,
      readableListening: false,
      resumeScheduled: false,
      errorEmitted: false,
      emitClose: true,
      autoDestroy: false,
      destroyed: false,
      errored: null,
      closed: false,
      closeEmitted: false,
      defaultEncoding: 'utf8',
      awaitDrainWriters: null,
      multiAwaitDrain: false,
      readingMore: true,
      dataEmitted: false,
      decoder: null,
      encoding: null,
      [Symbol(kPaused)]: null
    },
    writableState: WritableState {
      objectMode: false,
      highWaterMark: 16384,
      finalCalled: true,
      needDrain: true,
      ending: true,
      ended: true,
      finished: true,
      destroyed: false,
      decodeStrings: false,
      defaultEncoding: 'utf8',
      length: 0,
      writing: false,
      corked: 0,
      sync: false,
      bufferProcessing: false,
      onwrite: [Function: bound onwrite],
      writecb: null,
      writelen: 0,
      afterWriteTickInfo: null,
      buffered: [],
      bufferedIndex: 0,
      allBuffers: true,
      allNoop: true,
      pendingcb: 0,
      constructed: true,
      prefinished: true,
      errorEmitted: false,
      emitClose: true,
      autoDestroy: false,
      errored: null,
      closed: false,
      closeEmitted: false,
      [Symbol(kOnFinished)]: []
    }
  }

This is the response I'm getting. If you see my response, there is audioStream. That's what I'm trying to play. I don't find it anywhere in the document about playing the audiostream.



from Audio response in Amazon Lex

No comments:

Post a Comment