在记录行动中获得空体 - twilio

时间:2017-05-09 05:26:55

标签: javascript node.js express twilio plivo

我的用例:

我的情况是,我正在制作用于收听播客的机器人用户将拨打twilio号码,机器人会询问你想听什么类型的播客,然后录制10秒

录制结束时,它会说用户在我们找到播客时请等待

我希望在我的webhook中进行录制,这样我就能找出来电者的心情并从我的数据库中找到合适的播客mp3文件并播放给来电者

我面临的问题:

我的所有webhooks中都是空洞的

我的代码:

var express = require("express");
var bodyParser = require("body-parser");
var VoiceResponse = require('twilio').twiml.VoiceResponse;

var app = express();
var port = (process.env.PORT || 4000);

app.use(bodyParser.json())

// helper to append a new "Say" verb with alice voice
function say(text, twimlRef) {
    twimlRef.say({ voice: 'alice' }, text);
}
// respond with the current TwiML content
function respond(responseRef, twimlRef) {
    responseRef.type('text/xml');
    responseRef.send(twimlRef.toString());
}

app.post("/voice", function (request, response, next) {
    console.log("request: ", request.body); //body is comming as empty object

    var phone = request.body.From;
    var input = request.body.RecordingUrl;
    var twiml = new VoiceResponse();
    console.log("phone, input: ", phone, input);

    say('What type of podcast would you like to listen. Press any key to finish.', twiml);
    twiml.record({
        method: 'POST',
        action: '/voice/transcribe',
        transcribeCallback: '/voice/transcribe',
        maxLength: 10
    });

    respond(response, twiml);
});

app.post("/voice/transcribe", function (request, response, next) {
    console.log("request: ", request.body); //body is comming as empty object

    var phone = request.body.From;
    var input = request.body.RecordingUrl;
    var twiml = new VoiceResponse();

    var transcript = request.body.TranscriptionText;    
    console.log("transcribe text: ", transcript);

    //here i will do some magic(Ai) to detect user mood and find an 
    //appropriate mp3 file from my database and send to twilio

    var mp3Url = 'https://api.twilio.com/cowbell.mp3'

    say('start playing.', twiml);
    twiml.play(mp3Url);

    respond(response, twiml);
});

app.listen(port, function () {
    console.log('app is running on port', port);
});

邮递员的API测试:

enter image description here

enter image description here

在twilio上添加网址作为webhook:

enter image description here

Heroku Logs:

enter image description here

1 个答案:

答案 0 :(得分:2)

Twilio开发者传道者在这里。

您使用的是body-parser,这很好。但是,您正在使用JSON解析器。 Twilio以application/www-x-form-urlencoded的格式发出请求,因此您应该更改:

app.use(bodyParser.json())

app.use(bodyParser.urlencoded({ extended: false }))

然后,您应该将已解析的主体视为request.body对象的一部分。

作为额外注释,transcribeCallback将异步发送到呼叫。因此,响应该请求返回TwiML将不会影响该调用。当你得到转录结果时,你需要modify the call in flight, by redirecting it to some new TwiMLupdating a call with Node.js is below的一个例子:

const accountSid = 'your_account_sid';
const authToken = 'your_auth_token';
const client = require('twilio')(accountSid, authToken);

client.calls('CAe1644a7eed5088b159577c5802d8be38')
  .update({
    url: 'http://demo.twilio.com/docs/voice.xml',
    method: 'POST',
  })
  .then((call) => console.log(call.to));