如何与twilio开始会议?

时间:2016-03-16 08:55:48

标签: node.js twilio twiml conference

计划我的程序:有人打电话给Twilio号码,然后我的手机就被叫了。只要我不接受电话,来电者应该听到音乐或类似的东西,如果我拿起,会开始会议。

目前: 有人打电话给这个号码,然后用音乐排队,然后我的手机就被叫了。听起来不错,但是当我拿起时我们没有联系。

所以我想我不能正确地了解Twilio会议是如何运作的,如果你有一些建议如何逐步完成这项工作。

2 个答案:

答案 0 :(得分:2)

Twilio开发者传道者在这里。

好的,你正在使用Node.js构建,我知道我已经在其他问题中看到了你的代码,但我将从头开始这个。

使用Twilio,您描述的流程应如下所示。

  • 人称您的Twilio号码
  • Twilio向号码的语音请求URL(指向您的应用程序)发出HTTP请求
  • 您的应用程序现在需要做两件事
    • 拨打您的手机号码
    • 将来电者置于会议中,播放音乐
  • 首先,您需要决定会议的名称
  • 然后要拨打您的号码,您的应用程序需要拨打Twilio REST API以启动对您的移动电话的呼叫
    • 您需要在此次通话中提供回拨网址,这也会让您加入会议,您可以通过在网址中将会议名称包含为参数来实现此目的
  • 您的应用程序还需要返回TwiML以使用相同的会议名称将呼叫者加入会议
  • 最后,当您接听电话时,Twilio会向您在发起呼叫时提供的URL发出HTTP请求
  • 这会请求您的网址,该网址应该响应TwiML,让您进入同一个电话会议

让我们看看我们如何在Node.js Express应用程序中执行此操作:

您需要两条路线,一条路线将接收初始请求,另一条路线将响应您接听电话时发出的请求。查看下面的代码和评论。

var express = require("express");
var bodyParser = require("body-parser");
var twilio = require("twilio");

var app = express();
app.use(bodyParser.urlencoded({ extended: true }));

var client = twilio(YOUR_ACCOUNT_SID, YOUR_AUTH_TOKEN);

// This is the endpoint your Twilio number's Voice Request URL should point at
app.post('/calls', function(req, res, next) {
  // conference name will be a random number between 0 and 10000
  var conferenceName = Math.floor(Math.random() * 10000).toString();

  // Create a call to your mobile and add the conference name as a parameter to
  // the URL.
  client.calls.create({
    from: YOUR_TWILIO_NUMBER,
    to: YOUR_MOBILE_NUMBER,
    url: "/join_conference?id=" + conferenceName
  });

  // Now return TwiML to the caller to put them in the conference, using the
  // same name.
  var twiml = new twilio.TwimlResponse();
  twiml.dial(function(node) {
    node.conference(conferenceName, {
      waitUrl: "http://twimlets.com/holdmusic?Bucket=com.twilio.music.rock",
      startConferenceOnEnter: false
    });
  });
  res.set('Content-Type', 'text/xml');
  res.send(twiml.toString());
});

// This is the endpoint that Twilio will call when you answer the phone
app.post("/join_conference", function(req, res, next) {
  var conferenceName = req.query.id;

  // We return TwiML to enter the same conference
  var twiml = new twilio.TwimlResponse();
  twiml.dial(function(node) {
    node.conference(conferenceName, {
      startConferenceOnEnter: true
    });
  });
  res.set('Content-Type', 'text/xml');
  res.send(twiml.toString());
});

让我知道这是否有帮助。

<强>更新

在最新版本的Twilio Node.js库中,您无法使用twilio.TwimlResponse。相反,语音和消息传递有各自的类。在这个例子中的行

var twiml = new twilio.TwimlResponse();

应更新为:

var twiml = new twilio.twiml.VoiceResponse();

答案 1 :(得分:1)

我自己解决了这个问题,你需要用会议回答两个参与者,当对方接听电话时它会自动启动

示例

resp.say({voice:'woman'}, 'Welcome to our hotline. This could take a moment, please wait.')
   .dial({},function(err){
        this.conference('example');
});