Ejemplo n.º 1
0
        // Determine if an interruption has occurred before we dispatch to any active dialog.
        private async Task <bool> IsTurnInterruptedAsync(DialogContext dc, string topIntent, ITurnContext turnContext, CancellationToken cancellationToken)
        {
            var luisResults = await _services.LuisServices[LuisConfiguration].RecognizeAsync(dc.Context, cancellationToken);
            var activity    = turnContext.Activity;

            // See if there are any conversation interrupts we need to handle.
            if (topIntent.Equals(CancelIntent))
            {
                if (dc.ActiveDialog != null)
                {
                    await dc.CancelAllDialogsAsync();

                    await dc.Context.SendActivityAsync("Ok. I've canceled our last activity.");
                }
                else
                {
                    await dc.Context.SendActivityAsync("I don't have anything to cancel.");
                }

                return(true);        // Handled the interrupt.
            }

            // Weather Update
            if (topIntent.Equals(GetWeather))
            {
                var reply = turnContext.Activity.CreateReply();

                // Create an attachment.
                var attachment = new Attachment
                {
                    ContentUrl  = "https://imagejournal.org/wp-content/uploads/bb-plugin/cache/23466317216_b99485ba14_o-panorama.jpg",
                    ContentType = "image/jpg",
                };

                // Add the attachment to our reply.
                reply.Attachments = new List <Attachment>()
                {
                    attachment
                };

                // Send the activity to the user.
                await turnContext.SendActivityAsync(reply);

                // await dc.Context.SendActivityAsync("Getting weather info..");
                if (dc.ActiveDialog != null)
                {
                    await dc.RepromptDialogAsync();
                }

                return(true);
            }

            // EmergencyNo
            if (topIntent.Equals(EmergencyNumber))
            {
                if (turnContext.Activity.Text.Contains("police"))
                {
                    await dc.Context.SendActivityAsync("Police Helpline No: 100");
                }
                else if (turnContext.Activity.Text.Contains("fire"))
                {
                    await dc.Context.SendActivityAsync("Fire Helpline No: 101");
                }
                else if (turnContext.Activity.Text.Contains("women"))
                {
                    await dc.Context.SendActivityAsync("Women Helpline No: 1091");
                }
                else
                {
                    var card     = attachment.HelplineCard();
                    var response = CreateResponse(activity, card);
                    await dc.Context.SendActivityAsync(response);
                }

                if (dc.ActiveDialog != null)
                {
                    await dc.RepromptDialogAsync();
                }

                return(true);
            }

            // Help Intent
            if (topIntent.Equals(HelpIntent))
            {
                await dc.Context.SendActivityAsync("Let me try to provide some help.");

                await dc.Context.SendActivityAsync("I understand greetings, being asked for help, or being asked to cancel what I am doing.");

                if (dc.ActiveDialog != null)
                {
                    await dc.RepromptDialogAsync();
                }

                return(true);        // Handled the interrupt.
            }

            // Get All Employees
            if (topIntent.Equals(GetAllEmployee))
            {
                string empName  = turnContext.Activity.Text;
                var    employee = emp.GetEmployeeListByName(empName);

                if (employee.Count > 1)
                {
                    var reply = turnContext.Activity.CreateReply("Which employee do you want to know?");

                    reply.SuggestedActions         = new SuggestedActions();
                    reply.SuggestedActions.Actions = new List <CardAction>();
                    foreach (EmployeeModel model in employee)
                    {
                        reply.SuggestedActions.Actions.Add
                        (
                            new CardAction()
                        {
                            Title = model.Name, Type = ActionTypes.ImBack, Value = model.Name
                        }
                        );
                    }

                    await turnContext.SendActivityAsync(reply, cancellationToken);
                }
                else
                {
                    var entityFound = entity.GetAllEmployeeEntities(luisResults);
                    foreach (KeyValuePair <int, string> data in entityFound)
                    {
                        if (data.Key == 1)
                        {
                            await dc.Context.SendActivityAsync(data.Value);
                        }
                        else if (data.Key == 2)
                        {
                            var card     = attachment.GetEmployeeDetailsByName(data.Value);
                            var response = CreateResponse(activity, card);
                            await dc.Context.SendActivityAsync(response);
                        }
                        else if (data.Key == 3)
                        {
                            int id         = int.Parse(data.Value);
                            int employeeId = emp.GetEmployeeId(id);
                            if (employeeId != 0)
                            {
                                var card     = attachment.GetEmployeeDetailsById(id);
                                var response = CreateResponse(activity, card);
                                await dc.Context.SendActivityAsync(response);
                            }
                            else
                            {
                                await dc.Context.SendActivityAsync("Please enter valid Employee Id");
                            }
                        }
                        else if (data.Key == 4)
                        {
                            var card     = attachment.GetMaleEmployees();
                            var response = CreateResponse(activity, card);
                            await dc.Context.SendActivityAsync(response);
                        }
                        else if (data.Key == 5)
                        {
                            var card     = attachment.GetFemaleEmployees();
                            var response = CreateResponse(activity, card);
                            await dc.Context.SendActivityAsync(response);
                        }
                        else if (data.Key == 6)
                        {
                            var card     = attachment.AllEmployeesCard();
                            var response = CreateResponse(activity, card);
                            await dc.Context.SendActivityAsync(response);
                        }
                    }
                }

                if (dc.ActiveDialog != null)
                {
                    await dc.RepromptDialogAsync();
                }

                return(true);        // Handled the interrupt.
            }

            // Upcoming Birthday, Anniversaries
            if (topIntent.Equals(EmployeeUpcomingEvents))
            {
                if (turnContext.Activity.Text.ToLower() == "upcoming")
                {
                    var reply = turnContext.Activity.CreateReply("Which upcoming event do you want to know?");

                    reply.SuggestedActions = new SuggestedActions()
                    {
                        Actions = new List <CardAction>()
                        {
                            new CardAction()
                            {
                                Title = "Upcoming Anniversary", Type = ActionTypes.ImBack, Value = "Upcoming Anniversary"
                            },
                            new CardAction()
                            {
                                Title = "Upcoming Birthday", Type = ActionTypes.ImBack, Value = "Upcoming Birthday"
                            },
                        },
                    };
                    await turnContext.SendActivityAsync(reply, cancellationToken);
                }
                else
                {
                    var entityFound = entity.EmployeeUpcomingEventsEntities(luisResults);

                    if (entityFound.ToString().Equals("birthday"))
                    {
                        var card     = attachment.GetUpcomingBirthday();
                        var response = CreateResponse(activity, card);
                        await dc.Context.SendActivityAsync(response);
                    }
                    else if (entityFound.ToString().Equals("anniversary"))
                    {
                        var card     = attachment.GetUpcomingAnniversaries();
                        var response = CreateResponse(activity, card);
                        await dc.Context.SendActivityAsync(response);
                    }
                }

                if (dc.ActiveDialog != null)
                {
                    await dc.RepromptDialogAsync();
                }

                return(true);        // Handled the interrupt.
            }

            // Age, DOB
            if (topIntent.Equals(EmployeeBirthDay))
            {
                var entityFound = entity.EmployeeBirthDayEntities(luisResults);

                if (entityFound.ToString() != string.Empty)
                {
                    await turnContext.SendActivityAsync(entityFound);
                }

                if (dc.ActiveDialog != null)
                {
                    await dc.RepromptDialogAsync();
                }

                return(true);        // Handled the interrupt.
            }

            // All Projects, Specific Project
            if (topIntent.Equals(EmployeeProject))
            {
                var entityFound = entity.EmployeeProjectEntities(luisResults);

                if (entityFound != null)
                {
                    foreach (KeyValuePair <int, string> project in entityFound)
                    {
                        if (project.Key == 1)
                        {
                            string employeeName = project.Value;
                            var    employee     = emp.GetEmployeeListByName(employeeName);

                            if (employee.Count > 1)
                            {
                                var reply = turnContext.Activity.CreateReply("Which employee do you want to know?");

                                reply.SuggestedActions         = new SuggestedActions();
                                reply.SuggestedActions.Actions = new List <CardAction>();
                                foreach (EmployeeModel model in employee)
                                {
                                    reply.SuggestedActions.Actions.Add
                                    (
                                        new CardAction()
                                    {
                                        Title = model.Name, Type = ActionTypes.ImBack, Value = "projects of " + model.Name
                                    }
                                    );
                                }

                                await turnContext.SendActivityAsync(reply, cancellationToken);
                            }
                            else
                            {
                                string projectName = emp.GetProjectByName(project.Value);
                                await turnContext.SendActivityAsync($"{projectName}\n");
                            }
                        }
                        else if (project.Key == 2)
                        {
                            await turnContext.SendActivityAsync($"Total Number of Projects are: {project.Value}\n");
                        }
                    }
                }
                else
                {
                    var card     = attachment.GetProject();
                    var response = CreateResponse(activity, card);
                    await dc.Context.SendActivityAsync(response);
                }

                if (dc.ActiveDialog != null)
                {
                    await dc.RepromptDialogAsync();
                }

                return(true);        // Handled the interrupt.
            }

            // Designation, Last company, Email, Detail Info, Contact No
            if (topIntent.Equals(EmployeeDetail))
            {
                var entityFound = entity.EmployeeDetailEntities(luisResults);

                if (entityFound.ToString() != string.Empty)
                {
                    await turnContext.SendActivityAsync($"{entityFound}\n");
                }

                if (dc.ActiveDialog != null)
                {
                    await dc.RepromptDialogAsync();
                }

                return(true);        // Handled the interrupt.
            }

            // Anniversaries
            if (topIntent.Equals(EmployeeAnniversaries))
            {
                var entityFound = entity.EmployeeAnniversariesEntities(luisResults);

                if (entityFound.ToString() != string.Empty)
                {
                    if (entityFound.ToString().Equals("anniversary"))
                    {
                        var card     = attachment.GetEmployeeAnniversaries();
                        var response = CreateResponse(activity, card);
                        await dc.Context.SendActivityAsync(response);
                    }
                    else if (entityFound.ToString() != "anniversary")
                    {
                        await turnContext.SendActivityAsync($"{entityFound}\n");
                    }
                }

                if (dc.ActiveDialog != null)
                {
                    await dc.RepromptDialogAsync();
                }

                return(true);        // Handled the interrupt.
            }

            // Full Address, Search by City
            if (topIntent.Equals(EmployeeAddress))
            {
                var entityFound = entity.EmployeeAddressEntities(luisResults);

                foreach (KeyValuePair <int, string> employee in entityFound)
                {
                    if (employee.Key == 1)
                    {
                        await dc.Context.SendActivityAsync($"{employee.Value}");
                    }
                    else if (employee.Key == 2)
                    {
                        var card     = attachment.GetEmployeeByCity(employee.Value);
                        var response = CreateResponse(activity, card);
                        await dc.Context.SendActivityAsync(response);
                    }
                    else
                    {
                        await dc.Context.SendActivityAsync("No entity found");
                    }
                }

                if (dc.ActiveDialog != null)
                {
                    await dc.RepromptDialogAsync();
                }

                return(true);        // Handled the interrupt.
            }

            // DOJ, Experience Per Employee
            if (topIntent.Equals(EmployeeExperience))
            {
                Dictionary <int, string> record = entity.EmployeeExperienceEntities(luisResults);
                foreach (KeyValuePair <int, string> data in record)
                {
                    if (data.Value != null)
                    {
                        if (data.Key == 1)
                        {
                            string employeeName = data.Value;
                            var    employee     = emp.GetEmployeeListByName(employeeName);

                            if (employee.Count > 1)
                            {
                                var reply = turnContext.Activity.CreateReply("Which employee do you want to know?");

                                reply.SuggestedActions         = new SuggestedActions();
                                reply.SuggestedActions.Actions = new List <CardAction>();
                                foreach (EmployeeModel model in employee)
                                {
                                    reply.SuggestedActions.Actions.Add
                                    (
                                        new CardAction()
                                    {
                                        Title = model.Name, Type = ActionTypes.ImBack, Value = "experience of " + model.Name
                                    }
                                    );
                                }

                                await turnContext.SendActivityAsync(reply, cancellationToken);
                            }
                            else
                            {
                                float exp = emp.GetEmployeeExperience(data.Value);
                                if (exp == 0)
                                {
                                    await turnContext.SendActivityAsync(data.Value + " is a Trainee");
                                }
                                else
                                {
                                    await turnContext.SendActivityAsync($"{exp}" + " years");
                                }
                            }
                        }
                        else if (data.Key == 2)
                        {
                            var card     = attachment.GetEmployeeExperience();
                            var response = CreateResponse(activity, card);
                            await dc.Context.SendActivityAsync(response);
                        }
                        else if (data.Key == 3)
                        {
                            string empName  = data.Value;
                            var    employee = emp.GetEmployeeListByName(empName);

                            if (employee.Count > 1)
                            {
                                var reply = turnContext.Activity.CreateReply("Which employee do you want to know?");

                                reply.SuggestedActions         = new SuggestedActions();
                                reply.SuggestedActions.Actions = new List <CardAction>();
                                foreach (EmployeeModel model in employee)
                                {
                                    reply.SuggestedActions.Actions.Add
                                    (
                                        new CardAction()
                                    {
                                        Title = model.Name, Type = ActionTypes.ImBack, Value = "doj of " + model.Name
                                    }
                                    );
                                }

                                await turnContext.SendActivityAsync(reply, cancellationToken);
                            }
                            else
                            {
                                DateTime doj  = emp.GetEmployeeDOJ(data.Value);
                                string   date = doj.ToString("MM/dd/yyyy");
                                await turnContext.SendActivityAsync($"{date}");
                            }
                        }
                        else if (data.Key == 4)
                        {
                            float experience = float.Parse(data.Value);
                            var   card       = attachment.GetEmployeeMax(experience);
                            var   response   = CreateResponse(activity, card);
                            await dc.Context.SendActivityAsync(response);
                        }
                        else if (data.Key == 5)
                        {
                            float experience = float.Parse(data.Value);
                            var   card       = attachment.GetEmployeeMin(experience);
                            var   response   = CreateResponse(activity, card);
                            await dc.Context.SendActivityAsync(response);
                        }
                    }
                }

                if (dc.ActiveDialog != null)
                {
                    await dc.RepromptDialogAsync();
                }

                return(true);        // Handled the interrupt.
            }

            // Get Trainee
            if (topIntent.Equals(GetTrainee))
            {
                var entityFound = entity.GetAllTraineeEntities(luisResults);

                if (entityFound.ToString() != "trainee")
                {
                    await dc.Context.SendActivityAsync($"{entityFound}");
                }
                else if (entityFound.ToString().Equals("trainee"))
                {
                    var card     = attachment.GetTrainees();
                    var response = CreateResponse(activity, card);
                    await dc.Context.SendActivityAsync(response);
                }

                if (dc.ActiveDialog != null)
                {
                    await dc.RepromptDialogAsync();
                }

                return(true);        // Handled the interrupt.
            }

            return(false);           // Did not handle the interrupt.
        }