javascript - 在记录 Action 中变得空虚 - twilio

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

使用 postman 进行 API 测试:

enter image description here

enter image description here

在 twilio 上添加 url 作为 webhook:

enter image description here

Heroku 日志:

enter image description here

最佳答案

此处为 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 TwiML当你得到转录结果时。 updating 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));

关于javascript - 在记录 Action 中变得空虚 - twilio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43861788/

相关文章:

javascript - NodeJS socket.io 应用程序在按下按钮时卡住

javascript - 如何在不替换现有字段的情况下更新 MongoDB 中的记录?

node.js - 如何在node js中设置可以在所有twig页面访问的全局变量

node.js - express.compress() 和 express.response Time() 不适用于 Controller 输出

node.js - Socket.io 在某些机器上无法工作

node.js - Vanilla Node 与 express

postgresql - 属性无法在 Sequelize Model Create 中正确插入

javascript - 切换显示/隐藏 html 内容

javascript - 将元素加载到每个容器时遇到问题

javascript - jquery - 转到下一个时更改输入值