30 July 2005

Calling Features With Asterisk


In the past couple of days I’ve added a few useful calling features to the Asterisk installation we use for handling the calls to our household. These calling features would cost another $10-15 per month from the local RBOC if we wanted them normally. We’d just as soon not get nickeled and dimed for these though since they can be done for free.

There are snippets below to help the aspiring Asterisk user add these call features to their own system.

Caller-ID based Blacklisting

This feature takes incoming calls from specific folks that call a little too much and sends them somewhere that won’t make our phone ring. If the friendly, well-meaning vampires at the local blood bank are calling you twice every day, this is for you.

macro-stdexten

[incoming]
exten => s,1,Macro(stdexten,1234,SIP/MyInternalLine)


Now, in the macro-stdexten we tell it to check the blacklist. Key lines for this function are s,1 and s,102. The other lines are relevant to things I’ll cover later.


[macro-stdexten]
exten=s,1,LookupBlacklist ; If CID blacklisted, goto 102
exten=s,2,DBget(temp=CFIM/${ARG1}) ; Get CFIM (Call Forward IMmediate) key, if there isn't one, goto 103
exten=s,3,Dial(Local/${temp}@local/n) ; Unconditional forward
exten=s,4,DBput(lastcaller/${ARG1}=${CALLERIDNUM}) ; Note the last caller
exten=s,5,Dial(${ARG2},20) ; Call the extension with a 20 sec timeout
exten=s,6,DBget(temp=CFBS/${ARG1}) ; Get CFBS (Call Forward BuSy) key, if there isn't one, goto 107
exten=s,7,Dial(Local/${temp}@local/n) ; Forward on busy or unavailable
; Blacklisted CallerID
exten=s,102,Goto(blacklisted,s,1)
; No CFIM key
exten=s,103,Goto(s,4)
; No CFBS key = goto voicemail
exten=s,107,Answer
exten=s,108,Wait(1)
exten=s,109,Voicemail(u${ARG1})


So now people that have their phone numbers in the blacklist will get sent to blacklist s,1. We’d best define that…


[blacklisted]
exten=s,1,Answer
exten=s,2,Wait(1)
exten=s,3,Playback(nbdy-avail-to-take-call)
exten=s,4,Playback(carried-away-by-monkeys)
exten=s,5,Playback(lots-o-monkeys)
exten=s,6,Hangup


Now, I think the monkeys make the whole calling experience a lot more fun for the caller. If you want them to hear something more dignified you could always go for Playback(not-taking-your-call).

You can add and remove numbers from the blacklist by using the Asterisk console and database put blacklist 2025551212 1 and database del blacklist 2025551212. But it would probably be better if you could configure this from your phone so…


[blacklist-add]
exten=s,1,Playback(enter-num-blacklist)
exten=s,2,DigitTimeout(5)
exten=s,3,ResponseTimeout(30)
exten=_X.,1,DBput(blacklist/${EXTEN}=1)
exten=_X.,2,SayDigits(${EXTEN})
exten=_X.,3,Playback(num-was-successfully)
exten=_X.,4,Playback(added)
exten=_X.,5,Hangup

[blacklist-remove]
exten=s,1,Playback(enter-num-rmv-blklist)
exten=s,2,DigitTimeout(5)
exten=s,3,ResponseTimeout(30)
exten=_X.,1,DBput(blacklist/${EXTEN}=1)
exten=_X.,2,SayDigits(${EXTEN})
exten=_X.,3,Playback(num-was-successfully)
exten=_X.,4,Playback(removed)
exten=_X.,5,Hangup


If you are following the calling feature codes that are used in the United States, users should be able to do blacklist-add with *60 and blacklist-remove with *80.

Call Forwarding

Most of the logic for call forwarding is in the macro-stdexten above. If the call forwarding is configured, we send the call to that number. If you just have one PSTN line like we do, you’ll have to be able to route those forward calls over a VoIP provider, since the PSTN line may already be used up by the incoming call.

The main thing is simply to let people configure the call forwarding using a prompt system similar to the one we just used for the blacklist configuration. There are again standard calling feature codes for unconditional call forwarding in the United States. Activating call forwarding is *72 and deactivating is *73.


[callfwd-add]
exten=s,1,Playback(please-enter-the)
exten=s,2,Playback(call-forwarding)
exten=s,3,Playback(telephone-number)
exten=s,4,DigitTimeout(5)
exten=s,5,ResponseTimeout(30)
exten=_X.,1,DBput(CFIM/${CALLERID}=${EXTEN})
exten=_X.,2,Playback(call-forwarding)
exten=_X.,3,Playback(has-been-set-to)
exten=_X.,4,SayDigits(${EXTEN})
exten=_X.,5,Hangup

[callfwd-remove]
exten => s,1,Answer
exten => s,2,Wait(1)
exten => s,3,DBdel(CFIM/${CALLERIDNUM})
exten => s,4,Playback(call-fwd-cancelled)
exten => s,5,Hangup


Last Call Return

So again, part of what lets this work is in macro-stdexten s,4 above where the last call to each extension is recorded into the database for posterity. Then we can offer last call return when people dial *69 with the following snippet:


[app-lastcallreturn]
exten => s,1,Answer
exten => s,2,Wait(1)
exten => s,3,Playback(last-num-to-call)
exten => s,4,DBget(number=lastcaller/${CALLERIDNUM}) ; goto 105 if no lastcaller
exten => s,5,GotoIf($"${number}" = ""?105,1) ; also if it's blank (caller id blocked)
exten => s,6,SayDigits(${number})
exten => s,7,Wait,1
exten => s,8,Playback(to-call-num-press)
exten => s,9,Playback(digits/1)
exten => 1,1,Goto(longdistance,1${number},1)
exten => s,105,Playback(unidentified-no-callback)
exten => s,106,Hangup


Summary

Most of this was adapted from examples at www.voip-info.org where there is a nice wiki with Asterisk information. I made an effort to clean up some of the examples they had and try to use the more standard vertical service codes. Hopefully this is useful to folks.


3 comments:

Anonymous said...

This was an excellent example and appreciated. Now that 1.4 had changed a lot, do you have updated examples of doing the same thing with 1.4? Thanks.

Unknown said...

Can I used the blacklist features so that in our system we can have a list of numbers that our sales people can call and then the rest is deny unless it is on the blacklist

Unknown said...

Its really very useful staff. Anybody can refer it to build a black list and block a call.