/// <summary> /// Invoked when the user submits a response/suggests a response/updates a response. /// </summary> /// <param name="turnContext">Context object containing information cached for a single turn of conversation with a user.</param> /// <param name="action">Messaging extension action commands.</param> /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param> /// <returns>A task that represents the work queued to execute.</returns> /// <remarks> /// Reference link: https://docs.microsoft.com/en-us/dotnet/api/microsoft.bot.builder.teams.teamsactivityhandler.onteamsmessagingextensionsubmitactionasync?view=botbuilder-dotnet-stable. /// </remarks> protected override async Task <MessagingExtensionActionResponse> OnTeamsMessagingExtensionSubmitActionAsync( ITurnContext <IInvokeActivity> turnContext, MessagingExtensionAction action, CancellationToken cancellationToken) { try { turnContext = turnContext ?? throw new ArgumentNullException(nameof(turnContext)); this.RecordEvent(nameof(this.OnTeamsMessagingExtensionSubmitActionAsync), turnContext); action = action ?? throw new ArgumentNullException(nameof(action)); var valuesfromTaskModule = JsonConvert.DeserializeObject <TaskModuleResponseDetails>(action.Data.ToString()); if (valuesfromTaskModule.Command.ToUpperInvariant() == Constants.SaveNominatedDetailsAction) { var mentionActivity = await CardHelper.GetMentionActivityAsync(valuesfromTaskModule.NominatedToPrincipalName.Split(",").Select(row => row.Trim()).ToList(), turnContext.Activity.From.AadObjectId, valuesfromTaskModule.TeamId, turnContext, this.localizer, this.logger, MentionActivityType.Nomination, cancellationToken); var notificationCard = (Activity)MessageFactory.Attachment(EndorseCard.GetEndorseCard(this.appBaseUrl, this.localizer, valuesfromTaskModule)); await this.SendMentionedCardAsync(turnContext, notificationCard, mentionActivity); this.logger.LogInformation("Nominated an award"); return(null); } return(null); } catch (Exception ex) { this.logger.LogError(ex, $"Error at OnTeamsMessagingExtensionSubmitActionAsync(): {ex.Message}", SeverityLevel.Error); await turnContext.SendActivityAsync(this.localizer.GetString("ErrorMessage")); throw; } }
/// <summary> /// Invoked when the user submits a response from messaging extension. /// </summary> /// <param name="turnContext">Context object containing information cached for a single turn of conversation with a user.</param> /// <param name="action">Messaging extension action commands.</param> /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param> /// <returns>A task that represents the work queued to execute.</returns> /// <remarks> /// Reference link: https://docs.microsoft.com/en-us/dotnet/api/microsoft.bot.builder.teams.teamsactivityhandler.onteamsmessagingextensionsubmitactionasync?view=botbuilder-dotnet-stable. /// </remarks> protected override async Task <MessagingExtensionActionResponse> OnTeamsMessagingExtensionSubmitActionAsync( ITurnContext <IInvokeActivity> turnContext, MessagingExtensionAction action, CancellationToken cancellationToken) { turnContext = turnContext ?? throw new ArgumentNullException(nameof(turnContext)); action = action ?? throw new ArgumentNullException(nameof(action)); this.RecordEvent(nameof(this.OnTeamsMessagingExtensionSubmitActionAsync), turnContext); var valuesFromTaskModule = JsonConvert.DeserializeObject <TaskModuleResponseDetails>(action.Data.ToString()); if (valuesFromTaskModule.Command.ToUpperInvariant() == Constants.SaveNominatedDetailsAction) { var mentionActivity = await CardHelper.GetMentionActivityAsync( valuesFromTaskModule.NomineeUserPrincipalNames.Split(",").Select(row => row.Trim()).ToList(), turnContext.Activity.From.AadObjectId, valuesFromTaskModule.TeamId, turnContext, this.localizer, this.logger, MentionActivityType.Nomination, cancellationToken); var notificationCard = EndorseCard.GetEndorseCard(this.options.Value.AppBaseUri, this.localizer, valuesFromTaskModule); await this.SendCardAndMentionsAsync(turnContext, notificationCard, mentionActivity); this.logger.LogInformation("Award nomination card sent successfully."); return(null); } this.logger.LogWarning($"Unsupported bot command: {valuesFromTaskModule.Command}"); return(null); }
/// <summary> /// When OnTurn method receives a submit invoke activity on bot turn, it calls this method. /// </summary> /// <param name="turnContext">Context object containing information cached for a single turn of conversation with a user.</param> /// <param name="taskModuleRequest">Task module invoke request value payload.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns>A task that represents a task module response.</returns> protected override async Task <TaskModuleResponse> OnTeamsTaskModuleSubmitAsync(ITurnContext <IInvokeActivity> turnContext, TaskModuleRequest taskModuleRequest, CancellationToken cancellationToken) { try { turnContext = turnContext ?? throw new ArgumentNullException(nameof(turnContext)); var activity = (Activity)turnContext.Activity; this.RecordEvent(nameof(this.OnTeamsTaskModuleFetchAsync), turnContext); IMessageActivity notificationCard; Activity mentionActivity; var valuesfromTaskModule = JsonConvert.DeserializeObject <TaskModuleResponseDetails>(((JObject)activity.Value).GetValue("data", StringComparison.OrdinalIgnoreCase)?.ToString()); switch (valuesfromTaskModule.Command.ToUpperInvariant()) { case Constants.SaveAdminDetailsAction: mentionActivity = await CardHelper.GetMentionActivityAsync(valuesfromTaskModule.AdminPrincipalName.Split(",").ToList(), turnContext.Activity.From.AadObjectId, valuesfromTaskModule.TeamId, turnContext, this.localizer, this.logger, MentionActivityType.SetAdmin, cancellationToken); var cardDetail = (Activity)MessageFactory.Attachment(AdminCard.GetAdminCard(this.localizer, valuesfromTaskModule, this.options.Value.ManifestId)); await this.SendMentionedCardAsync(turnContext, cardDetail, mentionActivity); this.logger.LogInformation("R&R admin has been configured"); break; case Constants.CancelCommand: break; case Constants.UpdateAdminDetailCommand: mentionActivity = await CardHelper.GetMentionActivityAsync(valuesfromTaskModule.AdminPrincipalName.Split(",").ToList(), turnContext.Activity.From.AadObjectId, valuesfromTaskModule.TeamId, turnContext, this.localizer, this.logger, MentionActivityType.SetAdmin, cancellationToken); notificationCard = MessageFactory.Attachment(AdminCard.GetAdminCard(this.localizer, valuesfromTaskModule, this.options.Value.ManifestId)); notificationCard.Id = turnContext.Activity.Conversation.Id.Split(';')[1].Split("=")[1]; notificationCard.Conversation = turnContext.Activity.Conversation; await turnContext.UpdateActivityAsync(notificationCard); await turnContext.SendActivityAsync(mentionActivity); this.logger.LogInformation("Card is updated."); break; case Constants.NominateAction: var awardsList = await this.awardsStorageProvider.GetAwardsAsync(valuesfromTaskModule.TeamId); await turnContext.SendActivityAsync(MessageFactory.Carousel(NominateCarouselCard.GetAwardsCard(this.appBaseUrl, awardsList, this.localizer, valuesfromTaskModule))); break; case Constants.SaveNominatedDetailsAction: turnContext.Activity.Conversation.Id = valuesfromTaskModule.TeamId; var result = (Activity)MessageFactory.Attachment(EndorseCard.GetEndorseCard(this.appBaseUrl, this.localizer, valuesfromTaskModule)); mentionActivity = await CardHelper.GetMentionActivityAsync(valuesfromTaskModule.NominatedToPrincipalName.Split(",").Select(row => row.Trim()).ToList(), turnContext.Activity.From.AadObjectId, valuesfromTaskModule.TeamId, turnContext, this.localizer, this.logger, MentionActivityType.Nomination, cancellationToken); await this.SendMentionedCardAsync(turnContext, result, mentionActivity); this.logger.LogInformation("Nominated an award"); break; case Constants.OkCommand: return(null); default: this.logger.LogInformation($"Invalid command for task module submit activity.Command is : {valuesfromTaskModule.Command} "); await turnContext.SendActivityAsync(this.localizer.GetString("ErrorMessage")); break; } return(null); } catch (Exception ex) { this.logger.LogError(ex, $"Error at OnTeamsTaskModuleSubmitAsync(): {ex.Message}", SeverityLevel.Error); await turnContext.SendActivityAsync(this.localizer.GetString("ErrorMessage")); throw; } }
/// <summary> /// When OnTurn method receives a submit invoke activity on bot turn, it calls this method. /// </summary> /// <param name="turnContext">Context object containing information cached for a single turn of conversation with a user.</param> /// <param name="taskModuleRequest">Task module invoke request value payload.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns>A task that represents a task module response.</returns> protected override async Task <TaskModuleResponse> OnTeamsTaskModuleSubmitAsync(ITurnContext <IInvokeActivity> turnContext, TaskModuleRequest taskModuleRequest, CancellationToken cancellationToken) { try { turnContext = turnContext ?? throw new ArgumentNullException(nameof(turnContext)); var activity = (Activity)turnContext.Activity; this.RecordEvent(nameof(this.OnTeamsTaskModuleFetchAsync), turnContext); Activity mentionActivity; var valuesFromTaskModule = JsonConvert.DeserializeObject <TaskModuleResponseDetails>(((JObject)activity.Value).GetValue("data", StringComparison.OrdinalIgnoreCase)?.ToString()); switch (valuesFromTaskModule.Command.ToUpperInvariant()) { // Command to send award admin card on save admin action. case Constants.SaveAdminDetailsAction: mentionActivity = await CardHelper.GetMentionActivityAsync( valuesFromTaskModule.AdminUserPrincipalName.Split(",").ToList(), turnContext.Activity.From.AadObjectId, valuesFromTaskModule.TeamId, turnContext, this.localizer, this.logger, MentionActivityType.SetAdmin, cancellationToken); var cardDetail = AdminCard.GetAdminCard(this.localizer, valuesFromTaskModule); await this.SendCardAndMentionsAsync(turnContext, cardDetail, mentionActivity); this.logger.LogInformation("Admin has been configured successfully."); break; // Command to update award admin card case Constants.UpdateAdminDetailCommand: mentionActivity = await CardHelper.GetMentionActivityAsync( valuesFromTaskModule.AdminUserPrincipalName.Split(",").ToList(), turnContext.Activity.From.AadObjectId, valuesFromTaskModule.TeamId, turnContext, this.localizer, this.logger, MentionActivityType.SetAdmin, cancellationToken); var notificationCard = (Activity)MessageFactory.Attachment(AdminCard.GetAdminCard(this.localizer, valuesFromTaskModule)); // Split here extracts the activity id from turn context conversation notificationCard.Id = turnContext.Activity.Conversation.Id.Split(';')[1].Split("=")[1]; notificationCard.Conversation = turnContext.Activity.Conversation; await turnContext.UpdateActivityAsync(notificationCard); await turnContext.SendActivityAsync(mentionActivity); this.logger.LogInformation("Admin card is updated successfully."); break; // Command to show list of awards ready for nomination case Constants.NominateAction: var awardsList = await this.awardsStorageProvider.GetAwardsAsync(valuesFromTaskModule.TeamId); await turnContext.SendActivityAsync(MessageFactory.Carousel(NominateCarouselCard.GetAwardNominationCards(this.options.Value.AppBaseUri, awardsList, this.localizer, valuesFromTaskModule))); this.logger.LogInformation("Nomination carousel card is sent successfully."); break; // Command to save nominated user details case Constants.SaveNominatedDetailsAction: turnContext.Activity.Conversation.Id = valuesFromTaskModule.TeamId; var endorsementCard = EndorseCard.GetEndorseCard(this.options.Value.AppBaseUri, this.localizer, valuesFromTaskModule); mentionActivity = await CardHelper.GetMentionActivityAsync( valuesFromTaskModule.NomineeUserPrincipalNames.Split(",").Select(row => row.Trim()).ToList(), turnContext.Activity.From.AadObjectId, valuesFromTaskModule.TeamId, turnContext, this.localizer, this.logger, MentionActivityType.Nomination, cancellationToken); await this.SendCardAndMentionsAsync(turnContext, endorsementCard, mentionActivity); this.logger.LogInformation("Award nomination for user sent successfully"); break; // Commands to close task modules case Constants.OkCommand: case Constants.CancelCommand: this.logger.LogInformation($"{valuesFromTaskModule.Command.ToUpperInvariant()} is called. [note] - no actions are performed."); break; default: this.logger.LogInformation($"Invalid command for task module submit activity.Command is : {valuesFromTaskModule.Command} "); await turnContext.SendActivityAsync(this.localizer.GetString("ErrorMessage")); break; } return(null); } catch (Exception ex) { this.logger.LogError(ex, $"Error at OnTeamsTaskModuleSubmitAsync(): {ex.Message}"); throw; } }