Search code examples
phptwiliotwilio-php

How do I make this code say "Please hold" and play a custom mp3?


This is a conference line that begins when the moderator joins.

It works perfectly except I can't figure out how to make it say "Please hold, you'll be connected shortly." to all callers.

I also want to play a custom mp3 file for the hold music.

<?php
// Get the PHP helper library from twilio.com/docs/php/install

// this line loads the library
require_once '/var/www/one/conference/twilio/Twilio/autoload.php';
use Twilio\Twiml;

// Update with your own phone number in E.164 format
$MODERATOR = '+1347999999';

$response = new Twiml;

// Start with a <Dial> verb

$dial = $response->dial();

// If the caller is our MODERATOR, then start the conference when they
// join and end the conference when they leave
if ($_REQUEST['From'] == $MODERATOR) {
$dial->conference('My conference', array(
            'startConferenceOnEnter' => True,
            'endConferenceOnExit' => True,
            'beep' => True,
            'record' => True

            ));

} else {
// Otherwise have the caller join as a regular participant
$dial->conference('My conference', array(
            'startConferenceOnEnter' => False
            ));
}

print $response;

?>

Solution

  • Twilio developer evangelist here.

    In order to get a message at the start of the call, you need to use the TwiML <Say> verb before you use the <Dial>.

    To play custom music before the conference starts, you'll want to use the waitUrl attribute on the <Conference> tag. The waitUrl is a URL that points at either an MP3 or Wav file or something that returns TwiML which could include multiple <Say> or <Play> verbs.

    Here's an update of your code that includes a message at the start and a waitUrl for music (notably, the moderator doesn't need a waitUrl as they start the conference):

    // Get the PHP helper library from twilio.com/docs/php/install
    
    // this line loads the library
    require_once '/var/www/one/conference/twilio/Twilio/autoload.php';
    use Twilio\Twiml;
    
    // Update with your own phone number in E.164 format
    $MODERATOR = '+1347999999';
    
    $response = new Twiml;
    
    // Start with a welcome message
    $response->say("Please hold, you'll be connected shortly.");
    
    // Then add the <Dial> verb
    $dial = $response->dial();
    
    // If the caller is our MODERATOR, then start the conference when they
    // join and end the conference when they leave
    if ($_REQUEST['From'] == $MODERATOR) {
    $dial->conference('My conference', array(
                'startConferenceOnEnter' => True,
                'endConferenceOnExit' => True,
                'beep' => True,
                'record' => True
                ));
    
    } else {
    // Otherwise have the caller join as a regular participant
    $dial->conference('My conference', array(
                'startConferenceOnEnter' => False,
                'waitUrl' => 'http://twimlets.com/holdmusic?Bucket=com.twilio.music.classical'
                ));
    }
    
    print $response;
    

    Let me know if this helps at all.