예제 #1
0
        // Handle Submit True
        public async Task <TaskModuleContinueResponse> OnTeamsTaskModuleSubmitAsync(ITurnContext context, CancellationToken cancellationToken)
        {
            var state = await _stateAccessor.GetAsync(context, () => new SkillState());

            ActivityReferenceMap activityReferenceMap = await _activityReferenceMapAccessor.GetAsync(
                context,
                () => new ActivityReferenceMap(),
                cancellationToken)
                                                        .ConfigureAwait(false);

            var activityValueObject = JObject.FromObject(context.Activity.Value);

            var     isDataObject = activityValueObject.TryGetValue("data", StringComparison.InvariantCultureIgnoreCase, out JToken dataValue);
            JObject dataObject   = null;

            if (isDataObject)
            {
                dataObject = dataValue as JObject;

                // Get Title
                var title = dataObject.GetValue("IncidentTitle");

                // Get Description
                var description = dataObject.GetValue("IncidentDescription");

                // Get Urgency
                var urgency = dataObject.GetValue("IncidentUrgency");

                // Create Managemenet object
                var management = _serviceManager.CreateManagement(_settings, state.AccessTokenResponse, state.ServiceCache);

                // Create Ticket
                var result = await management.CreateTicket(title.Value <string>(), description.Value <string>(), (UrgencyLevel)Enum.Parse(typeof(UrgencyLevel), urgency.Value <string>(), true));

                if (result.Success)
                {
                    // Return Added Incident Envelope
                    // Get saved Activity Reference mapping to conversation Id
                    activityReferenceMap.TryGetValue(context.Activity.Conversation.Id, out var activityReference);

                    // Update Create Ticket Button with another Adaptive card to Update/Delete Ticket
                    await _teamsTicketUpdateActivity.UpdateTaskModuleActivityAsync(
                        context,
                        activityReference,
                        ServiceNowIncidentTaskModuleAdaptiveCardHelper.BuildIncidentCard(result.Tickets.FirstOrDefault(), _settings.MicrosoftAppId),
                        cancellationToken);

                    return(new TaskModuleContinueResponse()
                    {
                        Type = "continue",
                        Value = new TaskModuleTaskInfo()
                        {
                            Title = "Incident Created",
                            Height = "medium",
                            Width = 500,
                            Card = new Attachment
                            {
                                ContentType = AdaptiveCard.ContentType,
                                Content = ServiceNowIncidentTaskModuleAdaptiveCardHelper.IncidentResponseCard("Incident has been created")
                            }
                        }
                    });
                }
            }

            // Failed to create incident
            return(new TaskModuleContinueResponse()
            {
                Type = "continue",
                Value = new TaskModuleTaskInfo()
                {
                    Title = "Incident Create Failed",
                    Height = "medium",
                    Width = 500,
                    Card = new Attachment
                    {
                        ContentType = AdaptiveCard.ContentType,
                        Content = ServiceNowIncidentTaskModuleAdaptiveCardHelper.IncidentResponseCard("Incident Create Failed")
                    }
                }
            });
        }
        public async Task <TaskModuleContinueResponse> OnTeamsTaskModuleSubmitAsync(ITurnContext context, CancellationToken cancellationToken)
        {
            var state = await _stateAccessor.GetAsync(context, () => new SkillState());

            var taskModuleMetadata = context.Activity.GetTaskModuleMetadata <TaskModuleMetadata>();

            var ticketDetails = taskModuleMetadata.FlowData != null?
                                JsonConvert.DeserializeObject <Dictionary <string, object> >(JsonConvert.SerializeObject(taskModuleMetadata.FlowData))
                                .GetValueOrDefault("IncidentDetails") : null;

            // Convert JObject to Ticket
            Ticket incidentDetails = JsonConvert.DeserializeObject <Ticket>(ticketDetails.ToString());

            // If ticket is valid go ahead and update
            if (incidentDetails != null)
            {
                ActivityReferenceMap activityReferenceMap = await _activityReferenceMapAccessor.GetAsync(
                    context,
                    () => new ActivityReferenceMap(),
                    cancellationToken)
                                                            .ConfigureAwait(false);

                // Get Activity Id from ActivityReferenceMap
                activityReferenceMap.TryGetValue(context.Activity.Conversation.Id, out var activityReference);

                // Get User Input from AdatptiveCard
                var activityValueObject = JObject.FromObject(context.Activity.Value);

                var     isDataObject = activityValueObject.TryGetValue("data", StringComparison.InvariantCultureIgnoreCase, out JToken dataValue);
                JObject dataObject   = null;
                if (isDataObject)
                {
                    dataObject = dataValue as JObject;

                    // Get Title
                    var title = dataObject.GetValue("IncidentTitle");

                    // Get Description
                    var description = dataObject.GetValue("IncidentDescription");

                    // Get Urgency
                    var urgency = dataObject.GetValue("IncidentUrgency");

                    // Create Managemenet object
                    var management = _serviceManager.CreateManagement(_settings, state.AccessTokenResponse, state.ServiceCache);
                    var result     = await management.UpdateTicket(incidentDetails.Id, title.Value <string>(), description.Value <string>(), (UrgencyLevel)Enum.Parse(typeof(UrgencyLevel), urgency.Value <string>()));

                    if (result.Success)
                    {
                        await _teamsTicketUpdateActivity.UpdateTaskModuleActivityAsync(
                            context,
                            activityReference,
                            ServiceNowIncidentTaskModuleAdaptiveCardHelper.BuildIncidentCard(result.Tickets.FirstOrDefault(), _settings.MicrosoftAppId),
                            cancellationToken);

                        // Return Added Incident Envelope
                        return(new TaskModuleContinueResponse()
                        {
                            Type = "continue",
                            Value = new TaskModuleTaskInfo()
                            {
                                Title = "Incident Updated",
                                Height = "small",
                                Width = 300,
                                Card = new Attachment
                                {
                                    ContentType = AdaptiveCard.ContentType,
                                    Content = ServiceNowIncidentTaskModuleAdaptiveCardHelper.IncidentResponseCard("Incident has been Updated")
                                }
                            }
                        });
                    }
                }
            }

            // Failed to update incident
            return(new TaskModuleContinueResponse()
            {
                Type = "continue",
                Value = new TaskModuleTaskInfo()
                {
                    Title = "Incident Update Failed",
                    Height = "medium",
                    Width = 500,
                    Card = new Attachment
                    {
                        ContentType = AdaptiveCard.ContentType,
                        Content = ServiceNowIncidentTaskModuleAdaptiveCardHelper.IncidentResponseCard("Incident Update Failed")
                    }
                }
            });
        }