将录制的Twilio音频发送给Lex
目前,我能够录制用户输入,将录制URL传递给所需的函数,并在本地下载音频文件。我试图对音频文件做的是获取一个缓冲区,将其发送给Lex,或者将其转换为Lex需要的格式 根据AWS文件,输入流参数值可接受以下值:将录制的Twilio音频发送给Lex,twilio,amazon-lex,Twilio,Amazon Lex,目前,我能够录制用户输入,将录制URL传递给所需的函数,并在本地下载音频文件。我试图对音频文件做的是获取一个缓冲区,将其发送给Lex,或者将其转换为Lex需要的格式 根据AWS文件,输入流参数值可接受以下值: var params = { botAlias: 'STRING_VALUE', /* required */ botName: 'STRING_VALUE', /* required */ contentType: 'STRING_VALUE', /* required */
var params = {
botAlias: 'STRING_VALUE', /* required */
botName: 'STRING_VALUE', /* required */
contentType: 'STRING_VALUE', /* required */
inputStream: new Buffer('...') || 'STRING_VALUE' || streamObject, /*required */
userId: 'STRING_VALUE', /* required */
accept: 'STRING_VALUE',
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); // successful response
});
根据twilio文档,音频文件看起来非常灵活
默认情况下,对RecordingUrl的请求将返回二进制WAV音频格式的录制。要请求以MP3格式录制,请在RecordingUrl中附加“.MP3”
我需要做什么才能为Lex获得正确格式的twilio录制音频?这仅仅是建立正确的Lex参数集的问题,还是我需要先做一些音频转换?如果有帮助的话,我正在NodeJS中编写这个应用程序,如果有帮助的话,我可以添加更多的代码 我可以通过从Twilio下载文件作为PCM并稍微更改参数来解决这个问题。另外,由于Twilio处理record谓词的方式,我需要在等待recordingStatusCallback发出时将调用转移到保持状态。我还向调用者发送一条文本,其中包含来自Lex的最终状态 我用于下载该文件的代码:
app.post('/processRecording', (request, response) => {
var https = require('https');
var fs = require('fs');
let callSID = request.body.CallSid;
let url = request.body.RecordingUrl;
var saveFile = new Promise(function(resolve, reject) {
let fileName = callSID+ ".pcm";
var file = fs.createWriteStream(fileName);
var request = https.get(url, function(response) {
response.pipe(file);
resolve();
});
});
});
const accountSid = 'YOUR ACCOUNT SID';
const authToken = 'YOUR AUTH TOKEN';
const client = require('twilio')(accountSid, authToken);
//Once the file is downloaded, I then fetch the call from the hold state using this code:
saveFile.then(function(){
client.calls(callSID)
.update({method: 'POST', url: '/updateCall'})
.then(call => console.log(call.to))
.done();
});
我的updateCall端点如下所示:
app.post('/updateCall', (request, response) => {
let lexruntime = new AWS.LexRuntime();
let recordedFileName = request.body.CallSid + '.pcm';
let toNumber = request.body.To;
let fromNumber = request.body.From;
let twiml = new Twilio.twiml.VoiceResponse();
let lexFileStream = fs.createReadStream(recordedFileName);
let sid = request.body.CallSid;
var params = {
botAlias: 'prod', /* required */
botName: 'OrderFlowers', /* 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: sid /* required */
};
params.inputStream = lexFileStream;
lexruntime.postContent(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
if (data.dialogState == "ElicitSlot" || data.dialogState == "ConfirmIntent" || data.dialogState == "ElicitIntent" ){
twiml.say(data.message);
twiml.redirect({
method: 'POST'
}, '/recordVoice');
response.type('text/xml');
response.send(twiml.toString());
}
else if (data.dialogState == "Fulfilled" ){
twiml.say(data.message);
response.type('text/xml');
response.send(twiml.toString());
client.messages.create({
to: toNumber,
from: fromNumber,
body: data.message
}).then(msg => {
}).catch(err => console.log(err));
}
else{
twiml.say(data.message);
response.type('text/xml');
response.send(twiml.toString());
}
});
});
recordVoice端点实际上是一个Twilio无服务器函数,但我认为这就是它作为express端点的外观:
app.post('/recordVoice', (request, response) => {
let twiml = new Twilio.twiml.VoiceResponse();
twiml.record({
action: '/deadAir',
recordingStatusCallback: '/processRecording',
trim: true,
maxLength: 10,
finishOnKey: '*'
});
twiml.say('I did not receive a recording');
response.type('text/xml');
response.send(twiml.toString());
});
/deadAir端点也是一个Twilio无服务器函数,但它看起来是这样的:
app.post('/deadAir', (request, response) => {
let twiml = new Twilio.twiml.VoiceResponse();
twiml.pause({
length: 60
});
response.type('text/xml');
response.send(twiml.toString());
});