Twilio: Get parameters from outbound calls from recordings - twilio

I have a TwiMl like this in a Bin:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say voice="alice">Hello {{Name}}. You have ticket number {{TicketNum}} so please solve it.</Say>
</Response>
Then I use hmac authentication and launch the script to make the call using the following TWIML Bin url with query strings attached:
twiml_link="https://handler.twilio.com/twiml/EHxxxxxxxxxxx?Name=Bob&TicketNum=45"
I get the call with Bob and Ticket 45. After that I need to alert our incident tracking system that Bob has acknowledge the ticket 45 and is "on it." I can't because of architecture send something back to my system. I need to go out and query twilio again.
So I wrote this python snippet to hit the Twilio API to find all completed calls. :
from twilio.rest import Client
import sys, os
# Your Account Sid and Auth Token from twilio.com/console
account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)
calls = client.calls.list(status=completed)
for call in calls:
print(call.sid)
print(call.to)
print(call.status)
print(call.start_time)
print(call.duration)
print(call.annotation)
print(call.uri)
None of these commands can print Bob or the ticket number or the Twiml Bin URL or the query strings I used. Any tips? Thanks in Advance!

That approach isn't a reliable way to determine if the intended party answered the phone and acknowledge the message without "Human Detection". You can find more details here.
Alternative to AMD - "Human Detection"
https://support.twilio.com/hc/en-us/articles/223132567-Can-Twilio-tell-whether-a-call-was-answered-by-a-human-or-machine-#alternatives-to-amd
The dialed party could be a voicemail system, so querying the REST API to see if the call status was completed will provide misleading results if your goal is to determine the person acknowledged the ticket.
You could use a Twilio Function, https://support.twilio.com/hc/en-us/articles/115007737928-Building-apps-with-Twilio-Functions, to collect the result of the
Alan

Related

Twilio API: Transferring connection to a forwarded number?

Im placing an outbound call, and based on the automated message from whom Twilio is calling, it calls my phone with the connection.
Here is the script that places the phone call.
import os
from twilio.rest import Client
account_sid = "xxxx"
auth_token = "xxxx"
client = Client(account_sid, auth_token)
call = client.calls.create( url='http://myhost.com/rec.php',to='+1234',from_='+9876')
print(call.sid)
The phone call is placed, and the action script sends a TwiML gather response. Here's rec.php
echo '<Response>
<Gather input="speech"
partialResultCallback="http://myhost.com/partial.php"
action="http://myhost.com/finalresult.php">
</Gather>
</Response>
I've got the partial page logging the text. But when I forward a call, my phone rings for one second, then disconnects. There's no errors in the debugger either.
Here's partial.php
if(contains("To continue in English", $_REQUEST['UnstableSpeechResult'])){
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Response>
<Dial>+myPhoneNumber</Dial>
</Response>";
}
here is final result, which i think is called when the phone call is complete? im not sure.
if(contains("To continue in English", $_POST['SpeechResult'])){
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Response>
<Dial>+myPhoneNumber</Dial>
</Response>";
}
According to the documentation, partialResultCallback doesn't process TwiML.
The webhooks Twilio makes to your partialResultCallback are
asynchronous. They do not accept any TwiML in response. If you want to
take more actions based on this partial result, you need to use the
REST API to modify the call.
Not sure how your phone is ringing given the above. Any more details you can provide will help troubleshoot the issue.

Twilio API for making outbound calls with a speech stream

I have a scenario where say at 5.00 AM every morning, I have a server side script / batch job that wakes up, selects a phone number from a list based on an algorithm, places a call to that phone number and uses text-to-speech to deliver a customized message. I have 2 questions,
Which Twilio API can I use to achieve this? Bear in mind there is no app UI and all the code would be on the back end. Think NodeRED flow or a Python script that is made to run at a given time.
Instead of specifying the text in the TwiML, can I pass say an audio stream from Watson's Text to Speech to the appropriate Twilio API?
To do this, you would need to use the programmable voice API from Twilio. This lets you play audio files, text to speech, make and manipulate phone calls, etc. I have never used Watson Text-to-Speech, but, if it can output an audio file you can play that with Twilio TwiML.
Here's an example in Node.
npm install twilio
//require the Twilio module and create a REST client
var client = require('twilio')('ACCOUNT_SID', 'AUTH_TOKEN');
client.makeCall({
to:'+16515556677', // Any number Twilio can call
from: '+14506667788', // A number you bought from Twilio
url: 'url/to/twiml/which/may/have/WatsonURL' // A URL that produces TwiML
}, function(err, responseData) {
//executed when the call has been initiated.
console.log(responseData.from); // outputs "+14506667788"
});
The TwiML could look like this:
<Response>
<Play loop="1">https://api.twilio.com/cowbell.mp3</Play>
</Response>
This would play the cowbell sound from the Twilio API. Just a default sound. This could be easily generated to play a Watson sound file if you can get a URL for that.
You could do the same thing in Node, if you'd rather not build the XML manually.
var resp = new twilio.TwimlResponse();
resp.say('Welcome to Twilio!')
.pause({ length:3 })
.say('Please let us know if we can help during your development.', {
voice:'woman',
language:'en-us'
})
.play('http://www.example.com/some_sound.mp3');
If you were to take this toString() it would output formatted XML (TwiML):
console.log(resp.toString());
This outputs:
<Response>
<Say>Welcome to Twilio!</Say>
<Pause length="3"></Pause>
<Say voice="woman" language="en-us">Please let us know if we can help during your development.</Say>
<Play>http://www.example.com/some_sound.mp3</Play>
</Response>
Hopefully this clears it up for you.
Scott

How can I handle reaching voicemail using Twilio's <dial> verb

I know that on making a call Twilio can detect an answering machine, and react differently.
However if I use the <dial> verb, there's no obvious place to add this feature, even though it's essentially the same thing.
My intended flow is:
Customer enters their phone number
Twilio calls Customer and plays a voice message
Twilio dials an agent number, likely a mobile
If the Agent picks up, connect the customer to the agent
If the Agent busies the call or does not answer, call will likely go to Agent's voicemail.
Terminate call to Agent
Record voicemail from Customer
Email voicemail to Agent
From the official docs on the <Dial> verb (emphasis mine):
This is the simplest case for Dial. Twilio will dial 415-123-4567. If someone answers, Twilio will connect the caller to the called party. If the caller hangs up, the Twilio session ends. If the line is busy, if there is no answer, or if the called party hangs up, <Dial> exits and the <Say> verb is executed for the caller before the call flow ends.
<?xml version="1.0" encoding="UTF-8"?>
<!-- page located at http://example.com/simple_dial.xml -->
<Response>
<Dial>415-123-4567</Dial>
<Say>Goodbye</Say>
</Response>
Placing a <Record> verb after the <Say> verb sounds like what you are looking for. You can change the timeout from the default value of 30s like this:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial timeout="9001">415-123-4567</Dial>
<Say>Please leave a message</Say>
<Record action="/process_new_voicemail" />
</Response>
I am sure this is late, but hopefully it helps some one out. It sounds like you may just need to screen the call.
Basically, you can ask the "agent" that you dialed to accept the call and hang up if you do not receive input.
I am not sure what language you are using, but here is a great php/Laravel tutorial to explain:
https://www.twilio.com/docs/tutorials/walkthrough/ivr-screening/php/laravel
The key part is here:
$dialCommand = $response->dial(
['action' => route('agent-voicemail', ['agent' => $agent->id], false),
'method' => 'POST']
);
$dialCommand->number(
$numberToDial,
['url' => route('screen-call', [], false)]
);
Notice that the dial command uses the 'action' to specify a location that is sent a POST request should the call end i.e POST to /agent-voicemail.
Then, the number is dialed along with a 'url' parameter this is the location that will be requested after the agent has picked up but before connecting the two parties.
The /screen-call route then asks the agent to accept the call, if no input is received it hangs up and will make a POST request to the initial setup /agent-voicemail route.
This method will handle your scenario because if it goes to voicemail no input will be received and the call will end.

Dynamically set Twilio <Dial> timeLimit

I have an app which lets the users dial number(s) they want to add to a call. Each user is subjected to Balance they have in their account.
The dial is performed by using TwiML <Dial>
So as per my amount per minute rate i calculate the remaining balance in terms of seconds and set that as a timeLimit for <Dial>.
I want to do a simple thing like when the user is in a call and his call timeLimit is about to expire, I would want to charge them using my payment methods and If the charge was a success replenish the timeLimit for the same call.
Can this be done?
Twilio Developer Evangelist here.
There isn't a way to modify the timeLimit on a dial while the call is in progress. But I think I have a solution that could work for you.
Instead of dialing the number directly you could call into a conference with a timeLimit.
<Response>
<Dial timeLimit="30">
<Conference>YourCall</Conference>
</Dial>
</Response>
Then when their account is replenished you could modify the live call to redirect to a TwiML url that rejoins the conference call with the new timeLimit:
<?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 = "{{ sid }}";
$token = "{{ 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://youserver.com/conference.xml",
"Method" => "POST"
));
echo $call->to;
Probably easier is to use the https://www.twilio.com/docs/api/rest/change-call-state function of the Twilio REST API.The REST API is asynchronous.
In your situation you can do it as follows:
Dial timeLimit=[Max] (for an unlimited time, 4 hours is the max)
After a while, try to recharge the account.
On recharge success: Do nothing, the call persists.
On recharge failure: Disconnect by executing the change-call-state function of the Twilio REST API. You could even play an audio file or do other stuff before disconnect. For example ask the caller to verify its account because recharge failed or something.

Twilio initiate outbound call that connects agent phone before dialing target number

I want to create a help desk web page in which an agent can click a link to initiate an outbound call to a target number. I understand how to use the Web Client to make that happen, but for an agent who doesn't have bandwidth to support VoIP, I'd like Twilio to call the agent's phone number then dial the target number.
The experience would be much like using Google Voice with Google Chat/Hangout client -- Google Voice calls your number/client, then initiates a call to the target.
Also, if both agent and target phone numbers are domestic landlines, would this scenario incur 2X the per minute landline fees?
I'm not looking for code necessarily, but rather an answer based on Twilio APIs and Twiml concepts.
Twilio evangelist here.
Sounds like you are looking to create "Click to Call". Here is some code from our docs that shows how to do this:
https://www.twilio.com/docs/howto/click-to-call
The basics are:
Use the REST API to initiate an outbound call. When that call is answered Twilio is going to make an HTTP request to some URL that you told about in your initial REST request. That URL's job is to return TwiML that contains the <Dial> verb which tells Twilio to dial the second phone number and bridge the two call legs together.
For domestic US calls, the total cost is going to be 4 cents / minute. 2 cents per each leg, since each leg is considered outbound. See Example 4 on this page:
https://www.twilio.com/help/faq/voice/how-much-am-i-charged-for-call-forwarding
Hope that helps.
Simple/Direct Twilio Calls Agent->Call
original url: https://www.twilio.com/docs/quickstart/php/rest/call-request#call-end-callback
First file loaded from browser:
use Twilio\Rest\Client;
// Step 2: Set our AccountSid and AuthToken from https://twilio.com/console
$AccountSid = "SID";
$AuthToken = "AuthTok";
// Step 3: Instantiate a new Twilio Rest Client
$client = new Client($AccountSid, $AuthToken);
try {
// Initiate a new outbound call
$call = $client->account->calls->create(
"+12125551111",// connect this number(Agent)
// that you've purchased or verified with Twilio.
"+12135554646",// caller id for call
// Set the URL Twilio will request when the call is answered.
array("url" => "http://example.com/call_them.php")
);
echo "Started call: " . $call->sid;
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
call_them.php:
<?php
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
//inside dial.. actual number you want to reach
?>
<Response>
<Dial>+18185556363</Dial>
</Response>
Thank you for the great answer, #user3229526, it worked like a charm.
In order to unhardcode the number to call, just append the number you wish to call as a URL parameter in the Twilio Requst URL
array("url" => "http://example.com/call_them.php?number=1234567890")
And edit call_them.php to accept that parameter
<Response>
<Dial>
<?php echo '+1'. $_GET['number']; ?> // +1 for country code
</Dial>
</Response>

Resources