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.

3 comments:

  1. Thank you so much for writing this up, there is little to no documentation on how to do this anywhere and it took me three days to find this but I'm so glad I did and I'm so glad you put this together. tytytyty!!!

    ReplyDelete
    Replies
    1. You are welcome! Glad you found it helpful, hopefully it serves you well! ;)

      Delete
  2. Thank you. It help me to sort out things.

    ReplyDelete