Example #1
0
        internal PlayMediaAndCollectAction(Call call, string controlID, List <CallMedia> media, CallCollect collect)
        {
            Call      = call;
            ControlID = controlID;
            Media     = media;
            Collect   = collect;

            Call.OnCollect += Call_OnCollect;
        }
Example #2
0
        public PromptAction PromptAudioAsync(string url, CallCollect collect)
        {
            List <CallMedia> play = new List <CallMedia>
            {
                new CallMedia
                {
                    Type       = CallMedia.MediaType.audio,
                    Parameters = new CallMedia.AudioParams
                    {
                        URL = url,
                    }
                }
            };

            return(PromptAsync(play, collect));
        }
Example #3
0
        public PromptAction PromptTTSAsync(string text, CallCollect collect, string gender = null, string language = null)
        {
            List <CallMedia> play = new List <CallMedia>
            {
                new CallMedia
                {
                    Type       = CallMedia.MediaType.tts,
                    Parameters = new CallMedia.TTSParams
                    {
                        Text     = text,
                        Gender   = gender,
                        Language = language,
                    }
                }
            };

            return(PromptAsync(play, collect));
        }
Example #4
0
        public PromptAction PromptAsync(List <CallMedia> play, CallCollect collect)
        {
            PromptAction action = new PromptAction
            {
                Call           = this,
                ControlID      = Guid.NewGuid().ToString(),
                PlayPayload    = play,
                CollectPayload = collect,
            };

            Task.Run(async() =>
            {
                PlayStateChangeCallback playStateChangeCallback = (a, c, e, p) => action.State = p.State;
                OnPlayStateChange += playStateChangeCallback;

                action.Result    = await InternalPromptAsync(action.ControlID, play, collect);
                action.Completed = true;

                OnPlayStateChange -= playStateChangeCallback;
            });
            return(action);
        }
Example #5
0
        private async Task <PromptResult> InternalPromptAsync(string controlID, List <CallMedia> play, CallCollect collect)
        {
            await API.API.SetupAsync();

            PromptResult resultPrompt = new PromptResult();
            TaskCompletionSource <bool> tcsCompletion = new TaskCompletionSource <bool>();

            // Hook callbacks temporarily to catch required events
            PlayErrorCallback errorCallback = (a, c, e, p) =>
            {
                resultPrompt.Event = new Event(e.EventType, JObject.FromObject(p));
                tcsCompletion.SetResult(false);
            };
            PromptCallback promptCallback = (a, c, e, p) =>
            {
                resultPrompt.Event = new Event(e.EventType, JObject.FromObject(p));
                resultPrompt.Type  = p.Result.Type;

                switch (resultPrompt.Type)
                {
                case CallCollectType.digit:
                {
                    CallingEventParams.CollectParams.ResultParams.DigitParams digitParams = p.Result.ParametersAs <CallingEventParams.CollectParams.ResultParams.DigitParams>();
                    resultPrompt.Result     = digitParams.Digits;
                    resultPrompt.Terminator = digitParams.Terminator;

                    tcsCompletion.SetResult(true);
                    break;
                }

                case CallCollectType.speech:
                {
                    CallingEventParams.CollectParams.ResultParams.SpeechParams speechParams = p.Result.ParametersAs <CallingEventParams.CollectParams.ResultParams.SpeechParams>();
                    resultPrompt.Result     = speechParams.Text;
                    resultPrompt.Confidence = speechParams.Confidence;

                    tcsCompletion.SetResult(true);
                    break;
                }

                default:
                {
                    tcsCompletion.SetResult(false);
                    break;
                }
                }
            };

            OnPlayError += errorCallback;
            OnPrompt    += promptCallback;

            try
            {
                Task <LL_PlayAndCollectResult> taskLLPlayAndCollect = mAPI.LL_PlayAndCollectAsync(new LL_PlayAndCollectParams()
                {
                    NodeID    = mNodeID,
                    CallID    = mID,
                    ControlID = controlID,
                    Play      = play,
                    Collect   = collect,
                });

                // The use of await rethrows exceptions from the task
                LL_PlayAndCollectResult resultLLPlayAndCollect = await taskLLPlayAndCollect;
                if (resultLLPlayAndCollect.Code == "200")
                {
                    mLogger.LogDebug("Prompt {0} for call {1} waiting for completion events", controlID, ID);

                    resultPrompt.Successful = await tcsCompletion.Task;

                    mLogger.LogDebug("Prompt {0} for call {1} {2}", controlID, ID, resultPrompt.Successful ? "successful" : "unsuccessful");
                }
            }
            catch (Exception exc)
            {
                mLogger.LogError(exc, "Prompt {0} for call {1} exception", controlID, ID);
            }

            // Unhook temporary callbacks
            OnPlayError -= errorCallback;
            OnPrompt    -= promptCallback;

            return(resultPrompt);
        }
Example #6
0
 public PromptResult Prompt(List <CallMedia> play, CallCollect collect)
 {
     return(InternalPromptAsync(Guid.NewGuid().ToString(), play, collect).Result);
 }
Example #7
0
        public async Task <PlaySilenceAndCollectAction> PlaySilenceAndCollectAsync(CallMedia.SilenceParams silence, CallCollect collect)
        {
            CallMedia media = new CallMedia()
            {
                Type = CallMedia.MediaType.silence, Parameters = JObject.FromObject(silence)
            };
            PlayMediaAndCollectAction playMediaAndCollectAction = await PlayMediaAndCollectAsync(new List <CallMedia> {
                media
            }, collect);

            return(new PlaySilenceAndCollectAction(playMediaAndCollectAction));
        }
Example #8
0
 public PlaySilenceAndCollectAction PlaySilenceAndCollect(CallMedia.SilenceParams silence, CallCollect collect)
 {
     return(PlaySilenceAndCollectAsync(silence, collect).Result);
 }
Example #9
0
        public async Task <PlayTTSAndCollectAction> PlayTTSAndCollectAsync(CallMedia.TTSParams tts, CallCollect collect)
        {
            CallMedia media = new CallMedia()
            {
                Type = CallMedia.MediaType.tts, Parameters = JObject.FromObject(tts)
            };
            PlayMediaAndCollectAction playMediaAndCollectAction = await PlayMediaAndCollectAsync(new List <CallMedia> {
                media
            }, collect);

            return(new PlayTTSAndCollectAction(playMediaAndCollectAction));
        }
Example #10
0
 public PlayTTSAndCollectAction PlayTTSAndCollect(CallMedia.TTSParams tts, CallCollect collect)
 {
     return(PlayTTSAndCollectAsync(tts, collect).Result);
 }
Example #11
0
        public async Task <PlayAudioAndCollectAction> PlayAudioAndCollectAsync(CallMedia.AudioParams audio, CallCollect collect)
        {
            CallMedia media = new CallMedia()
            {
                Type = CallMedia.MediaType.audio, Parameters = JObject.FromObject(audio)
            };
            PlayMediaAndCollectAction playMediaAndCollectAction = await PlayMediaAndCollectAsync(new List <CallMedia> {
                media
            }, collect);

            return(new PlayAudioAndCollectAction(playMediaAndCollectAction));
        }
Example #12
0
 public PlayAudioAndCollectAction PlayAudioAndCollect(CallMedia.AudioParams audio, CallCollect collect)
 {
     return(PlayAudioAndCollectAsync(audio, collect).Result);
 }
Example #13
0
        public async Task <PlayMediaAndCollectAction> PlayMediaAndCollectAsync(List <CallMedia> media, CallCollect collect)
        {
            // Create the action first so it can hook events and not potentially miss anything
            PlayMediaAndCollectAction action = new PlayMediaAndCollectAction(this, Guid.NewGuid().ToString(), media, collect);

            Task <CallPlayAndCollectResult> taskCallPlayAndCollectResult = mAPI.LL_CallPlayAndCollectAsync(new CallPlayAndCollectParams()
            {
                NodeID    = mNodeID,
                CallID    = mCallID,
                ControlID = action.ControlID,
                Play      = media,
                Collect   = collect,
            });

            // The use of await ensures that exceptions are rethrown, or OperationCancelledException is thrown
            CallPlayAndCollectResult callPlayAndCollectResult = await taskCallPlayAndCollectResult;

            // If there was an internal error of any kind then throw an exception
            mAPI.ThrowIfError(callPlayAndCollectResult.Code, callPlayAndCollectResult.Message);

            return(action);
        }
Example #14
0
 public PlayMediaAndCollectAction PlayMediaAndCollect(List <CallMedia> media, CallCollect collect)
 {
     return(PlayMediaAndCollectAsync(media, collect).Result);
 }