Monday, March 2, 2020

Have asterisk IVR play different message if repeat caller that day



This code, when put into the FreePBX IVR code that is automatically generated has asterisk IVR play a different message if the person has called before base an SQL lookups.

exten => s,n,ExecIf($["${REALCALLERIDNUM}" = ""]?goto(start))        ;don't count people with no number
exten => s,n,ExecIf($[${LEN(${REALCALLERIDNUM})} != 10]?goto(start)) ;if their CLID isn't 10 digits, ignore.
exten => s,n,Set(dblogin=root)    ;database username
exten => s,n,Set(dbpass=)  ;database password
exten => s,n,Set(DateCheck=${STRFTIME(${EPOCH},,%Y-%m-%d)})    ;get current date YYYY-MM-DD
exten => s,n,MYSQL(Connect connid localhost ${dblogin} ${dbpass} asteriskcdrdb) ;connect to asterisk CDR
exten => s,n,MYSQL(Query resultid ${connid} SELECT * from cdr where clid like "%${REALCALLERIDNUM}%" and calldate like "%${DateCheck}%")   ;see if there has been any calls from the same number on today's date (yyyy-mm-dd)
exten => s,n,MYSQL(Fetch fetchid ${resultid} sqlresult)  ;get the sql results (if any)
exten => s,n,MYSQL(Disconnect ${connid})   ;close database connection
exten => s,n,ExecIf($["${sqlresult}" != ""]?Set(IVR_MSG=custom/FRIENDLYMESSAGE))   ;message to play database detected a call previous in database

Installation

Open up /etc/asterisk/extensions_additional.conf

Find your IVR. It will have a heading name like “IVR-2”

You’ll see a bunch of lines that looks like this:

[ivr-2] ; testIVR
include => ivr-13-custom
include => from-ivr-directory-Disabled
exten => fax,1,Goto(${CUT(FAX_DEST,^,1)},${CUT(FAX_DEST,^,2)},${CUT(FAX_DEST,^,3)})
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})
############ SNIP ##################################################
exten => h,1,Hangup
exten => hang,1,Playback(vm-goodbye)
exten => hang,n,Hangup
;–== end of [ivr-2] ==–;

Copy the entire section, then paste it into the bottom of /etc/asterisk/extensions_override_freepbx.conf

Now insert the code I posted up top between these lines as marked below
exten => s,n,Answer
exten => s,n,Wait(1)
exten => s,n(skip),Set(IVR_MSG=custom/YourNormalIVRannoucment)
             ######INSERT CODE HERE!#############
exten => s,n(start),Set(TIMEOUT(digit)=3)
exten => s,n,ExecIf($["${IVR_MSG}" != ""]?Background(${IVR_MSG}))
exten => s,n,WaitExten(10,)

Change “FRIENDLYMESSAGE” that is my code to be the name of the sound file you want to play 
instead if it finds a match. No extensions like .wav or anything.

Save your changes

In the command line type in f
 asterisk -rx 'core reload'
give it a test!

If you notice a problem, you can easily restore the original file by simply changing the heading IVR heading in the _override_freepbx.conf file from “IVR-2” to “IVR-2 disabled”, save your changes then issue the reload command. That will instantly restore your original config.

No comments:

Post a Comment

Feel free to leave a comment! If you have any information that you think should be included, please do so here and I'll get it added in.