Skip to content

Using C#/.Net to Invoke the Adhearsion API via REST

June 8, 2009

Recently an Adhearsion community member, David Lawal, built upon the PHP/REST example I posted back in February. David comes from a C#/.Net background and was beginning to learn Ruby in order to use Adhearsion. Then, in his own words:

“…I needed to leverage ‘all the work’ done in .Net…As I discovered, the marriage of .Net and Adhearsion was lovely, thanks to the RESTful RPC [API of Adhearsion]”

So David created an example and has graciously shared it with the rest of us. The example includes a component in Adhearsion that has this method extending the ‘methods_for :rpc’:

def launch_call_rpc(vars)
  ahn_log.ami vars   
  
  channel = vars["src"]
  exten = vars["dest"]
  options = { "Channel" => channel,
              "Context" =>  "callback",
              "Exten" =>    exten,
              "Priority" => “1”,
              "Callerid" => “33333” }
  result = Adhearsion::VoIP::Asterisk.manager_interface.originate options
  
  ahn_log.ami "status: call Queued" 
  ahn_log.ami result   
end

Following the full README and example here, this C# code may then be used to invoke the Adhearsion REST API:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JSONSharp;
using System.Net;
using System.IO;

namespace adhearsion
{//adhearsion server is http://192.168.1.62:5000
    class Program
    {
        static void Main(string[] args)
        {
            Program.connect();
        }
        static void connect()
        {
            try
            {     
                ahnparams ahn = new ahnparams();
                ahn.src = @"SIP/4031234567@192.168.1.200";
                ahn.dest = @"7801234567";
                //Pass it to our static reflector, which will build
                JSONReflector jsonReflector = new JSONReflector(ahn);   //JSONSharp converts the class to JSON format
                //Console.WriteLine(jsonReflector.ToString());
                NetworkCredential myCred = new NetworkCredential("jicksta", "roflcopterz");

                CredentialCache myCache = new CredentialCache();

                myCache.Add(new Uri("http://192.168.1.62:5000"), "Basic", myCred);


                WebRequest request = WebRequest.Create("http://192.168.1.62:5000/launch_call_rpc");
                request.Credentials = myCache;
                string postData = "["+jsonReflector.ToString()+"]";  //had to add the [] for it to work with restful_rpc just like php json
                Console.WriteLine(postData);
                request.Method = "POST";
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                // Set the ContentType property of the WebRequest.
                request.ContentType = "application/x-www-form-urlencoded";
                // Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length;
                // Get the request stream.
                Stream dataStream = request.GetRequestStream();
                // Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.
                dataStream.Close();

                WebResponse response = request.GetResponse();
                // Display the status.
                Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.
                string responseFromServer = reader.ReadToEnd();
                // Display the content.
                Console.WriteLine(responseFromServer);
                // Clean up the streams.
                reader.Close();
                dataStream.Close();
                response.Close();
            }
            catch (Exception e)
                {
                    throw e;
                }

        }
    }
}

This is a great example of the cross-platform capabilities of Adhearsion that you get for free via REST. You do not have to be a Ruby-ist to take advantage of adding voice capabilities to your web applications. You may use virtually any language and platform that may consume web services.

We definitely appreciate it when our community takes the time to share their Adhearsion experiences with the rest of us. Thanks David, and for the rest of you the full example with his README is available here.

2 Comments leave one →
  1. Bob permalink
    June 17, 2009 10:16 am

    Nice article but couple of things still not clear from me

    1) in readme, it says extension.conf need to be changed with
    ….
    exten => _X.,4,Dial(SIP/${EXTEN}@192.168.1.200,20,Tt)
    ….
    when you calling with src and destination, why you need changes to extension.conf

    2) Is there is a way to send encrypted credentials to restful_rpc instead of plain text

    Bob

  2. June 24, 2009 6:52 am

    On point 2, yes, as you may serve the RESTful RPC interface of Adhearsion up over something like Apache or Nginx with Passenger.

Leave a comment