Call limit freeswitch (FusionPBX) on outbound calls
I had a special request to send two inbound calls to a phone number (PSTN), if that phone number already had two active calls it should send the next two callers to a second phone number and if that one received two calls any other caller would just receive a sound file saying all lines are busy. Sounds simple and sounds like something freeswitch limit would handle.
Well, my first problem was that I didn’t see this important note at the beginning:
Note that a limit is active within a given context — if you transfer an inbound call from the public dialplan to an extension in the default dialplan — any limit you just set in the public dialplan will be reset.
Well… what that means is that as soon as you transfer the call, the limit is set back to zero and you have no clue on how many calls you actually already sent out. This is when I came across this blog post regarding execute_on_answer
Another request was to play some audio file while it was ringing, I could have done it with ring groups in fusionpbx but that caused some other issues so I went with a custom dialplan which looks like this.

first we export the limit and execute it on answer
action export nolocal:execute_on_answer=limit db ${domain_name} studio1Then in a second condition we check if the limit counter is > 1
${cond(${limit_usage(db foo.example.net studio1)} > 1 ? true:false)}if this is true, we transfer the call to our second dial plan 602.
For the audio playback while ringing to work, this does the trick
ignore_early_media=true
instant_ringback=true
ringback=/var/lib/freeswitch/recordings/foo.examle.net/somefile.wav
transfer_ringback=/var/lib/freeswitch/recordings/foo.examle.net/somefile.wavNow to our second dialplan which looks like this

Here we basically do the same, only that if the limit on studio2 > 1 we just play the desired audio file and hang up.
