Thursday, September 5, 2019

Modify Asterisk Queue members via SQL

It is possible to add static agents to a queue via a SQL statement.
Here you see the static members of a queue 
https://imgur.com/a/vzEgDLv
In the table "queues_details" in the database "asterisk" this select will show you all your configured queues.
MariaDB [asterisk]> select * from queues_details;
+------+-----------------------------+---------------------------+-------+
| id   | keyword                     | data                      | flags |
+------+-----------------------------+---------------------------+-------+
| 2001 | member                      | Local/1005@from-queue/n,0 |     3 |
| 2001 | member                      | Local/1006@from-queue/n,0 |     4 |
| 2001 | member                      | Local/1004@from-queue/n,0 |     2 |
| 2001 | member                      | Local/1002@from-queue/n,0 |     1 |
| 2001 | member                      | Local/1001@from-queue/n,0 |     0 |
| 2001 | answered_elsewhere          | 0                         |     0 |
| 2001 | penaltymemberslimit         | 0                         |     0 |
| 2001 | timeoutpriority             | app                       |     0 |
My example shows Queue (ID) 2001 with static members 1001-1006 (1003 is missing)
You can add a member (which will show up in the GUI if you edit it too) using this command. I'm adding extension "1003" into queue 2001.
MariaDB [asterisk]> insert into `queues_details` (`id`,`keyword`,`data`,`flags`) values (2001,'member','Local/1003@from-queue/n,0',100);
Query OK, 1 row affected (0.00 sec)

MariaDB [asterisk]> select * from queues_details;
+------+-----------------------------+---------------------------+-------+
| id   | keyword                     | data                      | flags |
+------+-----------------------------+---------------------------+-------+
| 2001 | member                      | Local/1005@from-queue/n,0 |     3 |
| 2001 | member                      | Local/1006@from-queue/n,0 |     4 |
| 2001 | member                      | Local/1004@from-queue/n,0 |     2 |
| 2001 | member                      | Local/1002@from-queue/n,0 |     1 |
| 2001 | member                      | Local/1001@from-queue/n,0 |     0 |
| 2001 | answered_elsewhere          | 0                         |     0 |
| 2001 | maxlen                      | 0                         |     0 |
| 2001 | member                      | Local/1003@from-queue/n,0 |   100 |
+------+-----------------------------+---------------------------+-------+
42 rows in set (0.00 sec)
What you have to do is set the "FLAG" value to a high value (higher than any current) when you enter it, and you have increment it. My example uses 100.
If you look in the GUI, you'll see it shows up there as well now.

Wednesday, September 4, 2019

Modify FreePBX IVR via commandline

Sometimes you need to modify your IVR by another application.  Maybe you need to remove or add features on the fly, pin numbers, change destinations.  Sometimes its just not convenient to do via the GUI, and you want to do it through a command line or some other method.  Heres two ways.


1 -> Dialplan
2 -> MySQL Database

EXAMPLE 1:

DIALPLAN
Create the ivr in PBX GUI

Open "extensions_additional.conf"
and find your IVR. It will start with a name like
[ivr-X]

Example IVR i created. When you open your file you see very similar code. Copy the entire code in your .conf file and paste it into the bottom of /etc/asterisk/extensions_custom.conf

[ivr-4] ; testivr
include => ivr-4-custom
exten => s,1,Set(TIMEOUT_LOOPCOUNT=0)
exten => s,n,Set(INVALID_LOOPCOUNT=0)
exten => s,n,Set(_IVR_CONTEXT_${CONTEXT}=${IVR_CONTEXT})
exten => s,n,Set(_IVR_CONTEXT=${CONTEXT})
exten => s,n,Set(__IVR_RETVM=)
exten => s,n,GotoIf($["${CDR(disposition)}" = "ANSWERED"]?skip)
exten => s,n,Answer
exten => s,n,Wait(1)
exten => s,n(skip),Set(IVR_MSG=)
exten => s,n(start),Set(TIMEOUT(digit)=3)
exten => s,n,ExecIf($["${IVR_MSG}" != ""]?Background(${IVR_MSG}))
exten => s,n,WaitExten(10,)

exten => 3,1,Set(__IVR_DIGIT_PRESSED=3)
exten => 3,n(ivrsel-3),Goto(app-blackhole,hangup,1)

exten => 2,1,Set(__IVR_DIGIT_PRESSED=2)
exten => 2,n(ivrsel-2),Goto(app-blackhole,hangup,1)

exten => 1,1,Set(__IVR_DIGIT_PRESSED=1)
exten => 1,n(ivrsel-1),Goto(app-blackhole,hangup,1)

exten => h,1,Hangup

exten => hang,1,Playback(vm-goodbye)
exten => hang,n,Hangup

;--== end of [ivr-4] ==--;
copy and paste into /etc/asterisk/extensions.conf

now you can manually edit the file here through code. BUT you can't edit in the GUI. Changes will NOT reflect on the PBX that are done in the GUI.

Example, i've added option # 4 and #5 in the code. (my example IVR had option 1-3)

exten => 4,1,Set(__IVR_DIGIT_PRESSED=3)
exten => 4,n(ivrsel-3),Goto(app-blackhole,hangup,1)

exten => 5,1,Set(__IVR_DIGIT_PRESSED=3)
exten => 5,n(ivrsel-3),Goto(app-blackhole,hangup,1)

exten => 3,1,Set(__IVR_DIGIT_PRESSED=3)
exten => 3,n(ivrsel-3),Goto(app-blackhole,hangup,1)

exten => 2,1,Set(__IVR_DIGIT_PRESSED=2)
exten => 2,n(ivrsel-2),Goto(app-blackhole,hangup,1)
  
exten => 1,1,Set(__IVR_DIGIT_PRESSED=1)
exten => 1,n(ivrsel-1),Goto(app-blackhole,hangup,1)


EXAMPLE 2

SQL commands

You can update/modify using SQL.
If you open up the database table "asterisk" and look at the table called "ivr_entries"

My IVR has 3 entries in the GUI



MariaDB [asterisk]> select * from ivr_entries;
+--------+-----------+------------------------+---------+
| ivr_id | selection | dest                   | ivr_ret |
+--------+-----------+------------------------+---------+
|      5 | 3         | app-blackhole,hangup,1 |       0 |
|      5 | 2         | ivr-3,s,1              |       0 |
|      5 | 1         | from-did-direct,1000,1 |       0 |
+--------+-----------+------------------------+---------+

3 rows in set (0.00 sec)

This IVR you see i created in the GUI. Now you want to add an option? Just update this table, maintain the "IVR_ID" to keep the right IVR and you should be good. This example, i'm going to add option 4, and have it goto extension 2105

MariaDB [asterisk]> insert into ivr_entries (ivr_id,selection,dest,ivr_ret) values (5,4,'from-did-direct,2105,1',0);
Query OK, 1 row affected (0.00 sec)
MariaDB [asterisk]> select * from ivr_entries;
+--------+-----------+------------------------+---------+
| ivr_id | selection | dest                   | ivr_ret |
+--------+-----------+------------------------+---------+
|      5 | 4         | from-did-direct,2104,1 |       0 |
|      5 | 3         | app-blackhole,hangup,1 |       0 |
|      5 | 2         | ivr-3,s,1              |       0 |
|      5 | 1         | from-did-direct,1000,1 |       0 |
+--------+-----------+------------------------+---------+
4 rows in set (0.00 sec)
MariaDB [asterisk]>

Now you've added the entry to the database, if you were to look at this IVR in the GUI, you will see option "4" is now showing.

EXAMPLE 3

Call center uses an IVR to verify customer PIN numbers.  They can manually enter them in via the GUI, but in some cases, during an onboarding processess, they want it to be somewhat automated.
Each customer PIN number is just an IVR entry, and points to a queue if entered properly.
Insted of just using digits 0-9 you can use pretty much any digit length you want.
IVR - "Please enter your pin number"
0 = go to operator
558939 = customer #1 support
443223 = customer #2 support
993828 = customer #83 support
746236 = customer #154 support
After 3 failed tries it hangsup.
FreePBX stores all PBX GUI related information in the MySQL database name "asterisk".
All IVR data is stored in the table "ivr_entries".
In the example below, this shows the IVR entries.
0= play phonebooook
12244 = Customer #12244 send call to queue#2001 (support gold queue)
MariaDB [asterisk]> select * from ivr_entries;
+--------+-----------+-------------------------------+---------+
| ivr_id | selection | dest                          | ivr_ret |
+--------+-----------+-------------------------------+---------+
|      3 | 0         | app-pbdirectory,pbdirectory,1 |       0 |
|      3 | 12244     | ext-queues,2001,1             |       0 |
+--------+-----------+-------------------------------+---------+
2 rows in set (0.00 sec)

If you wanted to automate adding customers, you could do it with a MYSQL command below.
This will add the customer pin 45657 into the IVR id#3 and send it to queue# 2001
Syntax
insert into TABLENAME (Column 1 name,Column 2 name,Column 3 name,Column 4 name) values (IVRID,CustomerPIN,'DESTINATION',"RETURN-IVR");
So adding a customer number of "45657" would look like this:
insert into `ivr_entries` (`ivr_id`,`selection`,`dest`,`ivr_ret`) values (3,45657,'ext-queues,2001,1',0);
EXAMPLE;
 MariaDB [asterisk]> insert into `ivr_entries` (`ivr_id`,`selection`,`dest`,`vr_ret`) values (3,45657,'ext-queues,2001,1',0);
 Query OK, 1 row affected (0.00 sec)

 MariaDB [asterisk]> select * from ivr_entries;                              
 +--------+-----------+-------------------------------+---------+
 | ivr_id | selection | dest                          | ivr_ret |
 +--------+-----------+-------------------------------+---------+
 |      3 | 45657     | ext-queues,2001,1             |       0 |
 |      3 | 0         | app-pbdirectory,pbdirectory,1 |       0 |
 |      3 | 12244     | ext-queues,2001,1             |       0 |
 +--------+-----------+-------------------------------+---------+
 3 rows in set (0.00 sec)
The above example shows entering that data in from a sql statement.
Customer 45657 is now entered into the IVR
This will also update in the PBX GUI if you look at it with your web browser.