Incoming call receive using AGI?

Apr 16, 2014 at 1:57 PM
Hi everyone! I'm completely stupid, so I need your help. I can't get how to answer incoming call?
I'm connecting to the AMI, then successfuly start to listen for new calls. And when i get one, i can't understand what to do. I can only hangup it. I have read all discussions in this forum, also tried to understand this:
but i can't understand what execly i must do to answer.
What have i done understood already:
-I can't answer using AMI, AGI - is the only way;
-If i want to answer using AMI, i need to add in the extensions.conf file some string like this: exten => _X.,1,AGI(agi:async) (i did it);
-If i want to answer using AGI, i need to send some command, but i can't understand its structure and which command must send it.

I realy need your help, guys. It would be perfect, if u could show me some part of code, which can answer. I don't care, AGI or AMI.
Apr 16, 2014 at 3:32 PM

You're correct, you can't Answer a call with AMI (alone), the only way to Answer a call using AMI is to use Async AGI.

So, over to AGI. If you want to answer a call using FastAGI (currnetly the main way supported in AsterNET) then in your AGI script you need to call
Your call must first have been passed over to FastAGI (using the dial plan AGI application).

Here is an example of a FastAGI application:
You can see right near the top of the code, we call Answer() which answers the incoming call. We can then control the flow of the call within the AGIScript.

Maybe it would be better if you told us why you want to answer the call, i.e. what is it you're trying to achieve you can't get by using Answer in the Asterisk Dial plan?
Apr 16, 2014 at 9:16 PM
Hi skrusty! Thanks a lot for your response, I'll try it.
I'm trying just to receive a call. As i understood, Dial plan just distributes calls besides agents, am i right? I don't need to do this, my program just have to receive the call, answer it and hangup. How this call'll get to me is not my problem.
Apr 17, 2014 at 9:23 AM
If you want to control the flow of a call, you need to use an AGI. It's the most simply way to do it.

AGIs are programs that can, in real-time control call flow. AS a call is passed to the FastAGI server (which asternet helps provide) it gets attached to an AGIScript (which you write using the framework provided by AsterNET).

Once a call is connected to your AGIScript, you can tell asterisk what to do with that call. Answer, Hangup, play a file and so on.

You use the dial plan to control calls, routing and distribution. The dial plan is used to pass calls to an AGI (or FastAGI Server).

Apr 17, 2014 at 11:57 AM
Thanks a lot for your answers. Hope that'll solve my problem :)
I'll post here my code when i'll make that AGI work.
May 19, 2014 at 2:32 PM
So, i've finally found some time to see into AGI. I've looked throw all examples and tried to reply it.

First of all, i want to ask you people, how to start that fastAGI server and do i must do it at all?
Here: i see that server starts as the project runs. But if i trying to do the same with the form application - it stack at agiServer.Start() and then do nothing. What do i have to do?

The second question is: what parametrs do this function need to receive?

public override void Service(AGIRequest param1, AGIChannel param2)

I can't understand where\how this function is called and what's the first parametr of it?

For example: i'm listening asterisk with

manager.NewState += new NewStateEventHandler(manager_Events);

public static void manager_Events(object sender, ManagerEvent e)
        info = (NewStateEvent)e;
        //Answered call
        if (info.ChannelState == "6")
            // MessageBox.Show("Disp " + + "received this call " + e.Attributes["connectedlinename"]);
        if (info.ChannelState == "4")
          __ _//Answer(); !!!___
when the call is coming (info.ChannelState == "4"), it do something. I tried to create that ReDialerAGI class with the Answer() function, but i can't call it here: //Answer(); !!!

Please, help me someone!
May 19, 2014 at 3:11 PM
.Start is thread blocking, i.e. the listener loop runs on the calling thread!

I find it's best to use

new Thread(agi.start());

THat way you run the agi server on a new thread.

Hope that helps.
May 19, 2014 at 9:12 PM
Thanks for your answer, skrusty. Yes, this solved my problem with the agi server starting, but i still can't understand why do i need it and how can i answer the call?
May 19, 2014 at 9:33 PM

So, a AGI Script is totally handled by the FastAGI server engine. All you nee dto do is write code inside the Service method. The engine calls this method and passes in param1 and param2.

You can call Answer, hangup etc inside the service method.

In the example of ReDialer, the AGIScript is loaded by an instance of the FastAGI server engine. When asterisk passes a call to FastAGI, it matches up the script name passed from Asterisk and calls the associated AGIScript, defined by the Mapping Strategy.

I hope this clears up your confusion.
May 20, 2014 at 1:35 PM
Thx again. So i must in the extensions.conf handle every call throw this script, like
exten => _X.,1,(AGIScript)
and then FastAGI server will call the Service method every time when the call happens, do i right?

And how can i call the Service with the pressing the button? Do i have to steal param1 and param2 first time and then always send them like Service (param1,param2)? Or it can be done easier?
May 20, 2014 at 2:04 PM
OK, so yes, you need to pass calls via the dial plan to the AGI Server
exten => 1,1,agi(agi:// 
see this line for more information:

Your second question I am a little unsure on what you mean. You should never call the Service method yourself, that simply wont work! If you want to place a call into an AGI, lets say by pressing a button in a winforms application, then you need to use the other part of AsterNET, the Manager framework to "Originate" a call from asterisk to a handset or channel.

You can find more information here on Asterisk Manager Interface and Call Originate.
May 20, 2014 at 2:29 PM
I just want to Answer. Look: i'm connecting to the Asterisk and listening the channels. When the ChannelState is 4 (someone ringing), it displays the button. Now, when i press that button, i want to Answer. What should i do to make this algorithm?
What i have now:
Any advises?
May 20, 2014 at 3:53 PM

Firstly, FastAGI is not dependent on AMI (Manager Interface), if you pass a call to an AGI, the AGI controls the call flow, not the manager interface!

AMI can't answer a call. To answer a call, the call has to be connected to something. For example a handset or an AGI. If the call is not connected to something, it can't be answered.

When you connect your call to the AGI, it's the AGI that decides if it wants to answer the call or not.

Can you explain why you want to answer the call with a button, and what you expect to answer the call with (receptionist, IVR etc).

If you want your own phone to answer the call when you press the button, then this isn't possible. The action of your phone answering the call is not controlled by Asterisk.
May 20, 2014 at 4:24 PM
Edited May 20, 2014 at 4:31 PM
Thx for your quick responses.
I know, that i can't answer using AMI and AGI is the only way to answer, that's why i'm here. I'm, already trying to use FastAGI server.

"When you connect your call to the AGI, it's the AGI that decides if it wants to answer the call or not. "
Does it mean that i can't control the call directly in the programm? So, that method Service() decides call's fate? Then i have to make some switches inside this method and make some actions depending on entered data (?)

I'm developing a C# programm for taxi, they've got a telephony inside their company and they want to use Asterisk to make\receive calls to\from clients and each other. So i want something like this: call is coming, programm showing a message that someone calls, then i have 2 choises: answer and hangup. Click Answer - i answer the call, click hangup - hangup the phone.

Asterisk is configurated already and tested via SIP-telephony - all works fine.
May 20, 2014 at 5:15 PM
OK, what you really want to do then is just use AMI.

Capture events like newstate to see incoming calls.

when you want to have that call answered, use originate to send the channel to a sip client (handset etc).

Where you might consider using an AGI to to "hold" the call while you decide what to do with it. You can use something maybe like ASyncAGI.

You should also consider asking asterisk specific questions on irc or the mailing list (
May 21, 2014 at 3:10 PM
Tried to Originate when someone is calling, nothing. And how can you originate to that channel which call is coming from? I can originate to anyone (using sip) like that:
 OriginateAction oa = new OriginateAction();
                oa.Channel = "SIP/101";
                oa.Context = "from-sip";
                oa.Exten = textBox4.Text;
                oa.CallerId = "101";
                oa.Priority = "1";
               // oa.Application = "Dial";
                oa.Timeout = 30000;
                oa.ActionId = "ABC4567890123456789";
                if (textBox4.Text.Length > 0)
                    ManagerResponse mr = Global.manager.SendAction(oa);
But how can i originate when someone is calling to me already? I can't use like this, because it has no sense:
 private void button1_Click(object sender, EventArgs e)
OriginateAction oa = new OriginateAction();
            oa.Channel =; //that's the channel where the call is coming from
            oa.Context = "from-sip";
            oa.Timeout = 30000;
            oa.Priority = "1";
            ManagerResponse mr = Global.manager.SendAction(oa);
Can you explain me this moment, please?
May 21, 2014 at 3:38 PM
You Originate from the channel on which they are calling you, to your extension in the dialplan. So Channel would be the channel of the caller, and Exten and Context would be you in the dial plan.
Aug 25, 2014 at 2:13 AM
Edited Aug 25, 2014 at 2:13 AM
Hi, i have the same situation as dolgovec23, am developing a c# application that allow me to receive and make call,
i have created two buttons so when a call is coming i can answer or hang up it .. i'am connecting to asterisk via AMI
i have some questions .. if s.o decide to call me what would be my number (oa.Exten )???how can i define it in the dialplan ? and how can i answer that call using AMI??

also, if i decide to make call what would be "".

help please ??
dolgovecc23 can you post your solution ????
Aug 25, 2014 at 9:29 PM
This is mostly an Asterisk question, but I hope I can answer this without going into too much detail.
  1. oa.Exten: This would be the extesion number defined in extensions.conf (and would have to exist in the context define by oa.Context). For more information about dialplans in asterisk, please see (or google extensions.conf)
  2. If you want your application to phone a telephone number (or Originate) it from your handset, then the channel could need to be your outbound peer. Again, this is more an Asterisk question and I could replicate a lot of already very well put documentation. Please google something like "asterisk dial sip peer".
I hope this goes some way to helping.
Aug 25, 2014 at 10:33 PM
thanks skurty,
well i successfully originated calls between two extensions which are already connected to softphone using AsterNET.
for example if i want to call phone B from phone A. i should first accept a call from asterisk on my phone A so that the call will originated to phone B.
Now my problem is how can i let my c# application auto-answer the call from asterisk to originate call. i have created two extensions and one of them is connected to softphone and the other one is not connected. i always get a failed response because the call is not picked up on extension A. ..
i hope that you'll understand me, and i will so grateful if you help me to resolve this problem.
here the code i used to originate call.

        OriginateAction oc = new OriginateAction();
            oc.Context = incoming_call;
            oc.Priority = "1";
            oc.Channel = SIP/6002;                      //6002 is not connected to any phone (channel that should be connected to my application)
            oc.CallerId = ORIGINATE_CALLERID;
            oc.Exten = 6001;                           //6001 is connected to softphone
            oc.Timeout = ORIGINATE_TIMEOUT;
            oc.Async= true;
            ManagerResponse originateResponse = manager.SendAction(oc, oc.Timeout);


Aug 26, 2014 at 7:33 PM
Right, AsterNET and Asterisk itself can't tell a phone to Auto Answer a call. However, you can configure most phones to answer a phone automatically. However, depending on what soft/hard phone you are using, you can send special sip messages telling the phone to auto answer. I suggest you google this or ask on the Asterisk-Users mailing list. This question is now specific to asterisk and not AsterNET.

Aug 26, 2014 at 11:36 PM
Edited Aug 30, 2014 at 4:02 AM
thanks for reply skurty :) am wondering is there a method that let my application receive any incoming call i mean without specifying "".???? in fact when i write"SIP/6002"it only allow the phone where the channel is sip/6002 make call to my application. how can i change that to let any phone call me. thanks for advance.
Apr 3, 2015 at 11:12 AM
Hi Can you please guide how from your application you have called to another SIP peer.