Esempio n. 1
0
        /// <summary>
        /// Handle message extension action fetch task received by the bot.
        /// </summary>
        /// <param name="turnContext">turn context.</param>
        /// <param name="action">action.</param>
        /// <param name="cancellationToken">cancellation token.</param>
        /// <returns>MessagingExtensionActionResponse.</returns>
        protected override Task <MessagingExtensionActionResponse> OnTeamsMessagingExtensionFetchTaskAsync(ITurnContext <IInvokeActivity> turnContext, MessagingExtensionAction action, CancellationToken cancellationToken)
        {
            try
            {
                var adaptiveCardEditor = CrowdsourcerCards.AddQuestionActionCard(isValid: true);

                return(Task.FromResult(new MessagingExtensionActionResponse
                {
                    Task = new TaskModuleContinueResponse
                    {
                        Value = new TaskModuleTaskInfo
                        {
                            Card = adaptiveCardEditor,
                            Height = "medium",
                            Width = "medium",
                            Title = CrowdSourcerResource.TaskModuleTitle,
                        },
                    },
                }));
            }
            catch (Exception ex)
            {
                this.telemetryClient.TrackException(ex);
            }

            return(default);
Esempio n. 2
0
        /// <summary>
        /// Handle member added activity in teams.
        /// </summary>
        /// <param name="membersAdded">member details.</param>
        /// <param name="turnContext">turn context.</param>
        /// <param name="cancellationToken">cancellation token.</param>
        /// <returns>task.</returns>
        protected override async Task OnMembersAddedAsync(IList <ChannelAccount> membersAdded, ITurnContext <IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
        {
            try
            {
                var activity = turnContext.Activity;
                this.telemetryClient.TrackTrace($"conversationType: {activity.Conversation.ConversationType}, membersAdded: {activity.MembersAdded?.Count}, membersRemoved: {activity.MembersRemoved?.Count()}");
                bool isKbMappingCreated = default;
                if (activity.Conversation.ConversationType.Equals("channel"))
                {
                    if (membersAdded.Any(m => m.Id == activity.Recipient.Id))
                    {
                        // Bot was added to a team
                        this.telemetryClient.TrackTrace($"Bot added to team {activity.Conversation.Id}");

                        var teamDetails = ((JObject)turnContext.Activity.ChannelData).ToObject <TeamsChannelData>();

                        // get endpoint key
                        string endpointKey = await this.qnaServiceProvider.GetQnaMakerEndpointKeysAsync();

                        // check if kb mapping exists for any team.
                        var kbMappings = await this.qnaServiceProvider.GetAllKbMappingsAsync();

                        if (kbMappings == null || kbMappings?.Count == 0)
                        {
                            // only create kb if not exists for any team.
                            string kbId = await this.qnaServiceProvider.CreateKnowledgeBaseAsync();

                            if (!string.IsNullOrEmpty(kbId))
                            {
                                this.telemetryClient.TrackTrace($"kb created : {kbId}");
                                isKbMappingCreated = await this.qnaServiceProvider.CreateKbMappingAsync(teamDetails.Team.Id, kbId, endpointKey);

                                if (!isKbMappingCreated)
                                {
                                    this.telemetryClient.TrackTrace($"kb mapping not created team id: {teamDetails.Team.Id} kbid: {kbId}");
                                    await turnContext.SendActivityAsync(MessageFactory.Text(CrowdSourcerResource.ErrorMsgText));
                                }
                                else
                                {
                                    this.telemetryClient.TrackTrace($"kb mapping created team id: {teamDetails.Team.Id} kbid: {kbId}");
                                    await this.qnaServiceProvider.PublishQnaAsync(kbId);
                                }
                            }
                        }
                        else
                        {
                            // if kb mapping not exists => create mapping
                            if (!kbMappings.Any(m => m.RowKey.Contains(teamDetails.Team.Id) && !string.IsNullOrEmpty(m.KbId)))
                            {
                                isKbMappingCreated = await this.qnaServiceProvider.CreateKbMappingAsync(teamDetails.Team.Id, kbMappings?.FirstOrDefault().KbId, endpointKey);

                                if (!isKbMappingCreated)
                                {
                                    await turnContext.SendActivityAsync(MessageFactory.Text(CrowdSourcerResource.ErrorMsgText));
                                }
                                else
                                {
                                    this.telemetryClient.TrackTrace($"kb mapping created team id: {teamDetails.Team.Id} kbid: {kbMappings?.FirstOrDefault().KbId}");
                                }
                            }
                        }

                        await turnContext.SendActivityAsync(MessageFactory.Attachment(CrowdsourcerCards.WelcomeCard()));
                    }
                }
            }
            catch (Exception ex)
            {
                this.telemetryClient.TrackException(ex);
            }
        }