Calling and connecting - twilio

I am trying to call two person at same time and want to join the calls. For example, call A and B, once one of them picks up say A , I want to play a message and connect the other person in this case B. So A and B communicates twilio is making the call and connect both of them. Is this possible?

Assuming we are using PHP for this implementation, it would look something like this.
Code to call person A:
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('/path/to/twilio-php/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$client = new Services_Twilio($sid, $token);
$person_a_phone = "+15555555555";
$verified_twilio_number = "+14444444444";
$person_b_phone = "+13333333333";
$call = $client->account->calls->create($verified_twilio_number, $person_a_phone, "http://callback.php?person_b_phone=$person_b_phone", array());
Code To Call person B (ie. callback.php):
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Play>http://url-to-play-your-message.com</Play>
<Dial timeout="10" record="true"><?php echo $_REQUEST['person_b_phone'] ?></Dial>
</Response>
References:
https://www.twilio.com/docs/api/rest/making-calls

Related

Unable to get custom parameters passed using Twilio connect function for outgoing and incoming calls

Am trying to create a call center with Twilio, am almost there but now am stuck because i can't get custom passed parameters.
My main aim is to allow customers to call but first they should provide their emails and names first, then click call-customer button, i want to receive the custom parameters on the agent's side.
Now i try to pass the parameters but i can't retrieve them.
Here is my code to make and receive calls and to pass and get the parameters
(Customer side)This code allows outgoing calls, customers to make calls to the call-center agents
require __DIR__ . '/vendor/autoload.php';
use Twilio\Jwt\ClientToken;
$accountSid = '';
$authToken = '';
$appSid = '';
$capability = new ClientToken($accountSid, $authToken);
$capability->allowClientOutgoing($appSid);
$token = $capability->generateToken();
So according to the documentation i should pass the custom parameters like this:
var params = {"name": "John", "email": "john#gmail.com"};
Twilio.Device.connect(params);
(Agent side) This code allows incoming calls from customers to agents.
$accountSid = '';
$authToken = '';
$capability = new ClientToken($accountSid, $authToken);
$capability->allowClientIncoming('joey');
$token = $capability->generateToken();
In the agent side i use this code to receive customer information or custom parameters.
According to the documentation a code to get custom parameters is this:
if (connection.customParameters.hasOwnProperty("name")) {
let displayName = connection.customParameters.get("name");
console.log(displayName)
}
if (connection.customParameters.hasOwnProperty("email")) {
let customerID = connection.customParameters.get("email");
console.log(customerID)
}
but i get undefined
So when a customer calls this twilio function en-queues the call and assign it to an operator
here is the code:
exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.VoiceResponse();
twiml.say(" Please hold, while we connect you to one of our available agent ");
twiml.enqueue({
workflowSid: context.WORKFLOW_SID
}).task({}, `{"selected_skill":"operator"}`);
callback(null, twiml);
};
Then from here an available operator will accept the task then dial an agent
The operator dials the client like this
{"skills":["operator"],"contact_uri":"client:joey"}
Please help
Thanks in advance
Based on my take on the associated documentation, it looks like the parameters are only sent to the Twilio client via TwiML, as shown here.
https://www.twilio.com/docs/voice/client/javascript/changelog#160-aug-29-2018
Added support for custom incoming parameters from TwiML as Map Connection.customParameters. When a TwiML application sends
custom parameters using the noun, these parameters will be
added to Connection.customParameters
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial>
<Client>
<Identity>alice</Identity>
<Parameter name="foo" value="bar"/>
<Parameter name="baz" value="123"/>
</Client>
</Dial>
</Response>
It sounds like you are using the Task Router JavaScript SDK, maybe you can add these parameters as attributes to the task, and then access those tasks from the Task Router client SDK?

Twillo,making call with REST API,call gets disconnect after receiving

In outgoing call,when make call using REST API then call gets disconnected after reading the twiml on the my_url that we pass when creating call.
I want to know what TWIML or what i do on that my_url to keep the call go as normal call(means talk to the person who I have called).
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$client = new Client($sid, $token);
$call = $client->calls->create(
"+14155551212", "+14158675309",
array("url" => "my_url")
);
echo $call->sid;
The twiml is
<Response>
<Say>Hello </Say>
</Response>
I want to connect the same caller.Like if agent A dail a user number then I want to connect the agent A with user.and I don't want to dail another number to connect with user

Make a call between two numbers not registered in twilio

There's some way of make a call between two of my users? I mean... I have a twilio acount with a registered number and I have to make a call to my client "Bill" so when he answer that, the call should be redirected to another client, that Bill choosed, let's say "Joe". So, Bill click's a button and he's phone rings, he answer that and start to call Joe. When some of them hangup, the all call should be over. Have someone ever made that? Help me! And I'm sorry about my bad english
(oh yeah, I'm using php for that)
This is just something simple to get you going, you can also look at connecting to a conference room https://www.twilio.com/docs/api/twiml/conference
You will need to use the Twilio PHP Helper Library (the "Services" folder from there) download from https://www.twilio.com/docs/libraries/php#install-via-zip-file
Project structure
/
/Services
/callBill.php
/callJoe.php
callBill.php
<?php
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<!-- // calling Bill -->
</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 = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// A phone number you have at Twilio
$phonenumber = '5557779999';
// The URL Twilio will request when the call is answered
$twilioRequestUrl = "http://somewebsite.xyz/callJoe.php";
// 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
'7779995555', // call Bill at 7779995555
$twilioRequestUrl
);
//echo 'Started call: ' . $call->sid;
} catch (Exception $e) {
//echo 'Error: ' . $e->getMessage();
}
?>
callJoe.php
<?php
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<!-- call and connect Joe at 9995557777 with Bill's caller ID-->
<Response>
<Pause length="2"/>
<Say>Please wait, I'm calling Joe.</Say>
<Dial callerId="7779995555">9995557777</Dial>
</Response>
Request http://somewebsite.xyz/callBill.php with your browser or with a click on a button.

Twilio: programmatically join conference and play <Say> command or <Play> sound file?

I have two users and I joined them both into a <Conference>.
I would like to have a robot join the <Conference> and then make an announcement.
There are two approaches I'm considering:
Take everyone in the conference, redirect them to a TwiML that plays a sound, and then move them back into the Conference.
Create a bot that somehow joins the Conference and plays TwiML, but it's not clear for me, from the documentation, how to do that.
Twilio developer evangelist here.
Either of those approaches will work, though will have slightly different effects. Redirecting will cut the conference regardless of who is speaking at the time, but a bot joining in may get spoken over. It depends on which will work better for your use case.
To do the redirect, you'll need to run through the list of Conference participants, redirect them by updating their call to a new URL and return TwiML from that URL that plays the sound and redirects back to your original Conference URL. Something like:
$sid = "{{ account_sid }}";
$token = "{{ auth_token }}";
$client = new Services_Twilio($sid, $token);
// Loop over the list of participants and redirect ($client->account->conferences->get(CONFERENCE_SID)->participants as $participant) {
$call = $client->account->calls->get($participant->call_sid);
$call->update(array(
"Url" => "http://example.com/conference_message"
));
}
Then your /conference_message endpoint would need TwiML like this:
<Response>
<Play>http://example.com/message.mp3</Play>
<Redirect>http://example.com/conference</Redirect>
</Response>
On the other hand, having a bot enter the room requires you to create a call to the conference number and supply a URL which points to the TwiML to play the message and then hangup. Like this:
$sid = "{{ account_sid }}";
$token = "{{ auth_token }}";
$client = new Services_Twilio($sid, $token);
$call = $client->account->calls->create(A_TWILIO_NUMBER, THE_CONFERENCE_NUMBER, "http://example.com/conference_message");
Then your /conference_message endpoint would return TwiML like this:
<Response>
<Play>http://example.com/message.mp3</Play>
<Hangup/>
</Response>
Let me know if this helps at all.

Pass Inbound call between twilio clients

i have several twilio client users
Sky - Kevin - Paul
A twilio registered number connects to callsky.xml by default / that code looking like.
<Response>
<Dial>
<Agent>sky</Agent>
</Dial>
</Response>
a call comes in and goes to sky, she then wants to pass that call over to Kevin or Paul.
how is this possible ?
tried:
$caid = $_REQUEST["CallSID"];
$client = new Services_Twilio($sid, $token);
$call = $client->account->calls->get($caid);
$url = 'http://thesite.com/twiml.xml';
$call->update(
'Url' => $url,
'Method' => "GET",
);
This is executed through Ajax, when sky presses a button with the destination users name on it.
<button onclick="divert('kevin')">Kevin</button>
Also: Oddly, the update command shows as a syntax error in my IDE.
Twilio evangelist here.
The update method takes an array, so that might be causing the syntax error:
https://www.twilio.com/docs/api/rest/change-call-state
I'd also recommened checking out the AppMonitor (https://www.twilio.com/user/account/developer-tools/app-monitor) to see if Twilio is logging and errors when trying to retreive the TwiML from your URL.
The URL you point the call at can return static XML, or you ca ndynamically generate it using the PHP helper library. Here is an example of using the <Dial> verb to dial a Client instance:
$response = new Services_Twilio_Twiml;
$dial = $response->dial(NULL, array(
'callerId' => '+14152223333'
));
$dial->client('client-id');
print $response;
This will output TwiML that looks like:
<Response>
<Dial callerId="+14152223333">
<Client>client-id</Client>
</Dial>
</Response>
Hope that helps.

Resources