gpt4 book ai didi

twilio - 将录制的 Twilio 音频发送给 Lex

转载 作者:行者123 更新时间:2023-12-01 08:17:02 26 4
gpt4 key购买 nike

目前我可以录制用户输入,将录制 URL 传递给所需的函数,并在本地下载音频文件。我试图对音频文件做的是获取它的缓冲区以发送给 Lex 或将其转换为 Lex 需要的格式。

根据 AWS 文档,输入流参数值接受以下值:

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 文档,音频文件看起来非常灵活...

A request to the RecordingUrl will return a recording in binary WAV audio format by default. To request the recording in MP3 format, append ".mp3" to the RecordingUrl.

我需要做什么才能以适合 Lex 的正确格式获取 twilio 录制的音频?仅仅是构建正确的 Lex 参数集的问题,还是我需要事先进行一些音频转换?如果有帮助,我正在用 node js 编写这个应用程序,如果有帮助,我可以添加更多代码。

最佳答案

我能够通过从 Twilio 下载文件作为 PCM 并稍微更改我的参数来解决这个问题。此外,由于 Twilio 处理记录动词的方式,我需要在等待 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());
});

关于twilio - 将录制的 Twilio 音频发送给 Lex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51898951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com