|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using AdaptiveCards; |
| 10 | +using Microsoft.Bot.Builder; |
| 11 | +using Microsoft.Bot.Builder.Teams; |
| 12 | +using Microsoft.Bot.Schema; |
| 13 | +using Microsoft.Bot.Schema.Teams; |
| 14 | +using Microsoft.Extensions.Configuration; |
| 15 | +using Newtonsoft.Json.Linq; |
| 16 | + |
| 17 | +namespace Microsoft.BotBuilderSamples.Bots |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// A bot that handles Teams messaging extensions for third-party storage integration. |
| 21 | + /// </summary> |
| 22 | + public class TeamsMsgextThirdpartyStorageBot : TeamsActivityHandler |
| 23 | + { |
| 24 | + private readonly string baseUrl; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Initializes a new instance of the <see cref="TeamsMsgextThirdpartyStorageBot"/> class. |
| 28 | + /// </summary> |
| 29 | + /// <param name="configuration">Configuration object to access app settings.</param> |
| 30 | + public TeamsMsgextThirdpartyStorageBot(IConfiguration configuration) |
| 31 | + { |
| 32 | + this.baseUrl = configuration["BaseUrl"] ?? throw new ArgumentNullException(nameof(configuration), "BaseUrl is not configured."); |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Handles the submit action for messaging extensions. |
| 37 | + /// </summary> |
| 38 | + /// <param name="turnContext">The turn context.</param> |
| 39 | + /// <param name="action">The messaging extension action.</param> |
| 40 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 41 | + /// <returns>A task that represents the messaging extension response.</returns> |
| 42 | + protected override async Task<MessagingExtensionActionResponse> OnTeamsMessagingExtensionSubmitActionAsync( |
| 43 | + ITurnContext<IInvokeActivity> turnContext, |
| 44 | + MessagingExtensionAction action, |
| 45 | + CancellationToken cancellationToken) |
| 46 | + { |
| 47 | + try |
| 48 | + { |
| 49 | + switch (action.CommandId) |
| 50 | + { |
| 51 | + case "createWithPreview": |
| 52 | + return CreateWebViewResponse(turnContext, action); |
| 53 | + default: |
| 54 | + throw new NotSupportedException($"Command '{action.CommandId}' is not supported."); |
| 55 | + } |
| 56 | + } |
| 57 | + catch (Exception ex) |
| 58 | + { |
| 59 | + // Log the exception |
| 60 | + Console.WriteLine($"Error in OnTeamsMessagingExtensionSubmitActionAsync: {ex.Message}"); |
| 61 | + return new MessagingExtensionActionResponse |
| 62 | + { |
| 63 | + ComposeExtension = new MessagingExtensionResult |
| 64 | + { |
| 65 | + Type = "message", |
| 66 | + Text = "An error occurred while processing your request. Please try again later." |
| 67 | + } |
| 68 | + }; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Generates a messaging extension action response containing an Adaptive Card with file details. |
| 74 | + /// </summary> |
| 75 | + /// <param name="turnContext">The turn context.</param> |
| 76 | + /// <param name="action">The messaging extension action.</param> |
| 77 | + /// <returns>A MessagingExtensionActionResponse with an Adaptive Card.</returns> |
| 78 | + private MessagingExtensionActionResponse CreateWebViewResponse(ITurnContext<IInvokeActivity> turnContext, MessagingExtensionAction action) |
| 79 | + { |
| 80 | + try |
| 81 | + { |
| 82 | + // Parse the uploaded data from action.Data |
| 83 | + var uploadedFiles = JArray.Parse(action.Data.ToString()); |
| 84 | + |
| 85 | + // Dictionary for file type icons |
| 86 | + var fileTypeIcons = new Dictionary<string, string> |
| 87 | + { |
| 88 | + { "spreadsheet", $"{this.baseUrl}/icons/ExcelIcon.png" }, |
| 89 | + { "pdf", $"{this.baseUrl}/icons/PdfIcon.png" }, |
| 90 | + { "wordprocessing", $"{this.baseUrl}/icons/WordIcons.png" }, |
| 91 | + { "image", $"{this.baseUrl}/icons/ImageIcon.png" }, |
| 92 | + }; |
| 93 | + |
| 94 | + var cardBody = new List<AdaptiveElement>(); |
| 95 | + |
| 96 | + foreach (var file in uploadedFiles) |
| 97 | + { |
| 98 | + string name = file["name"]?.ToString() ?? "Unknown"; |
| 99 | + string type = file["type"]?.ToString() ?? string.Empty; |
| 100 | + string fileTypeIconUrl = fileTypeIcons.FirstOrDefault(kvp => type.Contains(kvp.Key)).Value |
| 101 | + ?? $"{this.baseUrl}/icons/DefaultIcon.png"; |
| 102 | + |
| 103 | + cardBody.Add(new AdaptiveColumnSet |
| 104 | + { |
| 105 | + Columns = new List<AdaptiveColumn> |
| 106 | + { |
| 107 | + new AdaptiveColumn |
| 108 | + { |
| 109 | + Width = "auto", |
| 110 | + Items = new List<AdaptiveElement> |
| 111 | + { |
| 112 | + new AdaptiveImage |
| 113 | + { |
| 114 | + Url = new Uri(fileTypeIconUrl), |
| 115 | + Size = AdaptiveImageSize.Small |
| 116 | + } |
| 117 | + } |
| 118 | + }, |
| 119 | + new AdaptiveColumn |
| 120 | + { |
| 121 | + Width = "stretch", |
| 122 | + Items = new List<AdaptiveElement> |
| 123 | + { |
| 124 | + new AdaptiveTextBlock |
| 125 | + { |
| 126 | + Text = name, |
| 127 | + Wrap = true |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + // Create Adaptive Card |
| 136 | + var adaptiveCard = new AdaptiveCard(new AdaptiveSchemaVersion(1, 4)) { Body = cardBody }; |
| 137 | + |
| 138 | + return new MessagingExtensionActionResponse |
| 139 | + { |
| 140 | + ComposeExtension = new MessagingExtensionResult |
| 141 | + { |
| 142 | + Type = "result", |
| 143 | + AttachmentLayout = "list", |
| 144 | + Attachments = new List<MessagingExtensionAttachment> |
| 145 | + { |
| 146 | + new MessagingExtensionAttachment |
| 147 | + { |
| 148 | + ContentType = AdaptiveCard.ContentType, |
| 149 | + Content = adaptiveCard |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + }; |
| 154 | + } |
| 155 | + catch (Exception ex) |
| 156 | + { |
| 157 | + // Log the exception |
| 158 | + Console.WriteLine($"Error in CreateWebViewResponse: {ex.Message}"); |
| 159 | + throw; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + /// <summary> |
| 164 | + /// Handles the fetch task for messaging extensions. |
| 165 | + /// </summary> |
| 166 | + /// <param name="turnContext">The turn context.</param> |
| 167 | + /// <param name="action">The messaging extension action.</param> |
| 168 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 169 | + /// <returns>A task that represents the messaging extension response.</returns> |
| 170 | + protected override async Task<MessagingExtensionActionResponse> OnTeamsMessagingExtensionFetchTaskAsync( |
| 171 | + ITurnContext<IInvokeActivity> turnContext, |
| 172 | + MessagingExtensionAction action, |
| 173 | + CancellationToken cancellationToken) |
| 174 | + { |
| 175 | + try |
| 176 | + { |
| 177 | + // Check if the replyToId is empty and commandContext is "thirdParty" |
| 178 | + var activityValue = turnContext.Activity.Value as JObject; |
| 179 | + var commandContext = activityValue?["commandContext"]?.ToString(); |
| 180 | + var replyToId = activityValue?["messagePayload"]?["replyToId"]?.ToString(); |
| 181 | + |
| 182 | + // Process if conditions are met |
| 183 | + if (replyToId == "" && commandContext == "thirdParty") |
| 184 | + { |
| 185 | + // Call the method to generate the response based on context |
| 186 | + return CreateMediaNameDetailsTaskResponse(turnContext, action); |
| 187 | + } |
| 188 | + |
| 189 | + // Default response for other conditions |
| 190 | + return new MessagingExtensionActionResponse |
| 191 | + { |
| 192 | + Task = new TaskModuleContinueResponse |
| 193 | + { |
| 194 | + Value = new TaskModuleTaskInfo |
| 195 | + { |
| 196 | + Title = "Default Task", |
| 197 | + Height = 200, |
| 198 | + Width = 400, |
| 199 | + Url = null |
| 200 | + } |
| 201 | + } |
| 202 | + }; |
| 203 | + } |
| 204 | + catch (Exception ex) |
| 205 | + { |
| 206 | + // Log the exception for debugging |
| 207 | + Console.WriteLine($"Error in OnTeamsMessagingExtensionFetchTaskAsync: {ex.Message}"); |
| 208 | + |
| 209 | + // Return an error response |
| 210 | + return new MessagingExtensionActionResponse |
| 211 | + { |
| 212 | + Task = new TaskModuleContinueResponse |
| 213 | + { |
| 214 | + Value = new TaskModuleTaskInfo |
| 215 | + { |
| 216 | + Title = "Error", |
| 217 | + Height = 200, |
| 218 | + Width = 400, |
| 219 | + Url = null |
| 220 | + } |
| 221 | + } |
| 222 | + }; |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + /// <summary> |
| 227 | + /// Generates a task module response for the employee details form. |
| 228 | + /// </summary> |
| 229 | + /// <param name="turnContext">The turn context.</param> |
| 230 | + /// <param name="action">The messaging extension action.</param> |
| 231 | + /// <returns>A MessagingExtensionActionResponse with a task module.</returns> |
| 232 | + private MessagingExtensionActionResponse CreateMediaNameDetailsTaskResponse(ITurnContext<IInvokeActivity> turnContext, MessagingExtensionAction action) |
| 233 | + { |
| 234 | + try |
| 235 | + { |
| 236 | + return new MessagingExtensionActionResponse |
| 237 | + { |
| 238 | + Task = new TaskModuleContinueResponse |
| 239 | + { |
| 240 | + Value = new TaskModuleTaskInfo |
| 241 | + { |
| 242 | + Height = 530, |
| 243 | + Width = 700, |
| 244 | + Title = "Task Module WebView", |
| 245 | + Url = $"{this.baseUrl}/CustomForm", |
| 246 | + }, |
| 247 | + }, |
| 248 | + }; |
| 249 | + } |
| 250 | + catch (Exception ex) |
| 251 | + { |
| 252 | + // Log the exception |
| 253 | + Console.WriteLine($"Error in CreateMediaNameDetailsTaskResponse: {ex.Message}"); |
| 254 | + throw; |
| 255 | + } |
| 256 | + } |
| 257 | + } |
| 258 | +} |
0 commit comments