Tuesday, December 23, 2008

How to create a Callback option

Overview
This tutorial will show you how to create a queue, where your incoming calls to arrive. Noting so special, but we are also going to show you how to give a choice to the callers - they will be able to leave the queue at any time and in our example they will have the choice to leave a number at which you could callback them later. The best part is that the number will be nicely emailed to your email box with details not only about the left number, but also the CallerID, the queue at which the caller was initially placed and the date and time at which he has left his number.
Prerequisites
Before we start we will assume that you have a working Asterisk PBX with registered users in iax.confsip.conf or mgcp.conf(It depends on which protocol you would like to use)

We will show you how to create your extensions in extensions.conf.

For the sending of the emails you will need to install the mime-construct package. apt-get install mime-construct (in Debian) should do the trick. Of course you will need a SMTP server too.

Asterisk PBX configurations

We need to create one user in the iax.conf file. This is because we are going to use Idefisk and its IAX2 support. Idefisk supports the SIP protocol too. So if you want to use it, you have to do the configurations below respectively in sip.conf.

Here is the configuration.

[general]
trunkmtu = 4000
bandwidth=low
disallow=lpc10
jitterbuffer=no
forcejitterbuffer=no
tos=lowdelay
autokill=yes

[caller1]
secret=caller1
type=friend
host=dynamic
context=incoming_calls

So, we now we have the user caller1

Type=friend means that this user can make and receive calls. Host=dynamic means that the IP is not statically assigned but dynamically through a DHCP server. Allow=allmeans that the line which this user will use, support all available audio codecs, supported by Asterisk. Context=test - this shows that the user is allowed to work with the extensions in the context with this name in the configuration file extensions.conf.


Our extensions.conf looks like:

The configuration is below:

[incoming_calls]

exten => 100100,1,Set(CALLERID(name)=queue1)
exten => 100100,n,Queue(queue1)
exten => 100100,n,Hangup()

exten => 200200,1,Set(CALLERID(name)=queue2)
exten => 200200,n,Queue(queue2)
exten => 200200,n,Hangup()

exten => 300300,1,Set(CALLERID(name)=queue3)
exten => 300300,n,Queue(queue3)
exten => 300300,n,Hangup()

[queue1out]

exten => 1,1,Set(FLAG=1)
exten => 1,n,Playback(CallBack)
exten => 1,n,Read(NUMBER|beep|10|||5)
exten => 1,n,Wait(1)
exten => 1,n,Set(FLAG=2)
exten => 1,n,GoToIf($[${NUMBER} = ""]?empty:full)
exten => 1,n(empty),System(/usr/bin/call/mailnonumber.sh callback@test.org ${CALLERID(num)} Queue1)
exten => 1,n,Hangup()
exten => 1,n(full),System(/usr/bin/call/mailnumber.sh ${NUMBER} callback@test.org ${CALLERID(num)} Queue1)
exten => 1,n,Hangup()

exten => h,1,NoOp(${FLAG})
exten => h,2,GoToIf($[${FLAG} = 1]?h|3:h|4)
exten => h,3,System(/usr/bin/call/mailnonumber.sh callback@test.org ${CALLERID(num)} Queue1)
exten => h,4,Hangup()

[queue2out]

exten => 1,1,Set(FLAG=1)
exten => 1,n,Playback(CallBack)
exten => 1,n,Read(NUMBER|beep|10|||5)
exten => 1,n,Wait(1)
exten => 1,n,Set(FLAG=2)
exten => 1,n,GoToIf($[${NUMBER} = ""]?empty:full)
exten => 1,n(empty),System(/usr/bin/call/mailnonumber.sh callback@test.org ${CALLERID(num)} Queue2)
exten => 1,n,Hangup()
exten => 1,n(full),System(/usr/bin/call/mailnumber.sh ${NUMBER} callback@test.org ${CALLERID(num)} Queue2)
exten => 1,n,Hangup()

exten => h,1,NoOp(${FLAG})
exten => h,2,GoToIf($[${FLAG} = 1]?h|3:h|4)
exten => h,3,System(/usr/bin/call/mailnonumber.sh callback@test.org ${CALLERID(num)} Queue2)
exten => h,4,Hangup()

[queue3out]

exten => 1,1,Set(FLAG=1)
exten => 1,n,Playback(CallBack)
exten => 1,n,Read(NUMBER|beep|10|||5)
exten => 1,n,Wait(1)
exten => 1,n,Set(FLAG=2)
exten => 1,n,GoToIf($[${NUMBER} = ""]?empty:full)
exten => 1,n(empty),System(/usr/bin/call/mailnonumber.sh callback@test.org ${CALLERID(num)} Queue3)
exten => 1,n,Hangup()
exten => 1,n(full),System(/usr/bin/call/mailnumber.sh ${NUMBER} callback@test.org ${CALLERID(num)} Queue3)
exten => 1,n,Hangup()

exten => h,1,NoOp(${FLAG})
exten => h,2,GoToIf($[${FLAG} = 1]?h|3:h|4)
exten => h,3,System(/usr/bin/call/mailnonumber.sh callback@test.org ${CALLERID(num)} Queue3)
exten => h,4,Hangup()

As we are talking about queues, we are going to create three different queues in thequeues.conf configuration file

The configuration:

[general]

[default]

[queue1]

music = default
strategy = ringall
timeout=15
retry = 5
context = queue1out
periodic-announce-frequency = 60
periodic-announce = Call_Back_1

member => IAX2/user1
member => IAX2/user2
member => IAX2/user3
member => IAX2/user4
member => IAX2/user5
member => IAX2/user6
member => IAX2/user7
member => IAX2/user8
member => IAX2/user9

[queue2]

music = default
strategy = ringall
timeout=15
retry = 5
context = queue2out
periodic-announce-frequency = 60
periodic-announce = Call_Back_1

member => IAX2/user1
member => IAX2/user2
member => IAX2/user3
member => IAX2/user4
member => IAX2/user5
member => IAX2/user6
member => IAX2/user7
member => IAX2/user8
member => IAX2/user9

[queue3]

music = default
strategy = ringall
timeout=15
retry = 5
context = queue3out
periodic-announce-frequency = 60
periodic-announce = Call_Back_1

member => IAX2/user1
member => IAX2/user2
member => IAX2/user3
member => IAX2/user4


Now let’s take a look at the shell script you will need if you want to send emails with the left number. The script will use the mime-construct program to create the email message and send it to the desired recipient. The second script has the same purpose and the only change is in the body of the email message.

Here are the configurations:

Script 1:

#!/bin/sh

NUMBER=$1
RECIPIENT=$2
CALLERID=$3
QUEUE=$4

mime-construct --to $RECIPIENT --subject "You have just missed a call" --string "The number that have been left by the caller: $NUMBER. The CallerID we have received: $CALLERID. The call is coming from the $QUEUE queue. Call was received at `date`"



Script 2: 

#!/bin/sh

RECIPIENT=$1
CALLERID=$2
QUEUE=$3

mime-construct --to $RECIPIENT --subject "You have just missed a call" --string "The caller did not left a telephone number. The CallerID we have received: $CALLERID. The call is coming from the $QUEUE queue. Call was received at `date`"

 

4. Explanation

Now, we are going to explain you what the configurations about actually means and how it works.

However, keep in mind that we are going to explain you only the configurations concerning the Callback possibility. If you want to learn more about the used configuration files and their options - take a look at one of our tutorials:



Let's start with queues.conf.

In the example above you could see three different queues each one with different number of agents.

Each queue has its own configuration. Most of the settings are common, unless one option - the context.

This option is giving you the possibility to define a context, where eventually, by pressing a single digit, the caller will be "transferred". The digit could be pressed at any time while the caller is waiting in the queue. The context we are talking about has to be created in the extensions.conf configuration file. There is one more requirement and it is that you have to create an extension of one digit, in this context - the digit that the caller will press in order to exit the queue.

Despite of the fact, that for all the queues, the setup for leaving a number and then emailing it to our email address will be pretty much the same, we recommend you to create different contexts for every different queue. That is because we are going to email not only the left number but also some other information such as the queue from which the caller has quitted. Of course you could do it with one common and more complicated context for all the queues, but we think that it will be much easier to manage and change the settings for each queue independently of the other ones.

So here are all the options in our queue and what they are doing"

music = default - the music class defined in musiconhold.conf, where Asterisk will be looking for mp3 files, which will be played to the caller instead of ringing tone, while he is waiting in the queue. 
strategy = ringall - one of six ringing strategies that you could choose. This one means the phone of each agent, assigned to the queue, will start ringing in case of incoming call. For details about the other strategies take a look at our tutorial about queues.conf 
timeout=15 - a timeout in seconds. It defines after how many seconds with no answer the agent phone to stop ringing. For more information about refer to our tutorial about queues.conf .
retry = 5 - after how many seconds to try to ring all the agents again. For more information about refer to our tutorial about queues.conf.
context = queue3out - we have already explained this above.
periodic-announce-frequency = 60 - define in seconds an interval of time after which the caller, waiting in the queue, will hear a prerecorded message. It could be a message with instructions or something else. It is up to you. For more information about refer to our tutorial about queues.conf.
periodic-announce = Call_Back_1 - that is the name of the prerecorded message that should be played after the periodic announce timeout expires. For more information about refer to our tutorial about queues.conf.

member => IAX2/user1 - there are two different ways to assign agents to a specific queue. We have picked up the easies one. Whenever a phone with username user1 is registered successfully to the Asterisk system, the incoming calls in the queue will be send to this phone. You could have as many as you want agents assigned to the queue in this way and the incoming call will be send to all of them according to the chosen strategy. For more information about refer to our tutorial about queues.conf.


Now let's take a look at the configurations in extensions.conf.

First of all we have three extensions for the incoming calls. They are all put in the[incoming_calls] context. Their purpose is to put the incoming calls in the corresponding queue.

When we have an incoming call, the first thing we are doing is to change the Callerid name to the name of the queue where the caller will be put. Thus the agent who is answering the call will know from which queue exactly the call is coming. It is not absolutely necessary, but the idea is that one agent could be assigned to many different queues. In order to do the change we are using the Set application - More information about it in our tutorial

The next step is to actually send the call in the queue - pretty easy one. There is an application called Queue to which you just have to pass the name of the desired queue as an argument. We are not going to use any extra options. You could find more information about how to use the QUEUE application in our tutorial

You could notice and the usage of the Hangup application. It is always a good idea to use it as last application for all of your extensions. Thus, you will always be sure that the used channel will be released when the conversation is over. Refer to our tutorial for more information on this application

Next step is to create the contexts, where the callers should be "transferred" in case they have decided to exit the queue. As we have mentioned above we have different context for each of the queue. 

Before we continue - a few words about the idea behind. We are running queues with a callback option. In other words, when we have a caller waiting in the queue, we will play him a message every 60 seconds. The message will saying: "All of our lines are busy. You could leave your number by pressing one and we will call you back as soon as possible or you could stay in the queue and wait to be served.".

If the caller decides to exit the queue, he will be asked to leave his number after the "beep" signal. At this stage, you have to keep in mind that the caller might not leave his number for some reason. So what we will have a check and if the number is not left we will send an email message that the caller has decided not to leave his number.

So what do we need as extensions? The first thing we are doing is to set a flag. Why? In case the caller hangs up at some point before leaving his number, we need to send a message that we did not received the number. For the purpose we are using the so called predefined extension - h, which allows you to execute something in case of hang up. Refer to our tutorial for more information

However, there is a slight chance that the caller might wait for the timeout to expires without leaving his number or to have a problem with the sending of DTMF tone, that we will have an email saying that the caller has left his number but the number field will be empty and because we have a hang up event even if the caller has left his number, we need the flag to determine whether the hang up is before the application that will store the number or after it and on that basis we will know whether we need to send an email message on hang up or not. In this way we omit the sending of one message twice.

On the next step we have the Playback application. Its simple purpose is to play a sound files. We have a sound message, which instructs the caller to type his number after the beep tone and to press the pound key (#) when he is ready. For more information refer to our tutorial about the Playback application

Next, comes the Read application. It’s the one used to store the number typed by the caller. It has many options. 

There is a small limitation in the application we are going to use to save the number typed by the caller. We need to know, when he has finished with the typing. So there are two ways. 

The first one is to ask him to press the pound key (#) once he is ready and the second one is to limit the length of the number the caller could type plus a timeout.
In our example we are going to limit the length of the number to 10 digits and we are going to put a timeout of 5 seconds. We are going to store the number typed by the caller in the NUMBER variable. The Read application gives you the possibility to play a sound file before the typing of the number. In our case we will are going to play abeep tone

Refer to our tutorial for more information and details about the Read application

Next steps - we have the Wait application, which is not absolutely necessary and then we set the flag variable to 2. This, as we have already described above will be used to omit the sending of one and the same email message twice. For more information about the Wait application, refer to our tutorial.

We now have to check whether the caller has actually left his number or not. For the purpose we can use the GoToIf application. It has a very specific syntax, but its idea is simple to verify whether one condition is true or false. So, in our case we are just checking whether the NUMBER variable is empty or not. If it is empty then the execution of the dialplan will continue with the next step, marked as n(empty)nmakes your life easier as you do not have to write and follow the steps (priorities) - nmeans that you take the previous priority and increase it with 1. (empty) is a label showing to you and the system to which n you will be send.

If the NUMBER variable contains even one digit then we assume that we have the number of the caller so the execution of the dialplan will be send to the step with label (full). Refer to our Tutorial for more information, details and option about the GoToIf application.

Both steps (empty) and (full) have one and the same task - to execute the shell script, which will form the email message and send it. The only difference would be in the body of the email message. The application, which we have to use is calledSystem. It just executes system commands as you will do it in your Linux CLI. Refer to our Tutorial for more information about the System application.

In our case we are executing our own scripts called mailnumber andmailnonumber. They are written in such way that we have to pass them a few parameters. You could see them above as screenshots and in pure text

If we do not have the caller's number, we will execute the script calledmailnonumber. We need to add, as first parameter, the email address of the recipient. The second parameter is the CallerId number as we have received it in our system. The last parameter is the name of the queue from which the caller has exited.

If we have the caller's number we will have executed another shell script calledmailnumber which has a different body, but the same parameters plus one more, which of course is the number left by the caller.

The sending of the email is the last step we need to do and that is why after it we have to hang up the channel. For the purpose we will use once again the Hangup application.

In the predefined extensions starting with h we have a simple check of the FLAGvariable to help use determine whether we have to send an email with saying that we do not have the caller's number. (this step is only needed in case the caller hangs up before the prompt to leave his number)

A few words about the shell scripts.

They are not very complicated. All you need is a basic knowledge of shell scripts and the mime-construct packageapt-get install mime-construct (in Debian) and you will have it. Then its just up to the mime-construct's syntax


Now all you have to do is to register your IAX2 base softphone Idefisk to your Asterisk system with the settings, shown above, in iax.conf and dial one of the extensions created in the [incoming_calls] context.

What will happen (if you have followed our setup) is that you will be put in a queue and on every 60 seconds you will hear a message saying that you could exit the queue and leave your number by pressing 1. If you press one you will hear a prompt to leave you number after the signal. Then you will hear beep and if you leave your number followed by the pound key (#), the number will be emailed to the desired email address and you will probably receive a callback.

 


4. Uploaded files