Twilio call from browser to ios App - ios

I want to make call from browser to iOS app using twilio. And when i receive call at that time i need to open a custom view to accept and reject the call.
Is any body knows how to call TcDevice delegate method exa. -(void)device:(TCDevice*)device didReceiveIncomingConnection:(TCConnection*)connection
on receive a browser call ?
I am able to call from browser to device using php code but it will open the default calling screen and i need to open my custom screen and for that TcDevice delegate methods need to call.Please help.
Here is my php code to make a call
<?php
require 'Services/Twilio.php';
$version = "2010-04-01";
$sid = '******';
$token = '******';
$phonenumber = 'Twillonumber';
$client = new Services_Twilio($sid, $token, $version);
try {
$call = $client->account->calls->create(
$phonenumber,
'To_phonenumber',
'http://demo.twilio.com/welcome/voice/'
);
echo 'Started call: ' . $call->sid;
}
catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>
Thanks.

Twilio developer evangelist here.
I would recommend reading through the quickstart guide for Twilio Client on iOS. It takes you through setting up a server (which you will need for this) to receiving incoming calls on the device using the iOS library.
In short, you need to set up your iOS device as a client which has an identifier, and then use that identifier to dial into instead of the device's phone number. But get through the guide and you'll find out everything you need to know.

Related

Twilio caller name on receive programmable voice call in ios application

I’m using Twilio’s Programmable Voice in one of the projects. My primary requirement is to place VoIP class between mobile devices (no PSTN calls). I am able to place calls from one device to another, but unable to set appropriate Caller Name on Incoming Call screen.
Please guide me about how to display Caller’s name on receiving device. TVOCallInvite’s “from” value shows a mobile number “+18xxxxxxxx”, but I need to display the name of the caller.
.
We have created TwiML PHP file which contains the dialled client name and callerID (my twill number). We have assigned url of this file in TwiML app’s request URL (https://www.twilio.com/console/voice/twiml/apps/myappid).
We can assign name of the caller in CallKit’s “localizedCallerName”, but we are receiving phone number instead of caller’s identity.
Details:
Tutorial Followed : https://github.com/twilio/voice-quickstart-swift
TwilioVoice -> 2.0.0
iOS Version : 10.1
Device : iPhone 7 & iPhone 5S
Please find the attached screenshot.
Please note that I have searched google but I could not found the answer.
Thanks.
Below is my voice.php file
<?php
require __DIR__ . '/TwilioSdk/Twilio/autoload.php';
include('config.php');
use Twilio\Twiml;
$response = new Twiml;
if (isset($_REQUEST['To']) && strlen($_REQUEST['To']) > 0)
{
$number = htmlspecialchars($_REQUEST['To']);
$dial = $response->dial(array('callerId' => $callerid)); // callerid is +18XXXXXXXXX
if (preg_match("/^[\d\+\-\(\) ]+$/", $number))
{
$dial->number($number);
}
else
{
$dial->client($number);
}
}
else
{
$response->say("Thanks for calling!");
}
header('Content-Type: text/xml');
echo $response;
?>
Twilio console for call logs
Twilio developer evangelist here.
In order to get a name to appear on the iOS call screen in CallKit you need to pass a client identifier as the callerId rather than a phone number.
Client identifiers should be prefixed with client:. So in the code above, the important part is generating the TwiML, which should look like this:
$response->dial(array('callerId' => 'client:' . $clientName));
Note, you must use a number as a callerID if you a dialling a phone number. If you are dialling another client, then you can use a phone number or client identifier. If you want the name to appear in the application, then I recommend a client identifier as above.

How to integrate audio call(User to User) using twilio API?

I want to integrate user to user audio call feature using Twilio API, is it possible in Twilio? if yes can you please provide a tutorial.
Here I have added the code:
1. For get token from the Twilio
$.post(url, function(data) {
// Set up the Twilio Client Device with the token
Twilio.Device.setup(data.token);
});
and it returns the token using function
public function newToken(Request $request, ClientToken $clientToken)
{
$forPage = $request->input('forPage');
$twilio = config('services.twilio');
$applicationSid = $twilio['applicationSid'];
$clientToken->allowClientOutgoing($applicationSid);
if ($forPage === route('dashboard', [], false)) {
$clientToken->allowClientIncoming('support_agent');
} else {
$clientToken->allowClientIncoming('customer');
}
$token = $clientToken->generateToken();
return response()->json(['token' => $token]);
}
When I make a call following javascript function start
function callCustomer(phoneNumber) {
updateCallStatus("Calling " + phoneNumber + "...");
var params = {"phoneNumber": phoneNumber};
Twilio.Device.connect(params);
}
and then browser ask for the enable microphone and after allowing it plays the small audio say's that "Application error occurred, Good bye!".
Twilio voice call please refer the following documentation step by step
QuickStartDemo
Twilio developer evangelist here.
The TwiML connects the outgoing call to the other user. When you set up the TwiML Application for outgoing calls you need to set a voice URL. When you make the call using Twilio.Device.connect then Twilio Client will connect to Twilio, Twilio will then make an HTTP request to the voice URL of your TwiML app, passing along the parameters that you send, to find out what to do with the call.
You are passing in a phoneNumber parameter, so that will be passed to your application. You've tagged this question with PHP, so here's an example (using the Twilio PHP library) of what you could do to dial onto the phone number that you are passing.
<?php
require_once './vendor/autoload.php';
use Twilio\Twiml;
$phoneNumber = $_REQUEST['phoneNumber'];
$response = new Twiml();
$dial = $response->dial();
$dial->number($phoneNumber);
echo $response;
Check out the documentation on Twilio Client for more details on how this works.

Call someone with Twilio then disconnect me and play a message to other person

I'm trying to create a button on a webpage (on my presonal PHP webserver) that should connect me (either call my cellphone or via the webclient), then call a number, I then want to have an options to either hangup the call, or just disconnect me but play an mp3 to the other person and then hangup.
I'm not sure how to go about it. I created a TwiML, but how do I connect that to the existing call? Or is there a different way to do it?
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Play>https://something-something.twil.io/assets/recording1.mp3</Play>
<Hangup/>
</Response>
Thanks in advance.
Twilio developer evangelist here.
This is an ideal use case for Answering Machine Detection. With Twilio's answer machine detection you can set it to Enabled or DetectMessageEnd which means that you can use Twilio to work out whether a machine has answered the call and wait until the message is over then play it a message. Otherwise you can connect the call to yourself.
With PHP, you can generate the call like this:
use Twilio\Rest\Client;
// Your Account Sid and Auth Token from twilio.com/console
$sid = "YOUR_ACCOUNT_SID";
$token = "YOUR_AUTH_TOKEN";
$client = new Client($sid, $token);
$call = $client->calls->create(
"+14155551212", "+14158675309",
array(
"url" => "http://example.com/calls",
"MachineDetection" => "DetectMessageEnd"
)
);
Then, for your URL, you need to respond to the call depending on what the machine detected. You do that with the AnsweredBy parameter. Something like this, which dials your number if someone answers or speaks a message using <Say> if a machine answers:
<?php
if ($_REQUEST['AnsweredBy'] == "human") {
echo "<Response><Dial><Number>YOUR_NUMBER</Number></Dial></Response>";
} else {
echo "<Response><Say>Hello, this is my message</Say></Response>";
}
Let me know if that helps at all.
Edit
Without Answering Machine Detection
Ok, to do this without Answering Machine Detection I recommend you build yourself a dialler using Twilio Client JS. There is a quickstart guide here, so I won't go through how that works here.
Once you have a dialler you can use it to initiate the phone calls. The issue is then moving the voicemail calls to play the message. I would build two buttons, one that hangs up as if you've completed the call successfully and the other that plays the message instead. The first button is a simple function call to Twilio.Device.activeConnection().disconnect().
The second one needs a couple of things. The idea is that it will make a call to your server to redirect the other call to a new set of TwiML.
First up, you need the SID of the call you created. You can get that from the connection object you receive in response to calling connect.
var connection = Twilio.Device.connect({ number: "+1234567890" });
var callSid = connection.parameters.CallSid;
When you want to hangup and play a message you need to send this to your server. This is the SID of the parent call though, and you need to get the child call, the other leg. So, on your server you need to use the REST API to get the other call, then redirect it.
use Twilio\Rest\Client;
// Your Account Sid and Auth Token from twilio.com/console
$sid = "YOUR_ACCOUNT_SID";
$token = "YOUR_AUTH_TOKEN";
$client = new Client($sid, $token);
$calls = $client->calls->read(
array("ParentCallSid" => $_REQUEST['CallSid'])
);
// Loop over the list of calls, it should only have one call in it, and redirect the call to a URL that has the message TwiML
foreach ($calls as $call) {
$call->update(array(
"url" => "http://example.com/message.xml"
));
}
When you redirect the child call, the parent call will no longer be connected so it will hang up. The URL you redirect the child call to should contain the TwiML required to play the message to the machine using <Say> or <Play>.
I think I get what you're trying to do. You have a list of people you're trying to call. The app will call them and connect you. If you hear an answering machine, you want to press a key then hangup and move on to the next call. But after you hang up, that first outbound call stays online and leaves a .mp3 message to that recipient?
I believe one solution would be creating a conference with a bot.
Your app makes an outbound call to you, to the bot and to the recipient and puts everyone into a conference room called "room-timestamp" where timestamp is the current time. The bot is a twilio number that listens for a Gather dtmf. If you press 1, it will play message 1 then hang up. But because this is a conference, you can hangup at anytime and move on to the next call.
The bot could loop a few times and if no dtmf is detected, it will hang itself up.
This is all made easier using the new Outbound conference API where you can pass it the conference name instead of conference SID :
https://www.twilio.com/docs/api/rest/participant#list-post
Edit:
Connect three numbers to a conference room :
$uniqueid = time();
$call = $client->account->calls->create($officeline,$twilionum,
array("url" => "http://yourdomain/conference.php?id=$uniqueid"));
$call = $client->account->calls->create($botline,$twilionum,
array("url" => "http://yourdomain/conference.php?id=$uniqueid"));
$call = $client->account->calls->create($customerline,$twilionum,
array("url" => "http://yourdomain/conference.php?id=$uniqueid"));
This will connect three numbers to a conference room:
$officeline (your number),
$botline (twilio phone # of a bot that responds to dtmf)
$customerline (the customer you're calling)
conference.php just returns a conferenceID for calls to connect to:
header('Content-Type: text/xml');
$confid = $_REQUEST['id'];
echo<<<XMLOUT
<?xml version="1.0" encoding="ISO-8859-1"?>
<Response>
<Dial>
<Conference statusCallbackEvent="leave" statusCallback="killconference.php">$confid</Conference>
</Dial>
</Response>
XMLOUT;
killconference.php is called so that the conference can be terminated when there's only one person left. Just make sure your bot hangs up after playing something.
killconference.php
$theconference = $_REQUEST['ConferenceSid'];
$participants = $client
->conferences($theconference)
->participants
->read();
if (count($participants) == 1) {
$conference = $client
->conferences($theconference)
->fetch();
$conference->update(array(
"Status" => "completed"
));
}
your botline twilio number will be pointing to bot.php that responds to dtmf:
bot.php
header('Content-Type: text/xml');
$dtmf = isset($_REQUEST["Digits"]) ? $_REQUEST["Digits"] : "";
$playmore = "";
if ($dtmf == "1") {
$playmore = "<Say>Hey I just wanted to leave you a message </Say><Hangup/>\n";
}
if ($dtmf == "2") {
$playmore = "<Play>http://www.soundboard.com/mediafiles/22/224470-33a9f640-d998-45a3-b0c1-31c1687c2ae4.mp3</Play><Hangup/>\n";
}
echo<<<XMLOUT
<?xml version="1.0" encoding="ISO-8859-1"?>
<Response>
$playmore
<Gather action="bot.php" numDigits="1" timeout="30">
</Gather>
<Hangup/>
</Response>
XMLOUT;
The bot will stay on the line for 30 seconds, if no dtmf is entered it hangs itself up. Press 1 to leave the customer a message, 2 for Leroy Jenkins

How to make call through Twilio where two people will have live conversation?

I am trying to setup a call between two people and I am able to make a call but the person who pick up the call can listen pre-recorded voice mail. I want to have live conversation between these two people. I am not getting what should be the URL and how can I set it up.
My Sample PHP Code is -
require_once "application/helpers/Services/twilio-php-master/Twilio/autoload.php";
use Twilio\Rest\Client;
// Step 2: Set our AccountSid and AuthToken from https://twilio.com/console
$AccountSid = "Axxxxxxxxxxxxxxxxxxxxxc0";
$AuthToken = "xxxxxxxxxxxxxxxxxxxxxxxxx";
// Step 3: Instantiate a new Twilio Rest Client
$client = new Client($AccountSid, $AuthToken);
try {
// Initiate a new outbound call
$call = $client->account->calls->create(
// Step 4: Change the 'To' number below to whatever number you'd like
// to call.
"+91my_number",
//$_POST['to'],
// Step 5: Change the 'From' number below to be a valid Twilio number
// that you've purchased or verified with Twilio.
"+1_twilioverified_number",
// Step 6: Set the URL Twilio will request when the call is answered.
array("url" => "http://demo.twilio.com/welcome/voice/")
);
echo "Started call: " . $call->sid;
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
Is there anyone who can help me with this.
Thank you in advance. Awaiting for reply.
Twilio developer evangelist here.
Currently your code is working fine, the thing you need to change is the URL that is listed as "Step 6" in the comments. Currently that URL points to some TwiML that reads out a message.
Instead of reading that message, you will need to provide a URL that returns some TwiML that <Dial>s another <Number>. You see, when the first part of the call connects, Twilio makes an HTTP POST request to that URL to get the instructions for what to do next.
This URL needs to be available online for Twilio to make the HTTP request to. You can either deploy this TwiML to a server of yours, use a TwiML Bin from the Twilio console or test it out using ngrok on your local development machine.
The TwiML you want should look a bit like this:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial>
<Number>YOUR_NUMBER_HERE</Number>
</Dial>
</Response>
Let me know if that helps at all.

Hold, unhold feature for Twilio call which is already running

I've a Twilio call already routed to an agent via TaskRouter(Task, Reservation is already created, Agent has accepted the call) and now an agent needs Hold, unhold feature for the same Twilio call.
Can someone please provide best practice for this?
Can we do this by en-queuing a call to Workflow?if yes then, how should we redirect same call to the same agent for Un-hold feature?
Please let me know if anyone has code snippet available. I'm using PHP for this
You can handle this one of a few ways but the basic process is:
First, provide Twilio some TwiML that places the call into "hold" then
when you're ready, use the CallSid of the call and the REST API to redirect the live call.
<?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 = "YOUR_ACCOUNT_SID";
$token = "YOUR_AUTH_TOKEN";
$client = new Services_Twilio($sid, $token);
// Get an object from its sid. If you do not have a sid,
// check out the list resource examples on this page
$call = $client->account->calls->get("CALL_SID");
$call->update(array(
"Url" => "http://demo.twilio.com/docs/voice.xml",
"Method" => "POST"
));
echo $call->to;
To actually handle the "hold" you can use the <Enqueue> verb as you guessed, placing the call into a call queue. Then when you are ready, redirect that call back out of the queue to a new experience.
Or you can use <Play> and set the loop attribute to zero, which tell Twilio to loop audio indefinitely. And again, redirect the call when you are ready to remove the hold.

Resources