How to route an incoming call when I have multiple phone numbers with the same SIP provider? - communication

I have a main phone number 0120120020 given by my SIP provider for my office. I've recently asked my SIP provider for a second phone number 0230230030 on the same SIP account.
I want Asterisk to ring every phones belonging to Group A when someone calls the office by dialing the main phone number, and I want it to ring every phones belonging to Group B if the second phone number is dialed instead.
My extensions.conf contains the following lines :
[sip-incoming-calls]
exten => s,1,Dial(SIP/10&SIP/11&SIP/12&SIP/20&SIP/21,20,tr)
Extensions 10, 11 and 12 belongs to the Group A, extensions 20 and 21 belongs to the Group B. Every phones are ringing for each incoming calls coming from the main and the second phone number.
How can I do this with Asterisk 1.8 under CentOS 6.3 ?

For each call, Asterisk sets some variables containing informations about the current incoming call. These informations are useful to act differently if the call is coming from specific country, you can also block bad callers from their phone numbers,...
We are looking for a way to retrieve the DID, which actually means Direct Inward Dialing Number, that's the number dialed by the caller to call your office.
There are two ways to retrieve it :
By using the CALLERID(dnid) variable directly, it's value will be set to 0120120020 or 0230230030
[sip-incoming-calls]
exten => s,1,Set(thedid=${CALLERID(dnid)})
If the above variable is empty, then your SIP provider doesn't send any informations as needed to make Asterisk filling it correctly.
You can however retrieve the phone number directly from the To field inside the SIP header with SIP_HEADER(To), this variable will contain <sip:0230230030#sip.provider.com> when someone calls your office from your second phone number.
[sip-incoming-calls]
exten => s,1,Set(thedid=${SIP_HEADER(To)})
exten => s,2,Set(thedid=${CUT(thedid,#,1)})
exten => s,3,Set(thedid=${CUT(thedid,:,2)})
Once you have retrieved the DID inside the variable, you have to set a condition, let's use GotoIf. It will result like this in your case :
exten => s,4,GotoIf($["${thedid}" = "0120120020"]?6:5)
exten => s,5,GotoIf($["${thedid}" = "0230230030"]?7)
exten => s,6,Dial(SIP/10&SIP/11&SIP/12,20,tr)
exten => s,7,Dial(SIP/20&SIP/21,20,tr)

[sip-incoming-calls]
exten => s,1,goto(${CUT(CUT(SIP_HEADER(TO),#,1),:,2)},1)
exten => 0120120020,1,Dial(SIP/10&SIP/11&SIP/12,20,tr)
exten => 0230230030,1,Dial(SIP/20&SIP/21,20,tr)

easily you can create trunk (seperate dahdi port) and redirect trunk to specific extensions.

ask your voip provider to send the DID number for the incomming calls
then use the below dialplan
[sip-incoming-calls]
exten => _0120120020,1,Dial(SIP/10&SIP/11&SIP/12,tr)
exten => _0120120020,n,hangup
exten => _0230230030,1,Dial(SIP/20&SIP/21,20,tr)
exten => _0230230030,n,hangup

Related

The source phone number provided is not yet verified for your account

Getting an error that phone number is not verified when I add a callerId.
When placing a call it returns:
Fatal error: Uncaught Twilio\Exceptions\RestException: [HTTP 400]
Unable to create record: The source phone number provided, XXXX, is
not yet verified for your account.
The strange part is that it only gives this error when callerId value is set.
So for example
$call = $client->calls
->create(
"+x", // to
"+x", // from
[
"url" => "http://demo.twilio.com/docs/voice.xml",
]
);
Does work
but the following produces that error.
$call = $client->calls
->create(
"+x", // to
"+x", // from
[
"url" => "http://demo.twilio.com/docs/voice.xml",
"callerId" => 'SomeValue',
]
);
The only difference is that callerId gets set.
From number was purchased, account is not trial and SID is valid since it works without callerId field
Error is returned because it looks like Twilio is trying to use the value of callerId as the phone number. So it looks like you can't set a caller ID in the form of a word, it has to be a numeric value
I'm sorry the docs are not clear about this, but CallerId works the
same a From. It must be Twilio phone number or verified non-Twilio
phone number in your account. It cannot be a word or name. Twilio
phone numbers do not support CNAM (caller name) database entries, so
it is not possible to specify a name that the call recipient sees, if
their carrier does CNAM lookups.
When you specify From, you do not need to pass CallerId.

Twilio callStatusCallback for conferences not working

I'm trying to setup a flow where i'm using the twilio client sdk to start a conference.
Frontend creates a device and connects, which in turn calls the route I have setup in the twiml application. Then the following is returned:
$response = new VoiceResponse();
$dial = $response->dial('');
$dial->conference($x->getRoomName(), [
'participantLabel' => 'A participant label',
'startConferenceOnEnter' => 'True',
'endConferenceOnExit' => 'True',
'statusCallbackEvent' => 'start end join leave mute hold',
'statusCallback' => "https://example.com/conference-call/status-callback",
'statusCallbackMethod' => 'POST',
'callStatusCallbackEvent' => 'ringing completed initiated answered',
'callStatusCallback' => "https://example.com/conference-call/call-status-callback",
'callStatusCallbackMethod' => 'POST',
]);
I can get status callbacks for the conference, but not for the call that is actually initiating the conference. As such I am unable to track the call status of the one who is initiating the conference.
Inviting a participant via the API works fine, the callStatusCallback works when creating a participant via the rest API.
Twilio must be creating a participant in the background when we use the Conference term, but there doesn't seem to be a way to subscribe to the call status?
If there is no solution for this, is there a way to use the client sdk to inititate the call, then take care of the business logic only via the rest API? It seems like the twiml is not as powerful as the rest API, but i couldn't find a way to make calls from the browser without a twiml app.

Passing custom CNAM through Twilio SIP Domain

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.

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.

Difference between CallTo/To and CallFrom/From in Twilio

When I make an outgoing call using Twilio API & TwiML App Twilio makes request to my server with following params:
'AccountSid' => '...',
'ApplicationSid' => '...',
'Caller' => 'client:2',
'CallStatus' => 'ringing',
'callFrom' => 'some_phone_number',
'Called' => '',
'To' => '',
'callTo' => 'some_phone_numeber',
'CallSid' => '...',
'From' => 'client:2',
'Direction' => 'inbound',
'ApiVersion' => '2010-04-01',
what's the difference between CallTo (which equals the actual phone number I'm calling to) and To (which is empty for some reason) ? Same for From and CallFrom. I couldn't find any info in the docs about callTo/callFrom. Plus, why To is empty?
Update
Some additional info. I make a call from twilio number to real Russian phone number. I actually hangup the call. There is no forwarding/redirecting. The Twiml app for outgoing calls is:
<Response>
<Dial callerId="{{ $callFrom }}"
record="record-from-ringing"
action="{{ $action }}">
<Number>{{ $callTo }}</Number>
</Dial>
</Response>
CallFrom and CallTo equals those which are passed with the request. So the data I mentioned above is what Twilio passes with the request, and then Twilio gets the TwiML instructions.
What I do not understand if that what's the difference between to/from? Are from/to fields used when dealing with client names, like for internal conversation or kind of...?
And another question is: when the call is finished and the action url called, twilio passes DialCallStatus and CallStatus, what's the difference there? I need to store call status so that I know if the client was busy and didn't answer, but DialCallStatus has different value from CallStatus.
Thanks

Resources