Greet Caller by Name in C#.net

In this tutorial, we’ll show you how to implement Greet Caller by Name example in C#.Net to greet your partners when they call you. The code samples are free. In this tutorial we use Ozeki Phone System C#.net example. Let’s get started!

using System;
using System.Collections.Generic;
using OPSSDK;
using OPSSDKCommon.Model.Call;
using Ozeki.Media.MediaHandlers;
using Ozeki.VoIP;

namespace OPS_QuickStartExample_CSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new OpsClient();
            client.ErrorOccurred += (sender, info) =>
                Console.WriteLine(info.Message);

            if (!client.Login("ozekixepbx.ip", "admin", "12345"))
                return;

            var apiExtension = client.GetAPIExtension("9000");
            apiExtension.IncomingCall += IncomingCall;
            Console.ReadLine();
        }

        private static void IncomingCall(object sender, VoIPEventArgs  e)
        {
            var call = e.Item;
            var tts = new TextToSpeech();
            tts.Stopped += (s, ev) => call.HangUp();
            call.ConnectAudioSender(tts);

            call.CallStateChanged += (s, ev) =>
            {
                if (ev.Item == CallState.Answered)
                {
                    var phoneBook = new Dictionary();
                    phoneBook["+12345671"] = "John";
                    phoneBook["1001"] = "Kevin";
                    phoneBook["+12345675"] = "Jack";

                    var callerName = "Sir or Madame";

                    if (phoneBook.ContainsKey(call.OtherParty))
                        callerName = phoneBook[call.OtherParty];

                    tts.AddAndStartText("Hello" + callerName);
                }
                else if (ev.Item.IsCallEnded())
                    tts.Dispose();
            };

            call.Accept();
        }
    }
}
Code example 1 - Greeting the caller by using a phonebook
IN MORE DETAILS

Get the OPSSDK.dll

First you need to add the OPSSDK.dll reference to your project
(You can find OPSSDK.dll at C:\Program Files\Ozeki\Ozeki Phone System\Examples\.NET API\OPSSDK.dll).

Login and connect

Create a new instance of OpsClient, and subscribe to the ErrorOccurred event. Your program will communicate with the Ozeki Phone System through this client. Try to login with the client into the running Ozeki Phone System, with the address of the server(ozekixepbx.ip) and a valid username, password combination.

If you are ready, try to get an existing API Extension, with the GetApiExtension method of the OpsClient. Get more information about about the installation of API Extension.

Further steps

When you are ready with the initial steps above, subscribe the IncomingCall event of the apiExtension. This event will be triggered every time when the selected API Extension is called. In the mentioned event handler you will see the e.Item parameter, this parameter will be the call object. This call object has a CallState property, when the CallState is Ringing subscribe the CallStateChanged and the CallErrorOccurred events of the call object and Accept the call.

The CallStateChanged event will trigger when the call is going to another state. When the CallState is Answered, you can connect the devices to the call. When the call has ended, please do not forget to disconnect all devices from the call.

When the CallState is Answered, fill an associative array with telephone numbers and names. If the associative array contains the caller number, the method will get the corresponding CallerName from this array. After that, the method will start and connect a TextToSpeech object to the call, that will read the "Hello CallerName" text to the caller. Of course the CallerName will replace to the real caller name from the associative array "phone book". You can fill this array as you wish, for example from a file, that lists all numbers and names at your company. When the TextToSpeech is finished the call will hung up.

With these steps, you learned how to receive an incoming call, and greet the caller on his/her name.

More information