Mod event socket dotnet

From FreeSWITCH Wiki
Jump to: navigation, search

The .NET interface is still under heavily development, which means that the API can be changed without warning.

The event socket part should be pretty stable.

The source code can be found in freeswitch\scripts\contrib\verifier folder or at CodePlex [1] (where binaries are too).

Contents

Simple usage

The simplest way to use the EventManager class. It takes care of reconnecting and subscribing on events.

using System;
using FreeSwitch.EventSocket;

namespace IvrSocket
{
    class Program
    {
        private static EventManager mgr;
        static void Main(string[] args)
        {
            mgr = new EventManager();
            mgr.EventReceived += IvrManager;
            mgr.Subscribe(Event.ChannelAnswer, Event.ChannelHangup);
            mgr.Start("localhost");
            Console.ReadLine();
        }

        private static void IvrManager(EventBase receivedEvent)
        {
            Console.WriteLine(receivedEvent.Name);
            if (receivedEvent is EventChannelAnswer)
            {
                Console.WriteLine("Channel have been answered!");
            }
            else if (receivedEvent is EventChannelHangup)
            {
                Console.WriteLine("Bye bye baby!");
            }
        }
    }
}


IVR interface

The .Net implementation contains a IVR interface with allows you to create complete IVR applications in .Net.

The IVR interface is still under development. You can take a look at it and start using it in development. But dont use it in production.

Launching it in the dialplan

    <extension name="voicemail">
      <condition field="destination_number" expression="^8332$">
	<action application="set" data="ivr_name=voicemail" />
	<action application="answer" data="" />
	<action application="park" data="" />
      </condition>
    </extension>

Launching it from event socket

using System;
using FreeSwitch.EventSocket;

namespace IvrSocket
{
    class Program
    {
        private static EventManager mgr;
        static void Main(string[] args)
        {
            mgr = new EventManager();
            mgr.EventReceived += IvrManager;
            mgr.Subscribe(Event.All);
            mgr.Start("localhost");
            Console.ReadLine();
        }

        private static void IvrManager(EventBase receivedEvent)
        {
            Console.WriteLine(receivedEvent.Name);
            if (receivedEvent is EventChannelAnswer)
            {
                EventChannelAnswer answer = (EventChannelAnswer)receivedEvent;
                if (answer.Parameters["ivr_name"] == "voicemail")
                    new Voicemail(mgr, answer.UniqueId);
            }
            else if (receivedEvent is EventChannelHangup)
            {
                Console.WriteLine("Bye bye baby!");
            }
        }
    }
}

The actual app

using System;
using FreeSwitch.EventSocket;
using FreeSwitch.EventSocket.Ivr;

namespace IvrSocket
{
    /// <summary>
    /// Very simple voicemail app =)
    /// </summary>
    class Voicemail
    {
        private readonly IvrInterface _ivr;

        public Voicemail(EventManager mgr, string uuid)
        {
            _ivr = new IvrInterface(mgr, uuid);
            _ivr.BeginInvoke(OnIvr, null, null);
        }

        private void OnIvr(IvrInterface app)
        {
            _ivr.Play("record_after_beep.wav", IvrInterface.AllDigits);
            _ivr.Record("voicemails\\" + Guid.NewGuid() + ".wav", 60);
        }
    }
}

See Also