使用Lex Runtime在Node JS中发布内容

时间:2018-08-17 13:10:36

标签: node.js aws-sdk audio-streaming chatbot amazon-lex

我正在尝试开发一个应用程序,该应用程序将获取音频文件,并使用aws-sdk将其发送到Amazon Lex,特别是将lexruntime发送到postContent。

当前,我在本地下载的网络上有音频文件,然后尝试将该音频文件的名称放入postContent参数中。但是,lex返回一个空的转录和引起注意的意图,意味着它无法正确处理我的音频文件。这是我下载/发布到lex的方法:

var file = fs.createWriteStream("file.wav");
var request = https.get(url, function(response) {
  response.pipe(file);
});

var params = {
    botAlias: 'prod', /* required */
    botName: 'OrderFlowers', /* required */
    //inputText: `${command}`, /* required */
    contentType: 'audio/x-l16; sample-rate=16000; channel-count=1', /*required */
    inputStream: './file.wav',
    accept: 'text/plain; charset=utf-8',
    userId: 'Test'/* required */
    //requestAttributes: any /* This value will be JSON encoded on your behalf with JSON.stringify() */,
    //sessionAttributes: any /* This value will be JSON encoded on your behalf with JSON.stringify() */
  };


lexruntime.postContent(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);  
}); 

上面从Lex得到以下响应:

{ contentType: 'text/plain;charset=utf-8',
  message: 'I didn\'t understand you, what would you like to do?',
  messageFormat: 'PlainText',
  dialogState: 'ElicitIntent',
  inputTranscript: '',
  audioStream: <Buffer > }

我知道音频文件已正确下载,并且将映射到正确的Lex意图,但是我认为我没有正确发送音频文件。如果最好不要从URL本地下载音频文件,则可以使用这种方法。

以下是亚马逊文档:https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/LexRuntime.html

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我需要将文件保存为其他格式,更改内容类型并创建读取流以发送到lex。此处的解决方案:

var file = fs.createWriteStream("file.pcm");
var request = https.get(url, function(response) {
  response.pipe(file);
});

var params = {
    botAlias: 'prod', /* required */
    botName: 'OrderFlowers', /* required */
    //inputText: `${command}`, /* required */
    contentType: 'audio/lpcm; sample-rate=8000; sample-size-bits=16; channel-count=1; is-big-endian=false',
    accept: 'text/plain; charset=utf-8',
    userId: 'Test'/* required */
    //requestAttributes: any /* This value will be JSON encoded on your behalf with JSON.stringify() */,
    //sessionAttributes: any /* This value will be JSON encoded on your behalf with JSON.stringify() */
  };

var lexFileStream = fs.createReadStream("file.pcm");
params.inputStream = lexFileStream;


lexruntime.postContent(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);  
});