/// <summary>
        /// Starts an <see cref="AudioVideoIVRJob"/>.
        /// </summary>
        /// <param name="input"><see cref="AudioVideoIVRJobInput"/> specifying the configuration for the job.</param>
        /// <returns>ID of the started job</returns>
        public HttpResponseMessage Post(AudioVideoIVRJobInput input)
        {
            ValidateInput(input);

            var jobConfig = new PlatformServiceSampleJobConfiguration
            {
                JobType = JobType.AudioVideoIVR,
                AudioVideoIVRJobInput = input
            };

            string jobId = Guid.NewGuid().ToString("N");

            try
            {
                var job = PlatformServiceClientJobHelper.GetJob(jobId, WebApiApplication.InstanceId, WebApiApplication.AzureApplication, jobConfig) as AudioVideoIVRJob;

                if (job == null)
                {
                    return(CreateHttpResponse(HttpStatusCode.BadRequest, "{\"Error\":\"Invalid job input or job type\"}"));
                }

                job.ExecuteAsync().Observe <Exception>();

                return(Request.CreateResponse(HttpStatusCode.Created, job));
            }
            catch (Exception e)
            {
                Logger.Instance.Error(e, "Exception while starting the job.");
                return(CreateHttpResponse(HttpStatusCode.InternalServerError, "{\"Error\":\"Unable to start the job\"}"));
            }
        }
Example #2
0
        private async Task HandleToneEventAsync(IAudioVideoFlow flow, ToneReceivedEventArgs e)
        {
            string tone = ToneValueToString(e.Tone);

            if (tone == null)
            {
                Logger.Instance.Warning("[AudioVideoIVRJob] Tone could not be identified : {0}.", e.Tone.ToString());
                return;
            }

            Logger.Instance.Information("[AudioVideoIVRJob] ToneReceivedEvent received : {0}.", tone);

            if (currentMenu?.KeyMap == null || !currentMenu.KeyMap.ContainsKey(tone))
            {
                Logger.Instance.Information("[AudioVideoIVRJob] No action defined for this tone.");
                return;
            }

            var keyAction = currentMenu.KeyMap[tone];

            if (keyAction.Action == AudioVideoIVRActions.PlayPrompt)
            {
                Logger.Instance.Information("[AudioVideoIVRJob] Playing prompt.");
                currentMenu = keyAction;
                await PlayPromptAsync(flow).ConfigureAwait(false);
            }
            else if (keyAction.Action == AudioVideoIVRActions.RepeatPrompt)
            {
                Logger.Instance.Information("[AudioVideoIVRJob] Repeating prompt.");
                await PlayPromptAsync(flow).ConfigureAwait(false);
            }
            else if (keyAction.Action == AudioVideoIVRActions.GoToPreviousPrompt)
            {
                Logger.Instance.Information("[AudioVideoIVRJob] Going to previous prompt.");
                currentMenu = currentMenu.ParentInput ?? currentMenu;
                await PlayPromptAsync(flow).ConfigureAwait(false);
            }
            else if (keyAction.Action == AudioVideoIVRActions.TransferToUser)
            {
                Logger.Instance.Information("[AudioVideoIVRJob] Transferring the call to {0}.", keyAction.User);
                currentMenu = keyAction;
                await PlayPromptAsync(flow).ConfigureAwait(false);

                var audioVideoCall = flow.Parent as IAudioVideoCall;

                await audioVideoCall.TransferAsync(keyAction.User, null, loggingContext).ConfigureAwait(false);

                CleanupEventHandlers(flow);
            }
            else if (keyAction.Action == AudioVideoIVRActions.TerminateCall)
            {
                Logger.Instance.Information("[AudioVideoIVRJob] Terminating the call.");
                var audioVideoCall = flow.Parent as IAudioVideoCall;

                await audioVideoCall.TerminateAsync(loggingContext).ConfigureAwait(false);

                CleanupEventHandlers(flow);
            }
        }
        /// <summary>
        /// Checks if an <see cref="AudioVideoIVRJobInput"/> is valid or not.
        /// </summary>
        /// <param name="input"><see cref="AudioVideoIVRJobInput"/> to be validated.</param>
        /// <exception cref="HttpRequestValidationException">If <paramref name="input"/> is invalid</exception>
        /// <remarks>It also sets <see cref="AudioVideoIVRJobInput.ParentInput"/> references.</remarks>
        private void ValidateInput(AudioVideoIVRJobInput input)
        {
            if (input == null)
            {
                throw new HttpRequestValidationException("Invalid AudioVideoIVRJobInput");
            }

            if (input.Action == AudioVideoIVRActions.TerminateCall ||
                input.Action == AudioVideoIVRActions.GoToPreviousPrompt ||
                input.Action == AudioVideoIVRActions.RepeatPrompt)
            {
                // No more validation required.
                return;
            }

            string validationError;

            if (input.Action == AudioVideoIVRActions.TransferToUser)
            {
                if (!UriHelper.IsSipUri(input.User))
                {
                    throw new HttpRequestValidationException("Invalid User " + input.User);
                }

                // We require a transfer prompt.
                if (!IsPromptValid(input.Prompt, out validationError))
                {
                    throw new HttpRequestValidationException("Prompt specified for transfer to user " + input.User + " is invalid : " + validationError);
                }
            }

            if (input.Action == AudioVideoIVRActions.PlayPrompt)
            {
                if (!IsPromptValid(input.Prompt, out validationError))
                {
                    throw new HttpRequestValidationException(validationError);
                }

                if (input.KeyMap == null || input.KeyMap.Count == 0)
                {
                    throw new HttpRequestValidationException("No KeyMap specified for PlayPrompt.");
                }

                foreach (var keyInfo in input.KeyMap)
                {
                    if (!IsValidToneKey(keyInfo.Key))
                    {
                        throw new HttpRequestValidationException(keyInfo.Key + " is not a valid tone key");
                    }

                    ValidateInput(keyInfo.Value);
                    keyInfo.Value.ParentInput = input;
                }
            }
        }
Example #4
0
 /// <summary>
 /// Constructs instances of <see cref="AudioVideoIVRJob"/>.
 /// </summary>
 /// <param name="jobId">Unique ID of this job, it distinguishes the job from other jobs.</param>
 /// <param name="instanceId">ID of the service/process/webapp hosting all the jobs.</param>
 /// <param name="input"><see cref="AudioVideoIVRJobInput"/> providing configuration for the job.</param>
 public AudioVideoIVRJob(string jobId, string instanceId, AzureBasedApplicationBase azureApplication, AudioVideoIVRJobInput input)
     : base(jobId, instanceId, azureApplication, input, JobType.AudioVideoIVR)
 {
 }
Example #5
0
 /// <summary>
 /// Create instance of <see cref="AudioVideoIVRPromptHandler"/>.
 /// </summary>
 /// <param name="input"><see cref="AudioVideoIVRJobInput"/> providing configuration for the job</param>
 /// <param name="loggingContext"><see cref="LoggingContext"/> to be used for all log statements</param>
 internal AudioVideoIVRPromptHandler(AudioVideoIVRJobInput input, AzureBasedApplicationBase azureApplication, LoggingContext loggingContext)
 {
     currentMenu           = input;
     this.azureApplication = azureApplication;
     this.loggingContext   = loggingContext;
 }