当接到来电时,我需要要求调用者录制消息。然后调用一个号码,并在接听者接听电话时播放录制的消息。
这可能吗?
最佳答案
注意:将帐户 SID、身份验证 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 将向“action”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
关于Twilio Voice Api - 是否可以录制消息并在流程中播放录制消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37766211/