Twilio创建了呼叫 - 发布参数?

时间:2017-02-18 18:54:25

标签: javascript node.js twilio

我正在尝试使用自定义变量初始化调用。

正如twilio所说,the call is initiated by making a post request to the url provided

var client = require('twilio')(accountSid, authToken); 

client.calls.create({
    url: "http://demo.twilio.com/docs/voice.xml",
    to: "+14155551212",
    from: "+1544444444"
}, function(err, call) {
    process.stdout.write(call.sid);
});

如果文件voice.xml有变量{{firstName}}

如何发布body.firstName?什么是在xml方面格式化的适当方式?谢谢

1 个答案:

答案 0 :(得分:0)

Twilio开发者传道者在这里。

如果您需要通过该URL传递信息,则可以将其作为URL参数进行传递。例如:

var client = require('twilio')(accountSid, authToken); 

client.calls.create({
    url: "http://example.com/voice.xml&firstName=Phil",
    to: "+14155551212",
    from: "+1544444444"
}, function(err, call) {
    process.stdout.write(call.sid);
});

然后,当您处理incoming POST request from Twilio时,您可以自己检索URL参数。如果您使用Express作为服务器,它看起来有点像这样:

var express = require('express');
var twilio = require('twilio');

var app = new express();

app.post('/voice.xml', function(request, response) {
  var firstName = request.query.firstName;
  var twiml = new twilio.TwimlResponse();
  twiml.say('Hello ' + firstName + '! How are you today?';
  response.set('Content-Type', 'text/xml');
  response.send(twiml.toString());
});

让我知道这是否有帮助。