PearlPBX2
← All posts

Incoming calls to an Asterisk queue: the dialplan

An incoming call to a call center rarely goes straight into the queue. First you filter out unwanted callers, raise the priority of important ones, start recording and pick the hold music. Only then does the call go to the agents. Below is a real scenario that does all of this in one dialplan.

The whole scenario

This is AEL code for PearlPBX2. You write it in the dialplan editor in the admin panel, and the validator checks it in the form, before the config ever reaches Asterisk.

NoOp(CALL BEGIN >>>> :'${CALLERID(name)}'@<${CALLERID(num)}>);
Set(CHANNEL(language)=ua);
Set(TIMEOUT(absolute)=3600);
if (${LEN(${CALLERID(name)})} > 7) {
    Set(CALLERID(num)=${CALLERID(name)});
}
&callerid_normalization();
AGI(agi://127.0.0.1:4573/blacklist,${CALLERID(num)});
if ( ${BLACKLISTED} > 0) {
    Hangup(17);
}
AGI(agi://127.0.0.1:4573/whitelist,${CALLERID(num)});
if ( ${WHITELISTED} > 0) {
    Set(QUEUE_PRIO=10);
}
AGI(agi://127.0.0.1:4573/parking-uline);
AGI(agi://127.0.0.1:4573/mixmonitor,${CALLERID(num)},${EXTEN});
&lc_partday();
Set(CHANNEL(musicclass)=Support_${PARTDAY});
Answer();
Wait(1);
Queue(Support,tT,,,,integration_agi.py);
NoOp("QUEUESTATUS=${QUEUESTATUS}");
Hangup();

The easiest way to read it is as a series of gates: each block either lets the call through, changes its parameters, or ends it.

1. Setup: language and a hard time limit

Calls prefixed with an ampersand, such as &callerid_normalization() and &lc_partday(), are AEL macros. You create them under Dialplan macros in the admin panel and call them by name from the scenario.

NoOp does nothing except print a line to the console and the log, so the log shows where each call starts and who is calling.

Set(CHANNEL(language)=ua) picks the sound prompt directory Asterisk uses for system phrases. Set(TIMEOUT(absolute)=3600) sets a hard limit: after an hour Asterisk drops the call, even if everyone forgot about it. For a support queue this is insurance against stuck channels.

2. Normalizing the caller’s number

if (${LEN(${CALLERID(name)})} > 7) {
    Set(CALLERID(num)=${CALLERID(name)});
}
&callerid_normalization();

The first block uses the caller ID name as the number when the name is longer than seven characters. This is for trunks that send the real number in the name field instead of the number field.

The second line calls the callerid_normalization macro. PearlPBX2 ships it in the default set, and you can edit its code in the admin panel. By default it converts numbers to the ten-digit Ukrainian format: it strips the plus sign and the 380 country code, prepends 044 to a seven-digit Kyiv landline number and 0 to a nine-digit one. Anonymous calls (Anonymous) are left alone.

Order matters here. The lists and the recording below work with ${CALLERID(num)}, so the number has to be normalized first. Otherwise +380501234567 and 0501234567 would look like two different callers to the blocklist.

3. Blocklist and allowlist

AGI(agi://127.0.0.1:4573/blacklist,${CALLERID(num)});
if ( ${BLACKLISTED} > 0) {
    Hangup(17);
}

agi://127.0.0.1:4573 is the PearlPBX2 FastAGI server. The blacklist handler looks the number up in the database and sets BLACKLISTED to 1 or 0. An entry counts if it hasn’t expired or has no expiry at all. If the number is on the list, the call ends with cause 17 (User busy), so the caller hears a busy signal instead of long silence.

One detail: the scenario passes only the caller’s number to the handler, not the dialed number. So only blocklist entries that aren’t tied to a specific destination apply here, meaning global blocks.

AGI(agi://127.0.0.1:4573/whitelist,${CALLERID(num)});
if ( ${WHITELISTED} > 0) {
    Set(QUEUE_PRIO=10);
}

The allowlist check works the same way, with a different result: WHITELISTED raises the priority. The Queue application reads QUEUE_PRIO, and a caller with a higher value gets ahead of everyone waiting with a lower one. Numbers on the allowlist, such as key accounts, are served first even if they called later.

4. A parking slot and call recording

AGI(agi://127.0.0.1:4573/parking-uline);
AGI(agi://127.0.0.1:4573/mixmonitor,${CALLERID(num)},${EXTEN});

parking-uline reserves a free slot (ULINE) from a range that is 1 to 199 by default. Allocation is atomic, done by a Lua script in Redis, so two simultaneous calls never get the same slot. The slot number goes into the ULINE variable (0 means no free slots). You can see it on the /dashboard/ulines/ screen and in the channel_vars of webhooks.

mixmonitor decides whether to record. It gets the caller’s number and the dialed number, checks the recording rules in the admin panel (force on or force off by caller or destination), and if recording is needed, starts MixMonitor into a .wav file in a per-date directory. The result is in the MIXMONITOR variable. Recording starts before the call enters the queue.

5. Hold music by time of day

&lc_partday();
Set(CHANNEL(musicclass)=Support_${PARTDAY});

The lc_partday macro sets PARTDAY to the part of the day: morning, day, evening or night. The music class name is a prefix plus that variable, so the scenario picks one of four classes: Support_morning, Support_day, Support_evening or Support_night. Callers hear a different greeting and music in the morning, afternoon, evening and at night. Each PARTDAY value needs a class with exactly that name, or Asterisk won’t find it.

Why set the class with CHANNEL(musicclass) and not in the queue settings? The musiconhold.conf that PearlPBX2 generates has preferchannelclass=yes. With it, a class set on the channel wins over the queue’s class. One queue can serve any number of classes, and the dialplan makes the choice.

6. A greeting that plays while agents are already ringing

There is no separate Playback before the queue. The greeting is the first file in the hold music class. As soon as the call enters Queue, Asterisk starts ringing agents, and the caller hears hold music that opens with the greeting. If an agent is free, the conversation starts right away and the greeting holds nobody up.

To make the greeting play first, create the class in Files mode with Alpha sorting and number the files:

01_greeting.wav
02_music_1.wav
03_music_2.wav

Do the same for each of the four classes. We covered this technique in more detail, along with queue position announcements and callbacks, in Keep callers on the line: useful hold in Asterisk.

7. Answer and enter the queue

Answer();
Wait(1);
Queue(Support,tT,,,,integration_agi.py);
NoOp("QUEUESTATUS=${QUEUESTATUS}");
Hangup();

The call is answered (Answer) only now, after every check. A blocklisted caller is rejected before the call is even answered. The scenario then waits one second (Wait(1)) and puts the caller into the queue.

In Queue(Support,tT,,,,integration_agi.py) the first argument is the queue name. Option t lets the agent transfer the call, and T lets the caller do it. The sixth argument, integration_agi.py, is an AGI script that Asterisk runs on the caller’s channel at the moment an agent picks up. In this scenario the script sends a webhook tied to that agent, so the external system learns not only that the conversation started but also who is handling it.

If the call never connects, Queue returns control to the dialplan with the reason in ${QUEUESTATUS} (for example, TIMEOUT or FULL). The scenario logs it and hangs up.

What you can add

The same FastAGI server has two more handlers that extend this scenario well. queue-status sets the READYTORECEIVE and QUEUECALLERS variables, and add-callback inserts a callback request. Together they let you offer the caller a callback when every agent is busy, instead of keeping them in the queue.

Summary

The order of the blocks is deliberate. The number is normalized before the lists are checked, a blocklisted caller is cut off before Answer, and recording and music are set up before the call enters the queue. Each step gets a call that is already prepared, and the greeting in the music class gives the caller useful information instead of empty ringback.