Execute AGI Script 15 seconds after call is answered

Hey all,

So first off let me be very upfront in saying this may not be the right forum to post this question in however after spending a couple days reading docs and posts on this forum I figured one of you smart people here would be able to help this newbie out. So here is what I am trying to accomplish I want to to have a DID hit a custom extension that will ring either another extension or a ring group and once that call is picked up and 15 seconds pass, I want execute an AGI script which will make a simple HTTP request to a URL.So far I was able to setup the inbound DID and a simple custom extension in my extensions_custom.conf file. I can now dial in and get the AGI script to execute by using an inbound route and also a Custom Destination but where I am stuck at is how to get the call sent to a ring group and more importantly execute an AGI script after 15 seconds of the call being answered. I have seen other posts mention using the macro or queue applications but I just can’t seem to figure out how to accomplish my end goal. Yes I have RTFM and yes I have spent at least 2 days on this forum but I am still lost so if any of you could help me out that would be amazing. Below is my simple extension I setup in my extensions_custom.conf file. Thanks so much in advance!

include => agi-test
exten => 2000,1,Answer()
exten => 2000,n,wait(3)
exten => 2000,n,AGI(test.py)
exten => 2000,n,Playback(hello-world)
exten => 2000,n,Hangup()

<best announcer voice> “Thank you for playing Stump the telephony guy. Gene, tell him what he’s won.”

I don’t even know if I’d be able to figure out how to do that if someone else told me.

Stay tuned, though. One of these other guys might have an idea.

Awesome answer Dave lol! I am hoping someone has an idea.

Your priority 1 answers the call, no prob
nothing is heard by the caller until after 3 seconds of silence you call your agi script, (should that not need to be 15?) but it is unlikely that your agi script is set to play any media on the channel.

What experience do you want your callers experience for the first 3/15 seconds?

post your agi script.

Hey @dicko (can’t help but laugh while typing that),

So preferably what I would like to happen is someone on our end in our office answers the phone (via an extension or ring group) and talks to the potential customer and after 15 seconds of talking with the customer my AGI script would execute and make a call to a URL via HTTP. The test.py AGI script right now is just a test script which writes to a file for testing purposes. I was just using to make sure my AGI script fires. Below is the AGI python script. So here is a little more about our use case depending on the amount of time someone stays on the phone with us we can sell a lead to a potential buyer. That is why we need the URL call to be made 15 seconds into the call as that is a requirement from one of our buyers. I hope this makes a little more sense and if not please let me know. Thanks again.

from datetime import datetime

now = datetime.now()

f = open(’/tmp/test.txt’,‘a’)
f.write(str(now) + ‘\n’)

Using custom destinations or dialplan hooks you can easily do things before the dial happens or do something after hangup but it is excruciatingly difficult to do something on answer using only dialplan. The only way to accomplish this with dialplan is to call the macro/sub at time of dialing to the extension, and the confirm calls macro is already used for this purpose. So you could hack the confirm calls macro put your own dialplan there, but I think far better to use AMI (or even ARI) to track the call progress independently from dialplan. This is not a beginner project.


For the caller to speak to a person it needs to be answered by an endpoint that connects the call to a person, at that point the call is bridged ( asterisk is a B2BUA) and the dialplan will not continue until the answer-er hangs up or transfers the call to a context that handles your agi script.

As @lgaetz said, unless you are really good, you need to rethink your plan.


might be an option.

@lgaetz and @dicko,

So I like to think of myself as a pretty capable when it comes to coding etc. Let me ask if this might work.

I believe I can fork off an AGI execution and let the dialplan continue in which case the call would be answered and bridged to an extension via a ring group. Then I could have the AGI execution I forked off monitor the status of the call via AMI and finally after 15 seconds execute another AGI execution which would make the HTTP request? I looks like in Asterisk 1.6 the ability to execute an AGI execution via AMI is now supported. Let me know if that might work? I am hoping so.

Maybe but you are in a world of howtos and whydoesntitwork


(asterisk currently supported versions (13 , 15 or 16) is many years ahead of 1.6)


Good point and damn those pesky .'s! I am actually on 16.

core show version
Asterisk 16.1.1 built by root @ ip-10-0-0-161 on a x86_64 running Linux on 2019-01-30 19:03:31 UTC

With that said I just read the ARI documentation and I saw the words Resftul and JSON and immediately got a big smile on my face. I am going to dig deeper into this API and see if it can serve my purposes. You guys rock and I thank you very much. If any ideas on how to support my use case pop up in your mind please let me know. Thanks again.

I would do this in a separate script, independent of Asterisk.

Do you really need to ‘present the lead’ while the prospect is still on the line? If not, I’d just read the CDRs e.g. once an hour, find calls that lasted more than 15 seconds, were answered by an agent in the appropriate group and were from a ‘new’ number. You can configure FreePBX to write CDRs into a simple CSV file.

If you need real-time, use CEL (call event logging) instead. Set FreePBX to create a CSV, have your script follow the growing file, start a 15-second timer when a candidate call is answered, cancel the timer if the call ends, post the lead if the timer expires.

1 Like


That is an entirely different approach but it might be a much easier one! I like it! Thank you so much.

Nice, extend it with a well constructed trigger:-


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.