[FreePBX/Asterisk]Multiple / Rotary Call Flow Toggles

We got a customer the other day who wanted to manually override their normal call flow. This could have been easily achieved by a single toggle if it was to a single destination. But this customer wanted wanted to be able select multiple destination and also only wanted to push one button to do so.

This meant we had to write custom code to get this done and it was achieved by writing a code to manipulate call flow toggles.

What you need to add is the following to the extensions_custom.conf

[from-internal-custom]
include => enable-switch

[check-active-switch]
exten => s,1,NoOp("This context loops through all the switches and checks which one is ON. Than it follows its destination")
exten => s,n,GoSub(sub-get-nr-of-switches,s,1)
exten => s,n,Set(nrofs=${GOSUB_RETVAL})
exten => s,n,Set(i=0)
exten => s,n,Set(default-dest=app-daynight,4,1)
exten => s,n,Set(switchnr="NULL")
exten => s,n,While($[$[${i} < ${nrofs}] & $[${switchnr} = "NULL"]])
exten => s,n,Set(switchnr=${IF($["${DB(DAYNIGHT/C${i})}" = "NIGHT"]?${i}:"NULL")})
exten => s,n,Set(i=$[${i} + 1])
exten => s,n,EndWhile
exten => s,n,GotoIf($[${switchnr}!="NULL"]?app-daynight,${switchnr},1:${default-dest})
exten => s,n,Hangup()

[sub-get-nr-of-switches]
exten => s,1,Set(nr_of_s=0)
exten => s,n,Set(exten_state="NOT_ACQUIRED")
exten => s,n,While($[${exten_state}!=0])
exten => s,n,Set(exten_state=$[${VALID_EXTEN(app-daynight,${nr_of_s},1)}])
exten => s,n,NoOp(Exten nr ${nr_of_s} of app-daynight is ${exten_state})
exten => s,n,Set(nr_of_s=${IF($[${exten_state}=1]?$[${nr_of_s}+1]:${nr_of_s})})
exten => s,n,EndWhile
exten => s,n,NoOp(${nr_of_s} switches found)
exten => s,n,Return(${nr_of_s})

[enable-switch]
exten => _*20X,1,NoOp("Enable selected Switch and disable others")
same => n,GoSub(sub-get-nr-of-switches,s,1)
same => n,Set(nrofs=${GOSUB_RETVAL})
same => n,Set(switchtoactivate=${EXTEN:3})
same => n,Set(CSTATE=${DB(DAYNIGHT/C${switchtoactivate})})
same => n,NoOp(Activated Switch nr ${switchtoactivate})
same => n,Set(i=0)
same => n,While($[${i}<${nrofs}])
same => n,GotoIf($["${CSTATE}" = "NIGHT"]?end1)
same => n,Set(DB(DAYNIGHT/C${i})=DAY)
same => n(end1),NoOp(Dialing the same toggle code. Switch it off)
same => n,Set(DEVICE_STATE(Custom:AHTGL${i})=NOT_INUSE)
same => n,Set(DEVICE_STATE(Custom:DAYNIGHT${i})=NOT_INUSE)
same => n,Set(i=$[${i}+1])
same => n,EndWhile
same => n,Set(freepbx_toggle_dest=*28${switchtoactivate})
same => n,GotoIf($["${CSTATE}" = "NIGHT"]?end2)
same => n,Set(DEVICE_STATE(Custom:AHTGL${switchtoactivate})=INUSE)
same => n(end2),NoOp(Skip setting BLF)
same => n,Goto(app-daynight-toggle,${freepbx_toggle_dest},1)
same => n,Hangup
same => hint,Custom:AHTGL${EXTEN:3}

Then from FreePBX add custom destination with check-active-switch,s,1 as the value and point the inbound route to this custom destination.

Now, you create all the call flow toggles with their override mode set to the destinations you want to go and the normal mode is set identical on each toggle the normal operation you want to work.

Finally, create BLF keys to see which switch is active using *20X as the value to dial/monitor.

Modify the following line to point calls to a default destination if none of the toggles are on overrideĀ  mode. On the below code it is set to app-daynight,4,1

exten => s,n,Set(default-dest=app-daynight,4,1)

The original code is via : https://community.freepbx.org/t/multiple-daynight-toggles-aka-call-flow-control/27518

Modifications on this code helps you use BLF to monitor currently selected toggles as well as ability to turn off the the override.