Node.js 如何从Twilio录音电话中获取录音ID?
我的twilio代码是:Node.js 如何从Twilio录音电话中获取录音ID?,node.js,twilio,Node.js,Twilio,我的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 w
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);
const express=require('express');
const VoiceResponse=require('twilio').twiml.VoiceResponse;
常量app=express();
const PORT=process.env.PORT | 3000;
app.get('/health',(req,res)=>{
res.send('ok')
})
//返回TwiML,提示调用者录制消息
app.post('/record',(请求、响应)=>{
//使用Twilio Node.js SDK构建XML响应
const twiml=新语音应答();
twiml.说“嗨!”;
//用于记录呼叫者的消息
twiml.record();
console.log(twiml.toString())
send(twiml.toString());
});
//创建HTTP服务器并侦听端口3000上的请求
app.listen(端口);
但我想知道录音ID,以便通过编程访问原始文件。我该怎么做?要获取录制ID(RecordingID),您需要告诉Twilio一个操作
URL,如下所示:
twiml.record({
action: '/finished'
});
你可以在这里阅读更多:()。另外,请阅读关于recordingStatusCallback
URL属性的内容,也许这也是您需要的
然后,您需要解析Twilio将对您的应用程序发出的第二个请求的主体
您可以在此处阅读更多有关此的信息:()
为此,您可以使用body parser
,这可以通过npm安装body parser
获得
录制ID将是body.RecordingSid
下参数的一部分
无论如何,下面是对代码的粗略修改,以开始:
//npm安装express正文解析器
const express=require('express');
const bodyParser=require('body-parser');
const VoiceResponse=require('twilio').twiml.VoiceResponse;
常量app=express();
//告诉express使用主体解析器中间件,不要解析扩展主体
app.use(bodyParser.urlencoded({
扩展:false
}))
const PORT=process.env.PORT | 3000;
app.get('/health',(req,res)=>{
res.send('ok')
})
//返回TwiML,提示调用者录制消息
app.post('/record',(请求、响应)=>{
//使用Twilio Node.js SDK构建XML响应
const twiml=新语音应答();
twiml.说“嗨!”;
//用于记录呼叫者的消息
twiml.record({
操作:'/finished'
});
console.log(twiml.toString())
send(twiml.toString());
});
应用程序post('/finished',函数(请求,回复){
const body=请求body;
res.set('Content-Type','text/plain');
res.send(``);
控制台日志(主体);
console.log(body.RecordingSid);
});
//创建HTTP服务器并侦听端口3000上的请求
应用程序侦听(3000,函数(){
log('端口3000上侦听的示例应用程序!')
})
我希望这有帮助
// 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!')
})