IVR Logic for if an option is chosen twice in the same day (etc)

Can FreePBX or Asterisk cope with logic within IVRs so that if an option is chosen twice in the same day, the message (and destination) played to the caller is different?

Use case:
Supplier calls us to ask for an order, if they call twice in the same day, the message played back is a bit more apologetic as they’ve had to call twice!

Thanks

The IVR doesn’t have that logic at the moment, no. But the end result you want is possible. Did something similar for a group awhile back using some custom dialplan with SQL CDR table lookup.

They had customer calling back within an hour of their last call it immediately dropped them into a queue as high priority. Theirs was based on CLID though.

hiya dickson - this sounds ideal as CLID is how I would need to do it too - would you mind sharing? Totally understand if you’d rather not! :slight_smile:

Although I haven;t checked for typos (Or at all running in asterisk) something like

same => n,If($[${ISNULL(${DB(client/${CALLERID(num)})})}]?Set(DB(client/${CALLE
same => n,Set(since=$[ ${EPOCH} - ${DB(client/${CALLERID(num)}} ])
same => n,Set(DB(client/${CALLERID(num)})=${EPOCH}))
same => n,GotoIf($[ ${since} < 86400 ]?from-queue,700,1:from-group,800,1)

Could be a good starting point

I’m still reviewing the code snippet @dicko wrote, it looks elegant, mine is like its been written by 3 year old, but it works. (at least it does in the situation it came from)

I’ll add this approach is pretty simplistic. It just does a SQL lookup to see if the CLID number entering the IVR has called previously today. If it has, then the code just changes the sound file the caller hears to a different one that you have recorded. It looks to see that there is no blank callerID and make sure it is 10 digits in length (you might need to alter that depending on your needs). Simplistically trying to avoid people with no number getting a special treatment call.
Give it a try, and message if you have questions.

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(Clear ${resultid})
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.

2 Likes

Still a WIP but perhaps a little more understandable, please pick apart

exten =>  s,1,noop(WIP to pacify  ${CALLERID(num)}  when stressed)                                                                                                                                                                          
same => n,GotoIf($[${ISNULL(${DB(client/${CALLERID(num)})})}]?:oldclient)                                                                                                                                                                   
same => n,Set(DB(client/${CALLERID(num)})=${EPOCH})    ;seed a new caller                                                                                                                                                                                     
same => n,Goto(ext-group,700,1)       ;destination for a new caller                                                                                                                                       
same => n(oldclient),Gotoif($[ (${EPOCH} - ${DB(client/${CALLERID(num)})}) < 3600 ]?onehour)                                                                                                                                                 
same => n,Gotoif($[ (${EPOCH} - ${DB(client/${CALLERID(num)})}) < 7200 ]?twohours)                                                                                                                                                           
same => n(onehour),Goto(ext-group,800,1)        
;same => n,Set(DB(client/${CALLERID(num)})=${EPOCH})    ;re-seed the caller, probably better done in a hangup handler when she is satisfied                                                                                                                                                                                     

.
.
.

You need to reset to ${EPOCH} when client is satisfied

Thanks so much for this, at the moment the dialplan seems to always hop straight to ‘start’…I’ve changed the caller ID to 11 to match my country but still…
Call trace available at the end of the code snippet.
Thanks for this by the way…!

[ivr-1] Supplier call
include => ivr-1-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})
exten => s,n,Set(__IVR_RETVM=)
exten => s,n,GotoIf($["${CHANNEL(state)}" = "Up"]?skip)
exten => s,n,Answer
exten => s,n(skip),Set(IVR_MSG=custom/hello-which-day)

exten => s,n,Noop(Entering queer IVR)
exten => s,n,ExecIf($["${REALCALLERIDNUM}" = ""]?goto(start))        ;don't count people with no number
exten => s,n,ExecIf($[${LEN(${REALCALLERIDNUM})} != 11]?goto(start)) ;if their CLID isn't 11 digits, ignore.
exten => s,n,Set(dblogin=root)    ;database username
exten => s,n,Noop(db login)
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 "%${DateC$
exten => s,n,MYSQL(Fetch fetchid ${resultid} sqlresult)  ;get the sql results (if any)
exten => s,n,Noop(sql results)
exten => s,n,MYSQL(Disconnect ${connid})   ;close database connection
exten => s,n,ExecIf($["${sqlresult}" != ""]?Set(IVR_MSG=custom/heateroff))   ;message to play database detected a call previous $
exten => s,n,Noop(play msg detected)

exten => s,n(start),Set(TIMEOUT(digit)=3)
exten => s,n,ExecIf($["${IVR_MSG}" != ""]?Background(${IVR_MSG}))
exten => s,n,WaitExten(10,)

[snip]

Call trace available here

I think i see the problem, i’ll post an update for your situation shortly.

1 Like

thank you! :slight_smile:

I tried changing to this but it works no better. callerid(name) instead of REALCALLERIDNUM

exten => s,n,Noop(Entering queer IVR)
exten => s,n,ExecIf($["${CALLERID(name)}" = ""]?goto(start))        ;don't count people with no number
exten => s,n,ExecIf($[${LEN(${CALLERID(name)})} != 11]?goto(start)) ;if their CLID isn't 11 digits, ignore.
exten => s,n,Set(dblogin=root)    ;database username
exten => s,n,Noop(db login)
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 "%${CALLERID(name)}%" and calldate like "%${DateCheck}%")   ;see if th$
exten => s,n,MYSQL(Fetch fetchid ${resultid} sqlresult)  ;get the sql results (if any)
exten => s,n,Noop(sql results)
exten => s,n,MYSQL(Disconnect ${connid})   ;close database connection
exten => s,n,ExecIf($["${sqlresult}" != ""]?Set(IVR_MSG=custom/heateroff))   ;message to play database detected a call previous in database
exten => s,n,Noop(play msg detected)

Code fix. For your implementation change REALCALLERIDNUM to FROMEXTEN
That should fix things up!

Also, I edited my original post and put in the additional line to cleanly exit the SQL query

exten => s,n,MYSQL(Clear ${resultid})
1 Like

Thank you! :smiley: :smiley:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.