gpt4 book ai didi

node.js - 如何从 Twilio 录音通话中获取录音 ID?

转载 作者:太空宇宙 更新时间:2023-11-03 23:18:30 24 4
gpt4 key购买 nike

我的 twilio 代码是:

const express = require('express');
const VoiceResponse = require('twilio').twiml.VoiceResponse;

const app = express();
const PORT = process.env.PORT || 3000;
app.get('/health', (req, res) => {
res.send('ok')
})

// Returns TwiML which prompts the caller to record a message
app.post('/record', (request, response) => {
// Use the Twilio Node.js SDK to build an XML response
const twiml = new VoiceResponse();
twiml.say("Hi!");

// Use <Record> to record the caller's message
twiml.record();
console.log(twiml.toString())

response.send(twiml.toString());
});

// Create an HTTP server and listen for requests on port 3000
app.listen(PORT);

但我想知道录制 ID,以便我可以通过编程方式访问原始文件。我该怎么做?

最佳答案

要获取录制 ID (RecordingSid),您需要告诉 Twilio 一个 action URL,如下所示:

twiml.record({
action: '/finished'
});

您可以在此处阅读更多信息:( https://www.twilio.com/docs/voice/twiml/record#attributes )。另外,请阅读 recordingStatusCallback URL 属性,也许这也是您需要的。

然后,您需要解析 Twilio 将向您的应用程序发出的第二个请求的正文。

您可以在这里阅读更多相关信息:( https://www.twilio.com/blog/2016/07/how-to-receive-a-post-request-in-node-js.html )。

为此,您可以使用 body-parser,您可以通过 npm install body-parser 获得它。

录制 ID 将成为 body.RecordingSid 下参数的一部分。

无论如何,这里是对您的代码的粗略修改,以开始:

<小时/>
// npm install express body-parser
const express = require('express');
const bodyParser = require('body-parser');

const VoiceResponse = require('twilio').twiml.VoiceResponse;

const app = express();

// Tell express to use the body-parser middleware and to not parse extended bodies
app.use(bodyParser.urlencoded({
extended: false
}))

const PORT = process.env.PORT || 3000;
app.get('/health', (req, res) => {
res.send('ok')
})

// Returns TwiML which prompts the caller to record a message
app.post('/record', (request, response) => {
// Use the Twilio Node.js SDK to build an XML response
const twiml = new VoiceResponse();
twiml.say("Hi!");

// Use <Record> to record the caller's message
twiml.record({
action: '/finished'
});
console.log(twiml.toString())

response.send(twiml.toString());
});

app.post('/finished', function (req, res) {
const body = req.body;
res.set('Content-Type', 'text/plain');
res.send(``);
console.log(body);
console.log(body.RecordingSid);
});

// Create an HTTP server and listen for requests on port 3000
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
<小时/>

我希望这会有所帮助。

关于node.js - 如何从 Twilio 录音通话中获取录音 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52322083/

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