Is there a more flexible way to record <Conference> calls?

The majority of the time, using the <Dial record=”true”> TwiML will allow you to record a <Conference>:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Dial record="true">
    <Conference>Recorded Conference</Conference>
  </Dial>
</Response>

However, there are some cases where Twilio’s standard methods for recording might not fit your use case. This FAQ demonstrates a hack which offers more a more precise and flexible way to record a <Conference>. With this hack, it is possible to record a <Conference> in it’s entirety, or stop and start recording of a <Conference> at will.

It works by dialing in another Twilio phone number which is set to <Record> as a participant of the <Conference>.

Step 1: Setting up a Phone Number to <Record>

Purchase a Twilio phone number and point that number’s Voice Request URL to the following TwiML. For the purposes of this example, we have purchased +17075551234:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Record action="http://example.com/handler.php" timeout="14400" />
</Response>

Step 2: Set up a <Conference>

Set up a different phone number to point to the following TwiML. This is the phone number which you will have your users call to be connected to the conference. In this example, we’ll use +15305559876 and we’ll call this TwiML file conference.xml

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Dial>
    <Conference>Awesome Conference</Conference>
  </Dial>
</Response>

Step 3: Set up a Handler

In Step 1 we had the following TwiML: <Record action="http://example.com/handler.php">. The ‘action’ attribute of the <Record> verb tells Twilio to make a request to the URL specified when the recording is complete. Twilio will pass parameters specific to the recording in addition to the standard Twilio request parameters which are associated with all calls. Your handler script should be set up to capture the CallSid and RecordingURL parameters in order to keep track of your recordings. Most likely, you will want to store these values in some sort of persistant manner. You’ll also want to associate these calls with a particular <Conference>, but how you decide to implement this will depend on your application.

Step 4: Dial the <Record> into the <Conference>

When you want to start recording, make an POST request to the Calls resource of the API. The ‘To’ number should be the number you set up in Step 1, so in our case, +17075551234.

$ curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/ACXXXXXXXXXX/Calls.json' \
-d 'From=%2B15305559876' \
-d 'To=%2B17075551234' \
-d 'Url=http%3A%2F%2Fexample.com%2Fconference.xml' \
-d 'StatusCallback=http%3A%2F%2example.com%2Fhandler.xml' \
-u ACXXXXXXXXXX:YYYYYYYYYYYYY

Be sure you record the CallSid which will be provided as part of the Twilio response to your request. We’re going to need that CallSid in Step 5.

Step 5: Stopping recording

In order to stop the recording, you will need to modify the call to +17075551234 and terminate it. You don’t need to wait until the end of the phone call to make this API request. Using this request, you can terminate the call at any time.

$ curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/ACXXXXXXXXXX/Calls/CAe1644abcde088b159577c5802d8be38.json' \
-d "Status=completed" \
-u ACXXXXXXXXXX:YYYYYYYYYYYYY

Optional Easier Way to Stop the <Conference>: make it moderated!

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Dial>
    <Conference endConferenceOnExit="true">Awesome Conference</Conference>
  </Dial>
</Response>

You should either use the API to terminate the call to +17075551234, or you should have a the <Conference> be moderated, otherwise the recording call will continue to run until it reaches the 4 hour time limit which we have placed on it.