Max call attempts within timeframe (a quick solution)

Because of a question on the irc channel I was kind of challenged. I wrote an asterisk script that maximize the number of inbound calls from the same phone within a certain time frame. With some adjustments:

  • adding a dir to the system /var/spool/asterisk/inbound_count
  • routing inbound channel into proper queue or other destination
  • adjusting the number of attemps and the timeframe
  • adjusting GMT-1 to your own situation

the script can be used. It is kind of quick and dirty but it is working and solid and stable as a rock. In the cdr database one can find the relevant log info.

I hope it is usefull for someone .

Erik

[max_inbount_trials]

exten => s,1,Set(FILE_EXISTS=0)
exten => s,n,SET(PAD=/var/spool/asterisk/inbound_count/${CALLERID(num)})
exten => s,n,SET(FILE_EXISTS=${STAT(e,${PAD})}) ; check if file already exists
exten => s,n,GotoIf($[ “${FILE_EXISTS}” : “1”]?check_last_read)
exten => s,n,System(echo -E “xx” > ${PAD}) ; create new file and add 1 line with xx
exten => s,n,MusicOnHold() ; just for now. This is the place to route call into queue
exten => s,n,Hangup()

exten => s,n(check_last_read),Set(LAST_READ=${STAT(A,${PAD})}) ;EPOCH of last read
exten => s,n,NoOp(XXXXXXXXXXXXXXXXX ${LAST_READ})
exten => s,n,SET(SECONDS_AGO=$[${STRFTIME(${EPOCH},GMT-1,%s)} - ${LAST_READ}])

exten => s,n,Set(TIME_WINDOW=3600)
exten => s,n,GotoIf($[ ${SECONDS_AGO}<${TIME_WINDOW}]?add_line)
exten => s,n,NoOp(aantal seconden geleden dat bestand gelezen is ${SECONDS_AGO})
exten => s,n,System(echo -E “xx” > ${PAD}) ; create new file, last attempt was more then 1 hour ago
exten => s,n,MusicOnHold() ; just for now. This is the place to route call into queue
exten => s,n,Hangup()

exten => s,n(add_line),System(echo -E “xx” >> ${PAD}) ; add line to file
exten => s,n,Set(SIZE_FILE=${STAT(s,${PAD})}) ; check for number of bytes in file
exten => s,n,Set(MAX_SIZE=6) ; two attempts with in the timeframe, every attempt add 3 bytes to the file (xx and a line return)
exten => s,n,GotoIf($[ ${SIZE_FILE}>${MAX_SIZE}]?hang_up) ; Just two attempts within time frame
exten => s,n,MusicOnHold() ; just for now. This is the place to route call into queue
exten => s,n,Hangup()
exten => s,n(hang_up),Playback(beep)
exten => s,n,Wait(2)
exten => s,n,Goto(hang_up)