当接到来电时,我需要让来电者录制一条消息。然后拨打一个号码并在接听者接听电话时播放录制的消息。
这可能吗?
注意:将 Account SID、Auth Token、电话号码、网站地址替换为您的值。
步骤1。
接听来电并记录留言 ( 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
录制完成后,Twilio 将向“操作”URL 发出请求,并将录制的 URL 作为名为RecordingUrl
( $_REQUEST['RecordingUrl']
)的参数传递
第2步。
拨出电话 ( 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
步骤 3。
播放录制的信息 ( 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