Greet Caller by Name in Visual Basic

Imports System
Imports Ozeki.Media.MediaHandlers
Imports OPSSDKCommon.Model.Call
Imports OPSSDK
Imports Ozeki.VoIP

Module Module1
    Public Sub Main(args As String())
        Dim client = New OpsClient()
        AddHandler client.ErrorOccurred, Sub(sender, info)
                                             Console.WriteLine(info.Message)
                                         End Sub

        If Not client.Login("ozekixepbx.ip", "admin", "12345") Then
            Return
        End If

        Dim apiExtension = client.GetAPIExtension("9000")
        AddHandler apiExtension.IncomingCall, AddressOf IncomingCall
        Console.ReadLine()
    End Sub

    Private Sub IncomingCall(sender As Object, e As VoIPEventArgs(Of ICall))
        Dim [call] = e.Item
        Dim tts = New TextToSpeech()

        AddHandler tts.Stopped, Sub(s, ev)
                                    [call].HangUp()
                                End Sub

        [call].ConnectAudioSender(tts)

        AddHandler [call].CallStateChanged,
                Sub(s, ev)
                    If ev.Item = CallState.Answered Then
                        Dim phoneBook = New Dictionary(Of String, String)()
                        phoneBook("+12345671") = "John"
                        phoneBook("1001") = "Kevin"
                        phoneBook("+12345675") = "Jack"

                        Dim callerName = "Sir or Madame"

                        If phoneBook.ContainsKey([call].OtherParty) Then
                            callerName = phoneBook([call].OtherParty)
                        End If

                        tts.AddAndStartText("Hello" & callerName)
                    ElseIf ev.Item.IsCallEnded() Then
                        tts.Dispose()
                    End If
                End Sub

        [call].Accept()
    End Sub
End Module
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