Search code examples
c#asteriskasteriskamidialplanasternet

how to get variable value from asterisk with asternet


I use the asterNet for manage event IN asterisk.

I need to get value of variable (result) in c# from dialplan query.

exten => test,1, NoOp(************ test ****************);
same => n,Answer();
same => n,Playback(hellow-world);
same => n,set(result=123456);
same => n,Hangup();

I used the following query to get result variable:

private string GetVar(string channel)
{
    string result = "";
    try
    {
        var gv = new GetVarAction(channel, "result");
        ManagerResponse mr = manager.SendAction(gv);
        result = mr.Attributes["result"];
     }
     catch (Exception ex)
     { }
     return result;
}

but is's encounter with this error:

timeout waiting for response to originate

I searched the web but did not find anything useful.

Can anyone help me?


Solution

  • You can get the results with AGI.

    You need to setup FastAGI with Asternet and then you can get the results to save to you DB.

    To setup FastAGI you need to set the script to the specific class where you will handle the call and start the FastAGI service from you programs Main method in program.cs

    static void Main(string[] args)
    {
            AsteriskFastAGI agi = new AsteriskFastAGI();
            
    
            agi.MappingStrategy = new GeneralMappingStrategy(new List<ScriptMapping>()
            {
                new ScriptMapping() {
                    ScriptClass = "MyNamespace.GetResult",
                    ScriptName = "getresult"
                }
    
            });
    
            agi.Start();
    }
    

    and create a class GetResult which inherits AGIScript where you will handle the call inside the Service method and use the GetVariable function to get the variable content

    namespace MyNamespace
    {     
        class GetResult : AGIScript
        {
             public override void Service(AGIRequest request, AGIChannel channel)
             {    
                try
                {
                    string result = GetVariable("result")
                    //save to DB
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
    }
    

    and you need to enter the agi from your dialplan. If the c# program is running on the same server as asterisk you can do localhost same => n,agi(agi://localhost/getresult) or put your server's local ip address same => n,agi(agi://xxx.xxx.x.x/getresult) and if the c# program is running on a remote server put the remote server ip address same => n,agi(agi://xxx.xxx.xx.xxx/getresult) and make sure that your asterisk server public ip is open in the remote server's firewall.

    exten => test,1, NoOp(************ test ****************);
    same => n,Answer();
    same => n,Playback(hellow-world);
    same => n,set(result=123456);
    same => n,agi(agi://yourserverip/getresult)
    same => n,Hangup();