We help IT Professionals succeed at work.

Asterisk 1.8.6 setting outbound callerid based on caller

Hi Gurus,

I'm an asterisk programming novice and I need help with finding the correct syntax to match and set an outgoing call from a specific user from a specific SIP Trunk and set as a specific caller id, i.e.:

User A from SIP Trunk A @ exten 202 ==> dialing out ==> set callerid = 3101234567
User B from SIP Trunk B @ exten 202 ==> dialing out ==> set callerid = 3101235555

The problem is that the calls are originating from another PBX, i.e. MS Lync, via a SIP Trunk.

So my thinking is, I need to find out the outgoing SIP info of the caller, if it matches, set the caller id via:
 Set(CALLERID(num)=13101234567)

So I'm not sure if i need to use a marco or gotoif or what...Please help.

Comment
Watch Question

CERTIFIED EXPERT

Commented:
Normally what you would do is separate out the two trunks so that calls go into different contexts in the dialplan.
So in sip.conf where you have the configuration for trunk a you would have a line such as "CONTEXT=from-trunka". Then in the config for trunk b you would have "CONTEXT=from-trunkb".

Then in extensions.conf

[from-trunka]
exten => _X.,1,ExecIf($["${CALLERID(num)}" = "202"]?Set(CALLERID(num)=13101234567))
exten => _X.,n,Goto(common,${EXTEN},1)

[from-trunkb]
exten => _X.,1,ExecIf($["${CALLERID(num)}" = "202"]?Set(CALLERID(num)=3101235555))
exten => _X.,n,Goto(common,${EXTEN},1)

[common]
exten => _X.,1,Dial(DAHDI/g0/${EXTEN})

Author

Commented:
COOL!!! i was hoping it'd be mr. grblades.

thanks for the reply.  

I tried the above sample per below, "224" is my extension that's set on the Microsoft Lync Server, "lync-dial" is the context for the sip trunk "Lync_Trunk"

exten => _+1NXXNXXXXXX,1,ExecIf($["${CALLERID(num)}" = "224"]?Set(CALLERID(num)=17145551234))
exten => _+1NXXNXXXXXX,n,Goto(common,${EXTEN},1)


and i get the below when trying to dial out from CLI output...It seems that it's pulling callerid based on outbound number I'm dialing...Any other suggestions?  is it possible to identify the sip user  making the outbound call and matched based on that?



-- Executing [+17142271234@lync-dial:1] ExecIf("SIP/Lync_Trunk-00000811", "0?Set(CALLERID(num)=17145551234)") in new stack
    -- Executing [+17142271234@lync-dial:2] Goto("SIP/Lync_Trunk-00000811", "common,+17142271234,1") in new stack
    -- Goto (common,+17142272327,1)

Open in new window

Author

Commented:
I should note that asterisk is sending the outbound call to a PRI line, if that matters.
CERTIFIED EXPERT

Commented:
All those +1xxxx number you can see in the output will be the number you dialled. The ExecIf CLI output only shows the result of the comparison and not what it actually compared (the 0 in the 0?Set part meaning the condition did not match).

Try adding an extra line as shown below so you can see the supplied callerid in the CLI output.

exten => _+1NXXNXXXXXX,1,NoOp(Callerid number of incoming call from trunk is ${CALLERID(num)})
exten => _+1NXXNXXXXXX,n,ExecIf($["${CALLERID(num)}" = "224"]?Set(CALLERID(num)=17145551234))
exten => _+1NXXNXXXXXX,n,Goto(common,${EXTEN},1)

Author

Commented:
ok so i think the issue was that my callerid has a "+" in front of it...So i revised as below, but outbound calls aren't passing to context common properly:

here's my code:

exten => _+1NXXNXXXXXX,1,NoOp(Callerid number of incoming call from trunk is ${CALLERID(num)})
exten => _+1NXXNXXXXXX,1,ExecIf($["${CALLERID(num)}" = "+224"]?Set(CALLERID(num)=17145551234))
exten => _+1NXXNXXXXXX,n,Goto(common,${EXTEN},1)

[common]
exten => s,n,Dial(${TRUNK}/${EXTEN:1})
exten => s,n,Congestion

CLI output below:
-- Executing [+17142272327@lync-dial:1] NoOp("SIP/Lync_Trunk-00000839", "Callerid number of incoming call from trunk is +224") in new stack
    -- Executing [+17142272327@lync-dial:2] Goto("SIP/Lync_Trunk-00000839", "common,+17142272327,1") in new stack
    -- Goto (common,+17142272327,1)
[Nov 10 10:56:38] WARNING[21922]: pbx.c:4972 __ast_pbx_run: Channel 'SIP/Lync_Trunk-00000839' sent into invalid extension '+17142272327' in context 'common', but no invalid handler

Open in new window

CERTIFIED EXPERT

Commented:
2nd line in the dialplan is wrong. The 1 should be changed to an 'n' as you have tried making two entries with priority 1 so the second gets lost.

The other problem is that in the common extension you are only matching the 's' extension but you are going to an extension which matches the number to be dialled. So try the following :-

exten => _+1NXXNXXXXXX,1,NoOp(Callerid number of incoming call from trunk is ${CALLERID(num)})
exten => _+1NXXNXXXXXX,n,ExecIf($["${CALLERID(num)}" = "+224"]?Set(CALLERID(num)=17145551234))
exten => _+1NXXNXXXXXX,n,Goto(common,${EXTEN},1)

[common]
exten => _+1NXXNXXXXXX,n,Dial(${TRUNK}/${EXTEN:1})
exten => _+1NXXNXXXXXX,n,Congestion

Author

Commented:
oppp, wasn't verifying the syntax properly...My apologies.

i tried your code and got the following:
-- Executing [+17142272327@lync-dial:1] NoOp("SIP/Lync_Trunk-0000084d", "Callerid number of incoming call from trunk is +224") in new stack
    -- Executing [+17142272327@lync-dial:2] ExecIf("SIP/Lync_Trunk-0000084d", "1?Set(CALLERID(num)=17145551234)") in new stack
    -- Executing [+17142272327@lync-dial:3] Goto("SIP/Lync_Trunk-0000084d", "common,+17142272327,1") in new stack
    -- Goto (common,+17142272327,1)
[Nov 10 11:18:25] WARNING[22014]: pbx.c:4972 __ast_pbx_run: Channel 'SIP/Lync_Trunk-0000084d' sent into invalid extension '+17142272327' in context 'common', but no invalid handler

Open in new window

Author

Commented:
this is how my context "lync-dial"


[lync-dial]


exten => _+1NXXNXXXXXX,1,Set(CALLERID(num)=3102472235)
exten => _+1NXXNXXXXXX,n,Dial(${TRUNK}/${EXTEN:1})
exten => _+1NXXNXXXXXX,n,Congestion


[common]
exten => _+1NXXNXXXXXX,n,Dial(${TRUNK}/${EXTEN:1})
exten => _+1NXXNXXXXXX,n,Congestion

Open in new window

CERTIFIED EXPERT

Commented:
The first entry in a context should specify priority 1 and then the rest can be n (which is really just an abbreviation of writing the last priority+1)

[common]
exten => _+1NXXNXXXXXX,n,Dial(${TRUNK}/${EXTEN:1})

Author

Commented:
god!!! so stupid...I just copied and pasted your syntax without reviewing.

It works like a charm!

What should i use if i want to set non-matched outbound callers to a default callerid?

is the below correct?
exten => _+1NXXNXXXXXX,1,NoOp(Callerid number of incoming call from trunk is ${CALLERID(num)})
exten => _+1NXXNXXXXXX,n,ExecIf($["${CALLERID(num)}" = "+224"]?Set(CALLERID(num)=17145551234))
exten => _+1NXXNXXXXXX,n,Set(CALLERID(num)=17148889999)
exten => _+1NXXNXXXXXX,n,Goto(common,${EXTEN},1)

Open in new window

Author

Commented:
actually, that wouldn't work...It just overriding the syntax from above...Nevermind my stupidity.
CERTIFIED EXPERT

Commented:
You are not far off. You just need to set the callerid before the execif comparison but first need to make a copy of the original callerid value and use that in the comparison.
exten => _+1NXXNXXXXXX,1,NoOp(Callerid number of incoming call from trunk is ${CALLERID(num)})
exten => _+1NXXNXXXXXX,n,Set(ORIGCID=${CALLERID(num)})
exten => _+1NXXNXXXXXX,n,Set(CALLERID(num)=17148889999)
exten => _+1NXXNXXXXXX,n,ExecIf($["${ORIGCID}" = "+224"]?Set(CALLERID(num)=17145551234))
exten => _+1NXXNXXXXXX,n,Goto(common,${EXTEN},1)

Open in new window

Author

Commented:
Can you post a sample?  I assume you just copy the original value of the callerid and then match from a list and if it doesn't match, set to a default callerid?

I understand the logic, but it's more advanced then my novice asterisk coding can handle.


CERTIFIED EXPERT

Commented:
I did it for you in the code snippet in my previous post.

Author

Commented:
Hmmmm, sorry checking from my phone and didn't see the code...will check when I get to my pc shortly.

Author

Commented:
Beatiful!

my last question in regards to the your syntax, if i wanted to set callerid name,i.e.  (CALLERID(Name)), how do i append that code for line 3 and 4?

 i.e. if it doesn't match, name = Company and number = 17148889999, but if it does matcn, then name = JoeSmith and number= 17145551234
CERTIFIED EXPERT
Commented:
As in the code attached. You just set the default name and number and then have a few execif commands to set alternate values.
That method works fine if you have a few exceptions. If you are going to have lots then you can perform a lookup against the callerid in a mysql database and pick out the new callerid name and number to be displayed. Thats a far bit more advanced and not really worthwhile unless your dialplan starts getting very long.
exten => _+1NXXNXXXXXX,1,NoOp(Callerid number of incoming call from trunk is ${CALLERID(num)})
exten => _+1NXXNXXXXXX,n,Set(ORIGCID=${CALLERID(num)})
exten => _+1NXXNXXXXXX,n,Set(CALLERID(num)=17148889999)
exten => _+1NXXNXXXXXX,n,Set(CALLERID(name)=default company name)
exten => _+1NXXNXXXXXX,n,ExecIf($["${ORIGCID}" = "+224"]?Set(CALLERID(num)=17145551234))
exten => _+1NXXNXXXXXX,n,ExecIf($["${ORIGCID}" = "+224"]?Set(CALLERID(name)=JoeSmith))
exten => _+1NXXNXXXXXX,n,Goto(common,${EXTEN},1)

Open in new window

Author

Commented:
Thanks Grblades!  As always, you're the man when it comes to asterisk!

Author

Commented:
Hi Grblades,

i got a pretty simple one for you...If/when you can...Thanks:

http://www.experts-exchange.com/Networking/Telecommunications/IP_Telephony/VoIP/Q_27445573.html