Simultaneous outgoing call to user using twilio in iOS Application - ios

I'ld like to make native call to user until user answer the call (Recursive call). If in case of voice mail than also doing same. Is it possible using Twilio in my iOS application.
Also i want to know whether call is answered by human or machine.
If yes than please suggest me some solution.

you could do this by leveraging StatusCallBack, Something like this:
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$client = new Services_Twilio($sid, $token);
$call = $client->account->calls->create("+18668675309", "+14155551212", "http://demo.twilio.com/docs/voice.xml", array(
"Method" => "GET",
"IfMachine"=>"Hangup",
"StatusCallback" => "https://www.myapp.com/check_call_status.php",
"StatusCallbackMethod" => "POST"
));
StatusCallBack: A URL that Twilio will send asynchronous webhook
requests to on every call event specified in the StatusCallbackEvent
parameter. If no event is present, Twilio will send completed by
default. If an ApplicationSid parameter is present, this parameter is
ignored. URLs must contain a valid hostname (underscores are not
permitted).
So in your StatusCallBack URL (https://www.myapp.com/check_call_status.php) you would have some logic to determine if the call was answered, and if it was answered by a human not an answering machine. Your StatusCallBack endpoint would look something like this:
<?php
if($_REQUEST['CallStatus'] == 'no-asnwer' || $_REQUEST['AnsweredBy'] == 'machine'){
// call again!
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$client = new Services_Twilio($sid, $token);
$call = $client->account->calls->create("+18668675309", "+14155551212", "http://demo.twilio.com/docs/voice.xml", array(
"Method" => "GET",
"IfMachine"=>"Hangup",
"StatusCallback" => "https://www.myapp.com/check_call_status.php",
"StatusCallbackMethod" => "POST"
));
}

Related

Twilio always returns "in-progress" regardless of actual outcome

I'm trying to detect if the number being dialed answers the phone or not, but it always returns the status CallStatus as "in-progress". For example when I get a call from Twilio and I let the phone ring I still see
[CallStatus] => in-progress
This is the code snippet I use to generate the call. As you can see I subscribed to answered, so I'm not even sure why it pings the statusCallback URL, but it does.
$client = new Twilio\Rest\Client($this->account_sid, $this->auth_token);
$call = $client->calls->create(
$to,
$from,
array(
'statusCallback' => 'https://statusCallbackURL',
'statusCallbackEvent' => ['answered'],
'fallbackUrl' => '',
'url' => '',
'timeout' => 60,
)
);

how to record inbound calls with twilio

Outbound calls in Twilio can be recorded by setting the TwiML passed to the client.calls.create() API method to have a <Conference> tag with the record attribute set to true. If recordingStatusCallback is set to a URL it'll post a notification to that URL when the recording is available.
How do you do this for inbound calls?
For my specific phone number, for Voice & Fax, I have, on twilio.com, "Accept Incoming" set to "Voice Calls", "Configure With" set to "TwiML App" and "TwiML App" set to my custom app. For that app I have the Voice Request URL set to a given URL.
When a call is incoming an HTTP POST is made to my TwiML App, which responds with an <Enqueue>. Later, a Conference Instruction Reservation is made in the client side javascript with ConferenceRecord set to true and ConferenceRecordingStatusCallback set to the same URL that we used with recordingStatusCallback for the outbound calls.
Here's what my "Conference Instruction Reservation" request payload looks like:
stdClass Object
(
[url] => https://taskrouter.twilio.com/v1/Workspaces/.../Tasks/.../Reservations/...
[method] => POST
[token] => ...
[params] => stdClass Object
(
[Instruction] => conference
[From] =>
[PostWorkActivitySid] =>
[Timeout] => 10
[To] =>
[EndConferenceOnExit] => true
[EndConferenceOnCustomerExit] => true
[ConferenceStatusCallback] => https://my.domain.tld/twilio/conference
[ConferenceStatusCallbackMethod] => POST
[ConferenceStatusCallbackEvent] => start,end,join,leave,mute,hold
[ConferenceRecord] => true
[ConferenceRecordingStatusCallback] => https://my.domain.tld/twilio/conference/recording
[ConferenceRecordingStatusCallbackMethod] => POST
[ConferenceRecordingStatusCallbackEvent] => completed
)
)
As a result of that Twilio calls the ConferenceStatusCallback URL multiple times but the ConferenceRecordingStatusCallback URL is never called.
Any ideas?
So idk why the "Conference Instruction Reservation" request approach wasn't working but calling client.calls.recordings.create() did the trick

how to stop record after x seconds with Twilio during an outboud call?

i want to record an outgoing call on twilio and stop it after x seconds
everything works except the timeout or maxlength
i have two files:
makecall.php
to_number = "+33123456789";
$client = new Client($account_sid, $auth_token);
$client->account->calls->create(
$to_number,
$twilio_number,
array(
"record" => True,
"maxLength" => 30,
"RecordingStatusCallback" => "callback.php",
"Url" => "answercall.php"
)
);
answercall.php
require '../vendor/autoload.php';
use Twilio\TwiML\VoiceResponse;
$response = new VoiceResponse();
$response->record(['maxLength' => 30]);
header('Content-Type: text/xml');
echo $response;;
i also tried in answercall
$response->pause(['length' => 30]);
$response->hangup();
but it doesnt stop recording !
maxLength is an attribute of the TwiML Record Verb rather then being an attribute of the Calls Resource.
You will need to use Recording API for this purpose.
Reference Stop a call Recording.

StatusCallBack Example

What I want to do is simply call a list of numbers. If a number do not answer, I will need to call an alternative number for that specific number.
So I have put together the code below, which makes the outbound call. But I have no sample of StatusCallback code that will be called for the status of the call being made and in the StatusCallback, it will call another number if the status was un-answered.
$account_sid = 'Some Value';
$auth_token = 'Some Value';
$client = new \Services_Twilio($account_sid, $auth_token);
$client->account->calls->create('+448008021203', '+441604280111', 'xyz.com/play_msg.html', array(
'Method' => 'GET',
"StatusCallback" => "xyz.com/call_events.php",
"StatusCallbackMethod" => "POST",
"StatusCallbackEvent" => array("answered", "completed"),
'Record' => 'false',
));
Can someone please share existing sample to do this?
Twilio evangelist here.
In your call_events.php file you're going to check the CallStatus parameter which is passed by Twilio in its HTTP request as a form-encoded parameter. If the value is not "completed" or "queued" start the next call:
$status = $_REQUEST['CallStatus']
if ($status != "completed" || $status!="queued") {
/* start the next call */
}
Hope that helps.

Setting a time limit to a twilio call

I am using the twiml below to create and send a call to my users. However I want to set a time limit to this call. I know it is possible when you use the verb, as shown here. But I don' know how to do it in my situation
try {
$to = '+' . $phone;
$client->account->calls->create(
"+17********",
$to,
$url,
array(
'Method' => "GET",
'FallbackMethod' => "GET",
'StatusCallbackMethod' => "GET",
'Record' => "false",
)
);
} catch (Exception $e) {
// log error
}
Use timeLimit
check this twilio docs
Have a look here on how to modify an active call: https://www.twilio.com/docs/api/rest/change-call-state
You are going to need to store the call resource and the time it started. At the appropriate time you will have to make a post to the resource and set it to completed. This will end the call.

Resources