FreePBX: How to embed my code correctly?

Welcome to the forum @ogogon!

There are a few supported ways, it depends on what you want to do.

If you want to preprocess an inbound trunk call with dialplan before it hits an inbound route, you would set the trunk context GUI fields to something like from-trunk-preprocess (names are arbitrary) and then add the following to extensions_custom.conf:

[from-trunk-preprocess]
exten => _.,1,Noop(Entering user defined context from-trunk-preprocess in extensions_custom.conf)
exten => _.,n,     whatever
; addn'l lines as required
exten => _.,n,GoTo(from-trunk,${EXTEN},1)

Now suppose you want to add a FreePBX GUI destination with your own dialplan so you could set up a call flow in the GUI like Inbound Route → Custom Dialplan → Ring Group → etc. You do this with a Custom Destination. Browse to Admin, Custom Destination and add a gosub target to something like from-internal-ogogon,s,1 with this stub in extensions_custom.conf

[from-internal-ogogon]
exten => s,1,Noop(Entering user defined context from-internal-ogogon in extensions_custom.conf)
exten => s,n,     whatever
; addn'l lines as required
exten => s,n,Return

Enable the return option in the Custom Destination and choose the next step in the call flow.

There are dialplan hooks for use cases that don’t fit neatly into the above techniques which are discussed in detail here: Hooking for fun and income

You can see above how to add dialplan for incoming calls. In the same place you can add a hangup handler to have dialplan when the call ends. There is no easy way to hook the freepbx dialplan on call answer. Technically it can be done by overwriting the freepbx generated dialplan used for the call confirm option in FMFM, but it would be fragile. Thats something that could be done by monitoring AMI events.

1 Like