Cookie CSS

Saturday, November 21, 2015

Creating your first UCMA application - Part III

This week we continue with our UCMA app and look closely how to handle our incoming calls.  Last week we looked at our code in the registration of the application endpoint that registered to handle the calls.

_appEndpoint.RegisterForIncomingCall<AudioVideoCall>(incomingAVCall_CallReceived);

So let's examine what we do in this procedure.

 private void incomingAVCall_CallReceived(object sender, CallReceivedEventArgs<AudioVideoCall> e)
        {
            try
            {
                CallReceivedEventArgs<AudioVideoCall> args = e;

                _logger.Log("Incoming call.");

                AudioVideoCall _call = args.Call;

                bool done = false;

                answerCall(_call);

                Thread.Sleep(500);

                SaySomething says = new SaySomething(_call, "Hello, I am the Bridge Communications virtual operator.  I am hear to help you find the right person.  Please tell me the name of the person you are searching for, or the reason for your call.");
                says.Start();

                HearSomething hr = new HearSomething();

                string tohear = "";

                foreach (classUsers cu in _users)
                {
                    tohear += cu.Name + "," + cu.Hear + ",";
                }
                tohear = tohear.TrimEnd(',');

                hr.Start(_call, tohear);

                while (_call.State != CallState.Terminating && !done)
                {

                    int x = 0;

                    while (x < 1000 && string.IsNullOrEmpty(hr._whatIHeard))
                    {
                        Thread.Sleep(10);
                        x++;
                    }

                    if (string.IsNullOrEmpty(hr._whatIHeard))
                    {
                        SaySomething says4 = new SaySomething(_call, "Sorry I didn't find any matching employee, can you please try again?");
                        says4.Start();

                        hr._whatIHeard = "";

                        hr.Start(_call, tohear);
                    }
                    else
                    {
                        string fnd = hr._whatIHeard.ToUpper();

                        foreach (classUsers cu in _users)
                        {
                            try
                            {
                                if (cu.Name.ToUpper() == fnd | cu.Hear.ToUpper() == fnd)
                                {
                                    CallTransferOptions unattendedTransferOptions = new CallTransferOptions(CallTransferType.Unattended);
                                    
                                    _logger.Log("Transfering call to " + cu.Name);
                                    done = true;

                                    SaySomething says5 = new SaySomething(_call, "Transferring your call to " + cu.Name +", stand by");
                                    says5.Start();

                                    _call.BeginTransfer(cu.Uri, myar => {

                                        try
                                        {
                                            _call.EndTransfer(myar);
                                        }
                                        catch { }

                                    
                                    }, null);

                                }
                            }
                            catch { }
                        }
                    }

                    Thread.Sleep(50);
                }


            }
            catch { }

        }

So there is a lot going on here to answer the call, ask who or what the caller wants, and transfer the call to proper location.  Let's begin this week with how to answer the call.  You will see at the top of the code we log it, then create an AudioVideoCall from the args which we create from the CallReceivedEventArgs.  Once we have the call option we call the answerCall proc.


private void answerCall(AudioVideoCall c)
        {
            try
            {
                AudioVideoCall _call = c;

                try
                {
                    _logger.Log("Accepting call...");

                    _call.BeginAccept(
                        ar =>
                        {
                            try
                            {
                                _call.EndAccept(ar);
                                _logger.Log("Accepted call.");

                            }
                            catch (RealTimeException ex)
                            {
                                _logger.Log("Failed accepting call.", ex);
                            }
                        },
                        null
                    );
                }
                catch (InvalidOperationException ex)
                {
                    _logger.Log("Failed accepting call.", ex);
                }

            }
            catch { }

        }

Next week we'll continue with our call processing.


Doug Routledge, C# Lync, Skype for Business, SQL, Exchange, UC Developer  BridgeOC
Twitter - @droutledge @ndbridge

No comments:

Post a Comment

Any spam comments will be deleted and your user account will be disabled.