Perform a custom dialplan action on extension answer

How do I perform custom action X when an extension answers a ringing channel?

It’s a simple ask which comes up periodically in the forum and I have occasionally tried to come up with a supportable way of doing this just with dialplan in FreePBX. In the past I have hacked away at the macro-confirm context, which works but is an ugly solution. Today after this thread, I thought of a totally different approach, and came up with this:

exten => s,1,Noop(Entering user defined context macro-run-on-answer in extensions_custom.conf)
exten => s,n,DumpChan
; add additional lines as requred
exten => s,n,Return

; dial prefix **44 you can use to dial local extensions that will exectue a macro when the 
; extension answers
exten => _**44XX.,1,Noop(Entering user defined context from-internal-custom in extensions_custom.conf)
exten => _**44XX.,n,Dial(Local/${EXTEN:4}@from-internal,,U(run-on-answer));
exten => _**44XX.,n,Hangup

The above goes in extensions_custom.conf and then reload dialplan. When you call a local extension with the **44 prefix, it will run the Macro on answer. I would use this with static queue agents by defining them with a prefix instead of just the extension number.

I’ve tested a bit, and appears to be working as expected, but needs a real workout before putting into production.

1 Like

So this is what worked for me, thanks once again @lgaetz
I just updated extensions_custom.conf as follows;

include => from-queue

exten => 705,1,NoOp(Call from Sales queue)
exten => 705,n,Dial(SIP/705,U(run-on-answer))

exten => 708,1,NoOp(Call from Sales queue)
exten => 708,n,Dial(SIP/708,U(run-on-answer))

exten => s,1,Noop(After agent answering)
exten => s,n,AGI(inserttest.php)
exten => s,n,Return

If you are using this with a queue, you should rather use this solution:

Modifying the from-queue context may not be a good idea.

@PitzKey well this didn’t seem to work for me, care to explain further why modifying this isn’t cool?

I created the the custom dial plan and the dynamic route, the routed my inbound calls to the route but for some reason/s it didn’t hit the context when i dialed the queue.
But so far the above configurations I gave seem to work perfectly and I am able to run the script and save some data in a db I created. Actually what you could also help me on is how to access the data I am now entering in the DB and display back, so far I seem to find out that I need to lean ARI, which I have started.

You’re modifying core contexts generated by FreePBX. While I’m sure in your basic testing it’s working as expected, there are going to be unintended consequences of doing this. I recommend you put some effort into making some of the suggestions work instead of doing it your way.

1 Like