FAQ – Frequent Asterisk Questions – #1

Introduction

I’m a contributor at StackOverflow, answering questions concerning the Asterisk PBX system.  These types of questions, at their core, are part of my bread-and-butter work.  I’ve been working with Asterisk since 2004 and have personally deployed it successfully in 10 different countries under a wide variety of circumstances and use-cases.

However, there are a few things that are just “nice to have” in the tool-kit for a starting AST PBX admin.  So, I’m going to post a few of them on my blog.  It’s paying forward for a lot of the help I’ve gotten over the years.

Keep A Call Alive Past Hangup

A common requirement is to do something after the call ends.  Maybe run an AGI program to update a database for billing, or marketing.

The problem, of course, is that the call dies with the “HangUp()” command, and call processing stops. However, we can “prolong” the life of the call past the hangup condition with a special extension, “h”. Some example code follows:


exten => 5555,Verbose(2, Call for you, sire!)
 same => n, Answer() 
 same => n, Playback(silence/1&important_messages/user{$EXTEN})
 same => n,HangUp()

exten => h,1,Set(x=${CDR(billsec)})
 same => n,AGI(/opt/your_app/does_db.rb ${x})

In the example, the “${x}” will be replaced with whatever the call’s billable duration was, and that will be used as a command line argument to the “does_db” Ruby shell script.

Recommended reading:

Play and Record Simultaneously In Asterisk

Another common question is if you can play a sound file to a caller while recording the call.  I do it all the time, in fact. Your code would look something like:

exten => 5555,1,Answer()
 same => n,Wait(1)
 same => n,Monitor(wav,myfilename)
 same => n,Playback(this-call-may-be-monitored-or-recorded)
 same => n,Playback(pls-wait-connect-call)

For further reading, see:

Authenticating Users in Asterisk

One command that is often overlooked is the “Authenticate” command.  As the name implies, it’s a tool for validating the identity of a caller from within the Asterisk Dialplan Language (ADL).  So you could build a mechanism that assigns a user a number-based UID, and then a PIN. Drop the PIN into a file where the name is the UID, and then when they call in, READ their UID, Authenticate(uid_file_name), and if they enter the correct PIN via Authenticate, let them have access.

Your code might look something like:

exten => 5555, Verbose(2, New call to authenticate ...)
 same => n, Answer()
 same => n, Read(account_id,silence/1&please-enter-account-id,6,s,1,9)
 same => n, Authenticate(/opt/your_app/user_codes/${account_id}.dat,am,4,please-enter-pin-code)
 same => n, Verbose(2, ... we've gotten this far ... user account & PIN validated!)
 same => n, AGI(/opt/your_app/do_something_cool_here.rb)

Recommended reading:

Conclusion

So, that’s three common concepts that I deal with every day.  I’ll put a few more up next week.  If there is anything you’d like to see in particular, let me know in the comments below!