arun kumar
arun kumar

Reputation: 737

Twilio Voice Api - Is it possible to record message and play the record message in the a flow

When a incoming call is received, I need to ask the caller to record a message. Then dial a number and play the recorded message when the receiver picks the call.

Is this possible?

Upvotes: 2

Views: 1125

Answers (1)

Alex Baban
Alex Baban

Reputation: 11732

Note: replace Account SID, Auth Token, phone numbers, website address with your values.

Step 1.

Answer incoming call and record message (http://somewebsite.xyz/recordMessage.php)

<?php    
    header("content-type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>

<Response>
    <Say>
        Please leave a message at the beep. 
        Press the star key when finished. 
    </Say>
    <Record 
        action="http://somewebsite.xyz/makeOutgoingCall.php" 
        maxLength="60"
        timeout="10"
        finishOnKey="*"
        />
    <Say>I did not receive a recording</Say>
</Response>

https://www.twilio.com/docs/api/twiml/record

Once the recording is finished Twilio will make a request to the 'action' URL and will pass the URL of the recording as a parameter named RecordingUrl ( $_REQUEST['RecordingUrl'] )

Step 2.

Make outgoing call ( http://somewebsite.xyz/makeOutgoingCall.php)

<?php
    header("content-type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>

<Response>
    <Say>Thank you for your message. Goodbye.</Say>
    <Hangup/>
</Response>

<?php
    // Include the Twilio PHP library
    require 'Services/Twilio.php';

    // Twilio REST API version
    $version = "2010-04-01";

    // Set our Account SID and AuthToken
    $sid = 'AC123';
    $token = 'abcd';

    // A phone number you have previously validated with Twilio
    $phonenumber = '4151234567';

    $recordingUrl = urlencode($_REQUEST['RecordingUrl']);

    // The URL Twilio will request when the call is answered            
    $twilioRequestUrl = "http://somewebsite.xyz/playRecordedMessage.php?RecordingUrl=".$recordingUrl;

    // Instantiate a new Twilio Rest Client
    $client = new Services_Twilio($sid, $token, $version);

    try {

        // Initiate a new outbound call
        $call = $client->account->calls->create(
            $phonenumber, // The number of the phone initiating the call
            '5101234567', // The number of the phone receiving call
            $twilioRequestUrl 
        );
        //echo 'Started call: ' . $call->sid;
    } catch (Exception $e) {
        //echo 'Error: ' . $e->getMessage();
    }

https://www.twilio.com/docs/libraries/php

https://www.twilio.com/docs/quickstart/php/rest/call-request

Step 3.

Play the recorded message (http://somewebsite.xyz/playRecordedMessage.php).

<?php

    // and play the recording back, using the URL that Twilio posted
    header("content-type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
    <Say>Take a listen to your message.</Say>
    <Play><?php echo $_REQUEST['RecordingUrl']; ?></Play>
    <Say>Goodbye.</Say>
</Response>

https://www.twilio.com/docs/quickstart/php/twiml/play-mp3-for-caller

https://www.twilio.com/docs/api/twiml/play

Upvotes: 4

Related Questions