Asterisk / AMI / Perform actions based on the status of "Originate" action

Posted on 2013-12-05
Last Modified: 2014-02-28
I'm brand new to Asterisk. I'm using Asterisk AMI over http, and the "Originate" command to dial and connect two external phone numbers. Is it possible to add some additional logic (to the dialplan?) that would

a) do something when the calls are successfully connected (like make an http call, or log something in a db table)
b) do something else when the calls are NOT connected successfully (Say one of the parties accidentally hung up before the call could even get started).

I know I could repeated "poll" the server and check the events, but that seems inefficent if it could be done at a lower level. From what I've read so far in Asterisk: The Definitve Guide, I think it's possible ... I'm just not sure how yet :)

Any suggestions pointing in the right direction would be greatly appreciated!
Question by:_agx_
  • 3
  • 3
LVL 25

Expert Comment

ID: 39719599
This can be done strictly through the AMI interface by monitoring the activity through events.  It means watching all the traffic and sorting out those relevant to your originate but it works.

Here's a list of events in Asterisk 11 for example,

You will see the individual channel setup,  connection of those channels, and the shutdown.

The big benefit is that you don't have to modify the dialplan this way.  The drawback is there are a fair number of events to sift through.  I use this method for a status panel through AMI over TCP.  I would suggest logging all the call activity generated from one of your originate commands and you will have a example of the events you need to be monitoring for.

A second option would be to use the UserEvent in the dialplan which will send a event to the AMI with whatever information you want to send.  It is fairly simple but will require you to do some logic in your dialplan.  

You can also use the curl command to do a http request from the dialplan,  basically the same work as using a UserEvent in that you need to do the dialplan modifcations.

You can also post data to a database but then you would have to poll the database.  It is better to stay with something event driven.
LVL 52

Author Comment

ID: 39720278
> The drawback is there are a fair number of events to sift through.

Yeah, I was hoping to avoid monitoring and use something a little more direct, like trigger a curl call at the end of the call, and pass the final status ie connected, no answer, etc..  I'm just not sure where to add that to the dialplan.
LVL 25

Accepted Solution

kode99 earned 500 total points
ID: 39726396
I had to hook up my Asterisk lab setup.  With originate it is slightly different than a normal call.

So when you start the call it rings into the extension, then when that extension is answered the dialout occurs.  The portion where the extension rings is not really in the dialplan.  So if you reject that call nothing happens BUT you will get a even on the AMI telling you that the call was rejected etc.

This will be like this,  where ActionID matches the value you set when you made the call.
Event: OriginateResponse
Privilege: call,all
ActionID: ABC123456
Response: Failure
Channel: SIP/2223
Context: Polycom6701
Exten: 2231
Reason: 5
Uniqueid: <null>

What the Reason number means is seen here.

This even also fires a success after the call has been completed.

If you were watching the events you would also get Hangup event with a cause value and text info.  I think you need to be monitoring for call activity to see this event though.

Once you have picked up the extension the call will proceed normally in the dialplan from the point the originate specified.  So this is fairly simple in that you just need to add a bit of logic following the Dial line that checks the DIALSTATUS variable and do what you need to accordingly.
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

LVL 52

Author Comment

ID: 39727212
> The portion where the extension rings is not really in the dialplan ...
>  So if you reject that call nothing happens BUT you will get a even on
>  the AMI telling you that the call was rejected etc.

Interesting.. I didn't know that.  In my case I'm connecting two external numbers using the "Local" channel. Is the same true for the "Local" channel as well, since it's not an "extension" per se (at least not as I understand them)

My originate action between the two numbers looks something like this

Action: Originate
Channel:  Local/1NPANXX1111/@existing-internal-context
Context:  external-to-external-calls-context
Exten:  1NPANXX3333

BTW: Thanks for the good descriptions. It gives me a better picture of what's happening with the call flow.  I'm tied up on another task, but will set up monitoring and check the events you mentioned later.
LVL 25

Expert Comment

ID: 39757105
Have you looked at the AMI traffic?

The local channel is likely going to change where you need to do the dialplan modifications and may add a few more events to the AMI traffic.

I have not used local channels with my AMI apps so not sure how it will show up. I imagine you will see the setup for the local channel,  much the same as the setup for a direct sip to sip call,  then possibly a 2nd round for the connection of the two calls.

If I get the time this week I'll take a look at how the local channels showup in the AMI for connecting up calls.
LVL 52

Author Comment

ID: 39769202
Sorry, its my turn to be caught up with a deadline. I'll try and get back to this on the w/e

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There are no good configuration guides for HP-H3C router to LYNC on the web. :( Big statement, but we havent been able to find one yet. We did find the following document useful, but the information was not enough to use H3C router for use as a L…
Moving your enterprise fax infrastructure from in-house fax machines and servers to the cloud makes sense — from both an efficiency and productivity standpoint. But does migrating to a cloud fax solution mean you will no longer be able to send or re…
Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now