How to best run a script at the beginning of an inbound call?

Hi everyone,

I have a complex inbound route with calls going to Time Condition, then IVR, then extensions, then followMe, etc…

I would like to add a little script right before any of that happens and right at inbound. What is the best way to do this? Custom Context?

And how would I send the call back to it’s normal course of action as it is on the GUI? I would rather not disturb the GUI steps because it’s much easier to manage that.

Please give me your thoughts. I am also not sure how to put the call back on it’s GUI course. Some feedback on that would be great.


The easiest way I can think of to do it would be to simply change the inbound context of the trunk to something you’ve built. For example, you might add your custom code to extensions_custom.conf like this:

;Run Script
exten => _.,1,System(/path/to/your/script)
;Continue on to default “trunk handling” from GUI
exten => _.,n,Goto(from-trunk,${EXTEN},1)

; end of [from-trunk-sip-MyTrunkName]

Then under your trunk definition, set the inbound context to be from-trunk-sip-MyTrunkName. When a call comes in on that trunk, it will hit your defined context, run the script, and carry on to execute the rest of your FreePBX inbound routing/etc.

One important point would be to make sure that your script forks or runs in the background… otherwise Asterisk will sit there and wait for your script to finish executing before carrying on with the dialplan.