Пример #1
0
        private async Task <DialogTurnResult> StartProcessStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var processDetails = (ProcessDetails)stepContext.Options;
            var result         = stepContext.Result.ToString();
            var promptOption   = new PromptOption();

            try
            {
                promptOption = JsonConvert.DeserializeObject <PromptOption>(stepContext.Result.ToString());
            }
            catch (Exception) {}

            if (!string.IsNullOrEmpty(promptOption.Id))
            {
                if (promptOption.Id != "Confirm")
                {
                    processDetails.Action = "pastMenu";
                    return(await stepContext.ReplaceDialogAsync(nameof(MainDialog), processDetails, cancellationToken));
                }
                result = promptOption.Value;
            }

            if (result.ToLower() == "yes" || result.ToLower() == "y")
            {
                var rpaService = new RPAService();
                //save activity id for when process finish
                var activityId = stepContext.Context.Activity.Id;
                //check if the process can start
                if (processDetails.ProcessSelected.LastRun.State == "Faulted" || processDetails.ProcessSelected.LastRun.State == "Successful" || processDetails.ProcessSelected.LastRun.State == "Stopped" || string.IsNullOrEmpty(processDetails.ProcessSelected.LastRun.State))
                {
                    //check if has asset
                    var assetsWithValueFromChild = rpaService.HasAnyAsset(processDetails.ProcessSelected);
                    if (assetsWithValueFromChild.Count > 0)
                    {
                        var response = rpaService.MakeAssetFromChild(assetsWithValueFromChild);
                        if (response.Body != "Success")
                        {
                            var choices   = new List <Choice>();
                            var rpaOption = rpaService.GetMainMenuOption();
                            if (response.MissingAsset)
                            {
                                //create incident
                                var incident      = rpaService.CreateRPAIncident(processDetails.ProcessSelected);
                                var incidentValue = JsonConvert.SerializeObject(new PromptOption {
                                    Id = "incident", Value = "bam?id=rpa_request&table=u_robot_incident&sys_id=" + incident.Sys_Id
                                });
                                choices = new List <Choice>
                                {
                                    new Choice
                                    {
                                        Value  = "bam?id=rpa_request&table=u_robot_incident&sys_id=" + incident.Sys_Id,
                                        Action = new CardAction(ActionTypes.PostBack, incident.Number, null, incident.Number, "openUrl", value: incidentValue, null)
                                    }
                                };
                                choices.Add(rpaOption);

                                return(await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions
                                {
                                    Prompt = (Activity)ChoiceFactory.SuggestedAction(choices, "Process " + processDetails.ProcessSelected.Name + " requires an asset associated to your user, an incident has been opened to RPA Support." + Environment.NewLine + "Click incident number below to open it")
                                }, cancellationToken));
                            }
                            else
                            {
                                var assetValue = JsonConvert.SerializeObject(new PromptOption {
                                    Id = "asset", Value = "bam?id=rpa_process_assets&process=" + processDetails.ProcessSelected.Sys_id
                                });
                                choices = new List <Choice>
                                {
                                    new Choice
                                    {
                                        Value  = "bam?id=rpa_process_assets&process=" + processDetails.ProcessSelected.Sys_id,
                                        Action = new CardAction(ActionTypes.PostBack, "Update Asset", null, "Update Asset", "openUrl", value: assetValue, null)
                                    }
                                };
                                choices.Add(rpaOption);
                                //send the user to SN UI page
                                return(await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions
                                {
                                    Prompt = (Activity)ChoiceFactory.SuggestedAction(choices, response.Body + Environment.NewLine + "Please enter them before running the process by pressing Update Asset button below.")
                                }, cancellationToken));
                            }
                        }
                    }
                    if (processDetails.ProcessSelected.Releases.Any(r => r.robots.Count > 1))
                    {
                        processDetails.ProcessSelected.FirstBot = true;
                        return(await stepContext.ReplaceDialogAsync(nameof(RobotsDialog), processDetails, cancellationToken));
                    }                    //check if process need params
                    else if (processDetails.ProcessSelected.Releases.Any(r => r.parameters_required == true))
                    {
                        //set all params for this conversation to false(maybe was interrupted by a notification)
                        rpaService.DeactivatedConversationFlow(string.Empty, stepContext.Context.Activity.Conversation.Id);
                        rpaService.SaveConversationFlow(processDetails.ProcessSelected, stepContext.Context.Activity.Conversation.Id);
                        return(await stepContext.ReplaceDialogAsync(nameof(ParametersProcessDialog), processDetails, cancellationToken));
                    }
                    else
                    {
                        return(await stepContext.ReplaceDialogAsync(nameof(StartProcessSharedDialog), processDetails, cancellationToken));
                    }
                }
                else
                {
                    processDetails.Action = "error";
                    processDetails.Error  = "Cannot start " + processDetails.ProcessSelected.Name + " because the process is already running.";
                    return(await stepContext.ReplaceDialogAsync(nameof(MainDialog), processDetails, cancellationToken));
                }
            }
            else if (result.ToLower() == "no" || result.ToLower() == "n")            //when no is selected
            {
                processDetails.Action = string.Empty;
                return(await stepContext.ReplaceDialogAsync(nameof(MainDialog), null, cancellationToken));
            }
            else             //when something is typed
            {
                processDetails.Action = string.Empty;
                return(await stepContext.ReplaceDialogAsync(nameof(MainDialog), null, cancellationToken));
            }
        }