Passing custom CNAM through Twilio SIP Domain - twilio

I have a Twilio phone number configured to direct inbound calls to a PHP webhook. The webhook uses some of the addon information to try and find a useful caller name. I'm also using Twilio's built-in CNAM lookups, but they don't work right in Canada (I always get the caller's number as their name).
The webhook is designed to forward calls to a Twilio SIP Domain first, where I expect I'll be answering most of the calls. Other calls, if deemed urgent, will be forwarded via PSTN.
I've reached the point where I can pull out a relevant name, but I'm having difficulty trying to forward that information to my FXS (HT802). As per the device's documentation:
http://www.grandstream.com/sites/default/files/Resources/ht80x_administration_guide.pdf
Auto: When set to “Auto”, the HT801/HT802 will look for the caller ID in the order of P-Asserted Identity Header, Remote-Party-ID Header and From Header in the incoming SIP INVITE
I'm not able to find a means to pass these headers via a SIP noun in TwiML. Based on Twilio's documentation:
https://www.twilio.com/docs/voice/twiml/sip#custom-headers
UUI (User-to-User Information) header can be sent without prepending x-
https://www.twilio.com/docs/voice/api/sending-sip#sip-x-headers
If you send headers without X- prefix, Twilio will not read the header. As a result, the header will not be passed in the output.
For context, here's a reduced snippet of the PHP code I'm using so far. Note: I'm not actually doing anything with the $callerName value yet.
<?php
// Simple "starting value", in case we can't resolve the name.
// (will also resolve the numbers used for unknown/blocked IDs)
$callerName = FriendlyFormatPhoneNumber($_POST['From']);
use Twilio\Twiml;
$addOns = null;
if (array_key_exists('CallerName', $_POST)) {
$callerName = $_POST['CallerName'];
} elseif (array_key_exists('AddOns', $_POST)) {
$addOns = json_decode($_POST['AddOns']);
$teloName = $addOns->results->telo_opencnam->result->name;
// If we pulled a telo name, and it doesn't seem to be a phone number
// (in case that could happen), use the telo name.
if (isset($teloName) && preg_match('/.*[0-9]{4,}, $teloName') == 0) {
$callerName = $teloName;
}
}
$response = new TwiML;
$dialParams = array(,
'timeout' => 20,
'hangupOnStar' => false,
'answerOnBridge' => true,
'action' => API_BASE_URL . '/dial-callback.php'
);
$dialer = $response->dial($dialParams);
$dialer->sip('sip:101#mytwiliodomain.sip.us1.twilio.com;transport=tls');
echo $response;
Long story short: How do I pass a custom caller name to my SIP devices using TwiML and the Twilio SIP Domains? I don't want to overwrite the number, just the name. And only on the inbound calls to the devices registered to my Twilio SIP domain.
In case it helps: Don't worry about translating to PHP if that's not your field; I can translate from TwiML :)

Unfortunately, this is not possible with Twilio SIP Domains. Currently, there is no way to set the Caller Name via TwiML.

Related

Twilio Conference Call

I was able to implement basic Voice Conferencing but I feel my implementation may be lacking.
client = Client('ACxxxxxxxx', '34xxxxxxxxx')
#app.route('/', methods=["GET", "POST"])
def home():
form = ConferenceList() #form made using flask-wtf
if form.validate_on_submit():
contact_1 = form.data['contact_1'] #callee1
contact_2 = form.data['contact_2'] #callee2
from_ = form.data['from_'] #caller
response = VoiceResponse()
with Dial() as dial:
if from_ == MODERATOR:
dial.conference(
'Conf',
start_conference_on_enter=True,
end_conference_on_exit=True
)
else:
dial.conference('Conf', start_conference_on_enter=False)
response.append(dial)
'''here I feel could be a bottleneck'''
add_user(contact_1, conference_name='Conf', label='laed#1')
add_user(contact_2, conference_name='Conf', label="consumer")
return Response(str(response), 'text/xml')
return render_template('hello.html', form=form)
def add_user(contact, conference_name, label):
participant = client.conferences(conference_name).\
participants.create(
label=label, #label for participant
beep='onEnter',
record=True,
from_='from_', #same as above
to=str(contact)
)
if __name__ == '__main__':
app.run(debug=True, port=8000)
Basically the submit button triggers the / endpoint and the conference starts.
I feel there could be an issue with this implementation as I plan on cleaning it up and pushing to production (salesperson can make a conference call to leads on the app). Is there something I could have done better?
The voice(one client-one client) utilizes the Twilio Voice SDK, it there a way I could tweak it for conferencing?
You only need to respond with TwiML to a webhook from Twilio. In this case it appears that you are responding with TwiML to your application's front end when a submit button is pressed.
So, you can drop all the TwiML:
#app.route('/', methods=["GET", "POST"])
def home():
form = ConferenceList() #form made using flask-wtf
if form.validate_on_submit():
contact_1 = form.data['contact_1'] #callee1
contact_2 = form.data['contact_2'] #callee2
'''here I feel could be a bottleneck'''
add_user(contact_1, conference_name='Conf', label='laed#1')
add_user(contact_2, conference_name='Conf', label="consumer")
return "whatever"
Since the settings you are trying to apply in the TwiML do not apply to the participants joining the conference, you need to adjust your add_user function to use them. In this case, the startConferenceOnEnter and endConferenceOnExit parameters for the moderator should be sent when you create the participant. It might be easier to write two methods, add_user and add_moderator, to make things clear:
def add_user(contact, conference_name, label):
participant = client.conferences(conference_name).\
participants.create(
label=label, #label for participant
beep='onEnter',
record=True,
from_='from_', #same as above
to=str(contact),
start_conference_on_enter=False
)
def add_moderator(contact, conference_name, label):
participant = client.conferences(conference_name).\
participants.create(
label=label,
beep='onEnter',
record=True,
from_='from_',
to=str(contact),
start_conference_on_enter=True,
end_conference_on_exit=True
)
Then call different functions for the different participants:
add_moderator(contact_1, conference_name='Conf', label='laed#1')
add_user(contact_2, conference_name='Conf', label="consumer")
When you call add_user or add_moderator it will make an API request and slow down your server response. If you wanted to offload those requests to a worker, that would make your response quicker. But for 2 API requests, it is likely not a problem.
One other thing you might want to consider is the consumer experience. If they answer the phone before your agent does, then they will be greeted with hold music. You might want to architect it so that the application calls the agent first and only once they have picked up it then dials the consumer. Just worth considering.
Edit
After further explanation, you are now telling me that you want to make the call from the browser using the Twilio Voice SDK for JS.
To make outbound calls with the JS SDK you need to create an access token which includes an outgoing application sid, which refers to a TwiML application. That TwiML application has a voice URL. When you place the call with the SDK, Twilio makes a webhook request to the voice URL of your TwiML app. Your application can perform actions and return TwiML to tell Twilio what to do with the call.
When you create the call with the JS SDK you can pass parameters to the call.
const device = new Device(token);
const call = await device.connect({
params: {
To: ["+15551234567", "+145557654321"]
}
});
Those parameters are sent with the webhook request to your TwiML App voice URL. You can then use a response very similar to your original code to respond here, because you need to return TwiML to the request from Twilio, and start calls to the other participants in the call.
#app.route('/conference', methods=["POST"])
def conference():
const numbers = request.form["To"]
response = VoiceResponse()
with Dial() as dial:
dial.conference(
'Conf',
start_conference_on_enter=True,
end_conference_on_exit=True
)
response.append(dial)
for number in numbers:
add_user(number, conference_name='Conf')
return Response(str(response), 'text/xml')
This code receives the To parameter, a list of numbers to dial into this conference, builds the TwiML response that will put the browser caller into a conference call, places outbound calls to the numbers to dial them into the conference and then returns the TwiML to Twilio. The dialler in the browser will start the conference and the other participants will arrive in the conference when they answer the phone.
In this case you don't make the request to your server yourself, you use the JS SDK to trigger the call and let Twilio make the request to your server. As mentioned in the comments, you may want to offload the API calls to create participants to a background job so that you can respond to the webhook request quicker, but that is beyond the scope of this answer.

On Asterisk PBX get sip header for outgoing calls

This is how my dialplan (/etc/asterisk/extensions.conf) looks like:
[default]
exten => _X.,1,NoOp(New call from ${EXTEN} ! )
same => n,NoOp( The header X-Twilio-CallSid = ${SIP_HEADER(X-Twilio-CallSid)})
same => Dial(SIP/SomePeer)
... etc
Thanks to the function SIP_HEADER I am able to get the id of the call that my provider sends me. This is the first packet I capture using WireShark:
In other words ${SIP_HEADER(X-Twilio-CallSid)} = ACbccc967c48dda15d8d1c9b34961d19a0
This works great for incoming calls. Now my problem is for outgoing calls. The sip header X-Twilio-CallSid does not exists until the call is answered. How can I read that header once the call is answered? I have tried placing ${SIP_HEADER(X-Twilio-CallSid)} once the call hangs up. Analyzing the traffic through Wireshark that header appears after the INVITE request.
SIP_HEADER function work only for ONE packet - inbound FIRST invite message.
You have write your own function using c/c++ or use some other soft like homer/sipcapture.
I haven't tested this, but according to the docs, you can write a post-answer handler as either a macro (using M()) or a GoSub (using U()):
[outbound-twilio]
exten => _X.,1,Dial(SIP/${EXTEN}#twilio-trunk,,M(post-answer))
[macro-post-answer]
exten => s,1,Verbose("Answer header shows ${SIP_HEADER(X-Twilio-CallSid)}")
same => s,n,Return()
I'm not sure if this will be any different, since the INVITE transaction may only track the initial request, and not the response, even when we execute it from the other channel. You may also want to look into switching to chan_pjsip, which has PJSIP_HEADER:
PJSIP_HEADER allows you to read specific SIP headers from the inbound PJSIP channel as well as write(add, update, remove) headers on the outbound channel. One exception is that you can read headers that you have already added on the outbound channel
Perhaps this is implemented differently than chan_sip's SIP_HEADER function?
Also, more docs on Macros from the book.

How to use Twilio.Device to call a number

How to call to a phone number using Twilio.Device?
I am doing the click to call feature for my widget.
I am able to get the Capability token required in setting up the Twilio.Device. I am able also to connect the Twilio.Device to twilio by setup function provided in client javascript library.
What can I use to call a number using Twilio.Device?
Its been a while since I played with it, but looking back at the code then once you have the token and are connected to Twilio it's just a case of setting the number you want to call and then initiating the call. Something like this:
document.getElementById('button-call').onclick = function () {
// get the phone number to connect the call to
var params = {
Caller: document.getElementById('phone-number').value
};
console.log('Calling ' + params.Caller + '...');
Twilio.Device.connect(params);
};
You have to make sure your token allows outgoing calls. My token.php file contains the following:
$capability = new ClientToken($TWILIO_ACCOUNT_SID, $TWILIO_AUTH_TOKEN, 'ttl=20');
$capability->allowClientOutgoing($TWILIO_TWIML_APP_SID);
$token = $capability->generateToken();
I think this is all fairly standard stuff from the quickstart files. I only had a quick go with it, but I don't remember it being complicated. Have fun!

Is it possible to dynamically set the content of the Twilio phone call

we are developing a mobile (iOS/Android) application through which the user can send an automatic Twilio phone call (using Parse Cloud Code) to a number of her/his choice. Is it possible to dynamically set the content of the phone call from the client (like one can do with SMS’s by passing a string as a variable)? From the examples in the Parse/Twilio documentation it seems that this is not the case and one can only set in advance different texts at different URL’s and choose between them. Is it so?
Twilio developer evangelist here.
You can dynamically set phone call content from a single URL by adding URL parameters to the URL you set for the phone call. So, if, for example, you created a call like so:
client.makeCall({
to: NUMBER,
from: YOUR_TWILIO_NUMBER,
url: 'http://example.com/call/?name=Phil'
}, function(err, responseData) {
// call is made
});
Then you can use the name parameter in the callback to alter the response. Here's an example route in express:
app.post('/call', function(req, res) {
responseText = "Hello " + req.query.name;
res.send("<Response><Say>" + responseText + "</Say></Response>");
});
I hope this helps, let me know if you have any other questions.

twilio javascript client set from number , also how I can get the call sid after connect?

twilio javascript client set from number , Also how I can get the call sid after connect?
I tried to set the from Number in the call options like the next lines before connect and still the same issue in the javascript
$(document).ready(function () {
Twilio.Device.setup(token);
var connection = null;
$("#call").click(function () {
var params = { "Phone": $('#Phone').val(), "from":$('#Phone').val() };
connection = Twilio.Device.connect(params);
return false;
});
});
-- and inside the server side code vbnet when I am generating the token I added the next code but this doesn't solve the from number issue
Dim options As New CallOptions()
options.Record = True
options.From = model.FromNumber
Dim cap = New TwilioCapability(_settings.AccountSID, _settings.AuthToken)
cap.AllowClientOutgoing(_settings.ClientCallApplicationSID, options)
dim token = cap.GenerateToken()
Twilio evangelist here.
The params collection that you pass into the connect function is just a dictionary of key/value pairs. Those key/values simply get passed as parameters to the Voice URL that Twilio requests when Client makes its connection to Twilio, and you can use those parameters to dynamically generate some TwiML markup. Twilio does not do anything with them.
For example, if this is a PHP application, in the Voice URL you could do something like:
<Response>
<Dial>$_REQUEST['From']</Dial>
</Response>
One note of caution, Twilio already adds a parameter called from (which in the case of Client will be the client identifier set when you made your capability token) to the parameters sent to the Voice URL, so you might want to choose a different key name for your dictionary entry. I normally use a name like target for the key that holds the number that I want to dial.
Hope that helps.
To get the call sid, you can get it in connect event.
Please note that I am using Twilio JS v1.9.7
device.on('connect', function (conn) {
log('Successfully established call!');
//Get the CallSid for this call
callUUID = conn.outboundConnectionId;
console.log('CallSid: ' + callUUID);
});

Resources