Search code examples
node.jstwiliotwilio-twimlconference

how to start a conference with twilio?


Plan for my Programm: Someone is calling a Twilio Number, then I get called on my mobile. As long im not accept the call, the caller should hear music or something like that and if I pick up, a conference should start.

At the moment: Someone is calling the number and gets in queue with music, then my mobile gets called. Sounds great yet, but when i pick up we don't get connected.

So I guess I don't get properly how Twilio conference works, may you have some advices how to get this done step by step.


Solution

  • Twilio developer evangelist here.

    OK, you're building with Node.js and I know I've seen your code in other questions, but I'm going to start this one from scratch.

    With Twilio the flow that you described should look like this.

    • Person calls your Twilio number
    • Twilio makes an HTTP request to the number's Voice Request URL (which points at your application)
    • Your application now needs to do two things
      • Call your mobile number
      • Put the caller into a Conference with hold music playing
    • First you need to decide on the name of the Conference
    • Then to call your number, your application needs to make a call to the Twilio REST API to initiate the call to your mobile
      • You need to provide a callback URL in this call that will put you into the conference too, you can do this by including the conference name as a paramater in the URL
    • Your application also needs to return TwiML to put the caller into the Conference, using the same conference name
    • Finally, when you answer the phone Twilio will make an HTTP request to the URL you provided when initiating the call
    • That will request your URL which should respond with TwiML to put you into the same conference call

    Let's look at how we can do this in a Node.js Express application:

    You need two routes, one that will receive the initial request and one that will respond to the request made when you answer your phone. Check out the code and the comments below.

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

    Let me know if this helps at all.

    Update

    In the latest version of the Twilio Node.js library you cannot use twilio.TwimlResponse. Instead there are individual classes for voice and messaging. In this example the line

    var twiml = new twilio.TwimlResponse();
    

    Should be updated to:

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