在logging行动得到空的身体 – twilio

我的用例:

我的情况是,我正在制作一个收听播客的机器人,在这个播客中用户会打电话给twilio号码,机器人会问你想听什么types的播客,然后logging10秒钟

当录音完成时,它说用户请等待,而我们正在寻找播客

我想在我的webhook录音,所以我会弄清楚来电心情,并从我的数据库中find适当的播客MP3文件,并发挥给来电

问题我面临:

我在所有的webhook中都得到空的身体

我的代码:

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); }); 

用邮递员testingAPI:

在这里输入图像说明

在这里输入图像说明

在twilio上添加url为webhook:

在这里输入图像说明

Heroku日志:

在这里输入图像说明

Twilio开发者在这里传道。

您正在使用body-parser ,但是,您正在使用JSONparsing器。 Twilio以application/www-x-form-urlencoded的格式发出请求,所以你应该改变:

 app.use(bodyParser.json()) 

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

那么你应该看到parsing的身体作为request.body对象的一部分。

作为一个额外的注意事项, transcribeCallbackasynchronous发送到调用,所以返回TwiML响应该请求将不会影响该调用。 您需要在飞行中修改呼叫,当您获得转录结果时,将其redirect到一些新的TwiML 。 下面是一个用Node.js更新呼叫的例子:

 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));