2

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?

AminRostami
  • 2,585
  • 3
  • 29
  • 45
  • You have read source code. You also can use TCPDUMP command on linux to see how it really go via socket. Please also note, that channel will not exists exactly after Hangup. And no delay between. – arheops Mar 20 '23 at 20:55
  • thx @arheops, can i use the `AGI` for return the variable value? if `yes`, have a example code? – AminRostami Mar 21 '23 at 08:11
  • Yes, you can. I saw no article about AGI without example of how to get variable. – arheops Mar 21 '23 at 18:02
  • What do you want to do with the results? To get it with AGI you need to listen to agi and enter the agi from the dialplan. When do you want to access the variable? – Shloime Rosenblum Mar 27 '23 at 17:21
  • hi @Shloime Rosenblum, I need to result in `c#` environment to save in database. This code snippet is simplified – AminRostami Mar 28 '23 at 11:22

1 Answers1

3

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();
Shloime Rosenblum
  • 927
  • 11
  • 26
  • thx @Shloime Rosenblum, I tried this solution but couldn't get an answer, can you complete example `Agi` Dialplan code?, please. – AminRostami Mar 29 '23 at 05:32
  • Is the c# program running on the same server as asterisk? Where did you get stuck? – Shloime Rosenblum Mar 29 '23 at 15:07
  • `c#` program running on `192.168.1.2` and `asterisk` server run at `192.168.1.126`. when I run `agi.Start();` program is hanging without error. what is the `MyNamespace`? is my `c#` namespace? Where should I put and calling `AGIScript ` script? – AminRostami Mar 30 '23 at 06:57
  • of course, I used the `windows desktop` application. not `web` application. – AminRostami Mar 30 '23 at 09:14
  • Yes your program is listening to the requests from asterisk. The best way is a console application. You should put your c# namespace of the class that has the service method. You should put in your dialplan `same => n,agi(agi://192.168.1.2/getresult)` – Shloime Rosenblum Mar 30 '23 at 12:45
  • the agi script should be in a separate class it can be anywhere in the program but you must map it in the main method with the full namespace and class name. `AGIScript` is a base class that needs to be inherited on the script class like above. When the dialplan enters the program it will call the service method. Make sure that Asternet is referenced in the project – Shloime Rosenblum Mar 30 '23 at 12:56
  • thx @ShloimeRosenblum, I tested but not working. this is the log of asterisk: `AGI Rx << VERBOSE "No script configured for URL 'agi://192.168.1.2/getresult' (script 'getresult')"`. is this working in the `windows Desktop` application? – AminRostami Mar 30 '23 at 14:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/252878/discussion-between-shloime-rosenblum-and-aminrostami). – Shloime Rosenblum Mar 30 '23 at 14:40