예제 #1
0
 public async Task <TaskModuleContinueResponse> OnTeamsTaskModuleFetchAsync(ITurnContext context, CancellationToken cancellationToken)
 {
     return(new TaskModuleContinueResponse()
     {
         Value = new TaskModuleTaskInfo()
         {
             Title = "Subscription",
             Height = "medium",
             Width = 500,
             Card = new Attachment
             {
                 ContentType = AdaptiveCard.ContentType,
                 Content = SubscriptionTaskModuleAdaptiveCard.GetSubscriptionAdaptiveCard(_settings.MicrosoftAppId)
             }
         }
     });
 }
예제 #2
0
        public async Task <TaskModuleContinueResponse> OnTeamsTaskModuleSubmitAsync(ITurnContext context, CancellationToken cancellationToken)
        {
            var state = await _stateAccessor.GetAsync(context, () => new SkillState());

            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 filterUrgency
                var filterUrgency = dataObject.GetValue("UrgencyFilter");

                // Get filterName
                var filterName = dataObject.GetValue("FilterName");

                //// Get NotificationNamespace name
                var notificationNameSpace = dataObject.GetValue("NotificationNameSpace");

                //// Get filterName
                var postNotificationAPIName = dataObject.GetValue("PostNotificationAPIName");

                // Check if this BusinessRule is already created if not create proactivesubscription
                var isNewSubscription = await _subscriptionManager.AddSubscription(context, filterName.Value <string>(), context.Activity.GetConversationReference(), cancellationToken);

                // Create Managemenet object
                if (isNewSubscription)
                {
                    var management = _serviceManager.CreateManagementForSubscription(_settings, state.AccessTokenResponse, state.ServiceCache);

                    // Create Subscription New RESTAPI for callback from ServiceNow
                    var response = await management.CreateNewRestMessage(notificationNameSpace.Value <string>(), postNotificationAPIName.Value <string>());

                    // Create Subscription BusinessRule
                    var result = await management.CreateSubscriptionBusinessRule(filterUrgency.Value <string>(), filterName.Value <string>(), notificationNameSpace.Value <string>(), postNotificationAPIName.Value <string>());

                    if (result == System.Net.HttpStatusCode.OK)
                    {
                        var serviceNowSub = new ServiceNowSubscription
                        {
                            FilterName            = filterName.Value <string>(),
                            FilterCondition       = "UrgencyChanges, DescriptionChanges, PriorityChanges, AssignedToChanges",
                            NotificationNameSpace = notificationNameSpace.Value <string>(),
                            NotificationApiName   = postNotificationAPIName.Value <string>(),
                        };
                        ActivityReferenceMap activityReferenceMap = await _activityReferenceMapAccessor.GetAsync(
                            context,
                            () => new ActivityReferenceMap(),
                            cancellationToken)
                                                                    .ConfigureAwait(false);

                        // 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,
                            SubscriptionTaskModuleAdaptiveCard.BuildSubscriptionCard(serviceNowSub, _settings.MicrosoftAppId),
                            cancellationToken);
                    }
                }

                // Create ProactiveModel
                // Get Conversation from Activity
                var conversation = context.Activity.GetConversationReference();

                // ProactiveConversationReferenceMap to save list of conversation
                var proactiveConversationReferenceMap = await _proactiveStateConversationReferenceMapAccessor.GetAsync(
                    context,
                    () => new ConversationReferenceMap(),
                    cancellationToken)
                                                        .ConfigureAwait(false);

                // Get Conversations from map
                proactiveConversationReferenceMap.TryGetValue(filterName.Value <string>(), out var listOfConversationReferences);

                if (listOfConversationReferences == null)
                {
                    proactiveConversationReferenceMap[filterName.Value <string>()] = new List <ConversationReference> {
                        conversation
                    };
                }
                else if (!listOfConversationReferences.Contains(conversation))
                {
                    listOfConversationReferences.Add(conversation);
                    proactiveConversationReferenceMap[filterName.Value <string>()] = listOfConversationReferences;
                }

                // Save ActivityReference and ProactiveState Accessor
                await _proactiveStateConversationReferenceMapAccessor.SetAsync(context, proactiveConversationReferenceMap).ConfigureAwait(false);

                // Save Conversation State
                await _proactiveState.SaveChangesAsync(context, false, cancellationToken);

                await _conversationState.SaveChangesAsync(context, false, cancellationToken);

                return(new TaskModuleContinueResponse()
                {
                    Value = new TaskModuleTaskInfo()
                    {
                        Title = "Subscription",
                        Height = "medium",
                        Width = 500,
                        Card = new Attachment
                        {
                            ContentType = AdaptiveCard.ContentType,
                            Content = SubscriptionTaskModuleAdaptiveCard.SubscriptionResponseCard($"Created Subscription With Business RuleName: {filterName} in ServiceNow")
                        }
                    }
                });
            }

            return(new TaskModuleContinueResponse()
            {
                Value = new TaskModuleTaskInfo()
                {
                    Title = "Subscription",
                    Height = "medium",
                    Width = 500,
                    Card = new Attachment
                    {
                        ContentType = AdaptiveCard.ContentType,
                        Content = SubscriptionTaskModuleAdaptiveCard.SubscriptionResponseCard("Failed To Create Subscription")
                    }
                }
            });
        }