Getting Started with the Speech Server 2007 API

Speech Server 2007 has a really cool Windows Workflow based programming model that lets you quickly build interactive voice response applications. For many applications it is all you will ever need.

Sometimes however you find the workflow model just isn't the right fit. If you're looking for really fine-grained control over the application, or you simply prefer to work in code, then the Core API is what you need.

Unfortunately figuring out you want to use the API is a lot easier than figuring out how to start using it. There is very little documentation and no Visual Studio project templates or samples included with Speech Server.

I'll do my best to give a brick-simple explanation of how to get your first core API project started. You can also download the zipped project files.

1) First you'll need to create a new Voice Response Workflow Application. We'll use the project that gets generated as our foundation.

image

2) When asked for the application resources you'll want to uncheck everything.

image

3) Open up the Class1.cs file and remove all of the references to the VoiceResponseWorkflow1 class. The resulting class should look like the following (I removed the comments in the code for brevity):

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SpeechServer ;
using Microsoft.SpeechServer.Dialog;

namespace VoiceResponseWorkflowApplication1
{
    public class Class1 : IHostedSpeechApplication
    {     
        private IApplicationHost _host;

        public void Start(IApplicationHost host)
        {
            if (host != null)
            {
                _host = host;
                _host.TelephonySession.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
            }
            else
            {
                throw new ArgumentNullException("host");
            }
        }
        public void Stop(bool immediate)
        {
        }

        public void OnUnhandledException(Exception exception)
        {
            if (exception != null)
            {
                _host.TelephonySession.LoggingManager.LogApplicationError(100, "An unexpected exception occurred: {0}", exception.Message);
            }
            else
            {
                _host.TelephonySession.LoggingManager.LogApplicationError(100, "An unknown exception occurred: {0}", System.Environment.StackTrace);
            }

            _host.OnCompleted();
        }
    }
}

That's all folks. Class1.cs is now the starting point of your Core API application. As a further example, lets take the project and add some code to turn it into an outbound dialing "Hello World" application.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SpeechServer ;
using Microsoft.SpeechServer.Dialog;

namespace VoiceResponseWorkflowApplication1
{
    public class Class1 : IHostedSpeechApplication
    {     
        private IApplicationHost _host;

        public void Start(IApplicationHost host)
        {
            if (host != null)
            {
                _host = host;
                _host.TelephonySession.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
                // Dial and outbound call (make sure you change these numbers :-)
                _host.TelephonySession.OpenCompleted += new EventHandler<AsyncCompletedEventArgs>(TelephonySession_OpenCompleted);
                _host.TelephonySession.OpenAsync("7813062200", "8887006263");
            }
            else
            {
                throw new ArgumentNullException("host");
            }
        }

        void TelephonySession_OpenCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                _host.TelephonySession.Close();
            }
            else
            {
                _host.TelephonySession.Synthesizer.SpeakCompleted += new EventHandler<Microsoft.SpeechServer.Synthesis.SpeakCompletedEventArgs>(Synthesizer_SpeakCompleted);
                _host.TelephonySession.Synthesizer.SpeakAsync("Hello World", Microsoft.SpeechServer.Synthesis.SynthesisTextFormat.PlainText);
            }
        }

        void Synthesizer_SpeakCompleted(object sender, Microsoft.SpeechServer.Synthesis.SpeakCompletedEventArgs e)
        {
            _host.TelephonySession.Close();
        }
        public void Stop(bool immediate)
        {
        }

        public void OnUnhandledException(Exception exception)
        {
            if (exception != null)
            {
                _host.TelephonySession.LoggingManager.LogApplicationError(100, "An unexpected exception occurred: {0}", exception.Message);
            }
            else
            {
                _host.TelephonySession.LoggingManager.LogApplicationError(100, "An unknown exception occurred: {0}", System.Environment.StackTrace);
            }

            _host.OnCompleted();
        }
    }
}

(download the zipped project)

Posted: Wednesday, July 09, 2008 2:21 PM by MarcLaFleur with 5 comment(s)

Comments

# Executing a Speech Server Workflow via the API @Thursday, July 10, 2008 6:23 PM

In my previous post I outlined a basic framework for using the Core API for Speech Server 2007. Today

Marc LaFleur

# re: Getting Started with the Speech Server 2007 API @Friday, July 11, 2008 4:17 AM

It's a good idea to unsubscribe Synthesizer_SpeakCompleted once it's fired.  It doesn't matter in this example, however if you were to subscribe a new handler and call SpeakAsync again, both handlers will fire, which might cause some confusion.  Therefore it's a good practice to always unsubscribe the event handler.

Anthony Bearon [MSFT]

# re: Getting Started with the Speech Server 2007 API @Friday, July 11, 2008 9:02 AM

That is an excellent point Anthony.

MarcLaFleur

# Getting Started with the Core API @Tuesday, July 22, 2008 9:46 AM

With the introduction of Voice Response Workflows in Speech Server 2007, Microsoft has greatly simplified

Speaking From The Edge

# re: Getting Started with the Speech Server 2007 API @Tuesday, July 22, 2008 10:05 PM

hi i have a problem with the built-in date grammar.can i use built-in date for "this week" and "next week".

suppose if today is tuesday if i say next wednesday it is returning tomorrow's date but i want next week date. If i say next monday then it returns next week date means for the past days it is returning next week date.

       Is it possible for me to use both this week and next week dates in built-in date grammar.if possible give me the solution ...

john

Leave a Comment

(required) 
(required) 
(optional)
(required)