|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using Microsoft.Agents.Builder; |
| 5 | +using Microsoft.Agents.Builder.App; |
| 6 | +using Microsoft.Agents.Builder.State; |
| 7 | +using Microsoft.Agents.Connector; |
| 8 | +using Microsoft.Agents.Connector.Types; |
| 9 | +using Microsoft.Agents.Core.Models; |
| 10 | +using System; |
| 11 | +using System.IO; |
| 12 | +using System.Linq; |
| 13 | +using System.Threading; |
| 14 | +using System.Threading.Tasks; |
| 15 | + |
| 16 | +namespace HandlingAttachments; |
| 17 | + |
| 18 | +public class AttachmentsAgent : AgentApplication |
| 19 | +{ |
| 20 | + public AttachmentsAgent(AgentApplicationOptions options) : base(options) |
| 21 | + { |
| 22 | + OnConversationUpdate(ConversationUpdateEvents.MembersAdded, WelcomeMessageAsync); |
| 23 | + OnActivity(ActivityTypes.Message, OnMessageAsync, rank: RouteRank.Last); |
| 24 | + } |
| 25 | + |
| 26 | + private async Task WelcomeMessageAsync(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken) |
| 27 | + { |
| 28 | + foreach (var member in turnContext.Activity.MembersAdded) |
| 29 | + { |
| 30 | + if (member.Id != turnContext.Activity.Recipient.Id) |
| 31 | + { |
| 32 | + await turnContext.SendActivityAsync( |
| 33 | + $"Welcome to HandlingAttachment Agent." + |
| 34 | + $" This bot will introduce you to Attachments." + |
| 35 | + $" Please select an option", |
| 36 | + cancellationToken: cancellationToken); |
| 37 | + await DisplayOptionsAsync(turnContext, cancellationToken); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private async Task OnMessageAsync(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken) |
| 43 | + { |
| 44 | + var reply = await ProcessInput(turnContext, turnState, cancellationToken); |
| 45 | + if (reply != null) |
| 46 | + { |
| 47 | + // Respond to the user. |
| 48 | + await turnContext.SendActivityAsync(reply, cancellationToken); |
| 49 | + } |
| 50 | + await DisplayOptionsAsync(turnContext, cancellationToken); |
| 51 | + } |
| 52 | + |
| 53 | + private static async Task DisplayOptionsAsync(ITurnContext turnContext, CancellationToken cancellationToken) |
| 54 | + { |
| 55 | + // Create a HeroCard with options for the user to interact with the bot. |
| 56 | + var card = new HeroCard |
| 57 | + { |
| 58 | + Text = "You can upload an image or select one of the following choices", |
| 59 | + Buttons = |
| 60 | + [ |
| 61 | + // Note that some channels require different values to be used in order to get buttons to display text. |
| 62 | + // In this code the emulator is accounted for with the 'title' parameter, but in other channels you may |
| 63 | + // need to provide a value for other parameters like 'text' or 'displayText'. |
| 64 | + new CardAction(ActionTypes.ImBack, title: "1. Inline Attachment", value: "1"), |
| 65 | + new CardAction(ActionTypes.ImBack, title: "2. Internet Attachment", value: "2"), |
| 66 | + ], |
| 67 | + }; |
| 68 | + |
| 69 | + if (!turnContext.Activity.ChannelId.Equals(Channels.Msteams, StringComparison.OrdinalIgnoreCase)) |
| 70 | + { |
| 71 | + card.Buttons.Add(new CardAction(ActionTypes.ImBack, title: "3. Uploaded Attachment", value: "3")); |
| 72 | + } |
| 73 | + |
| 74 | + var reply = MessageFactory.Attachment(card.ToAttachment()); |
| 75 | + await turnContext.SendActivityAsync(reply, cancellationToken); |
| 76 | + } |
| 77 | + |
| 78 | + // Given the input from the message, create the response. |
| 79 | + private static async Task<IActivity> ProcessInput(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken) |
| 80 | + { |
| 81 | + IActivity reply; |
| 82 | + |
| 83 | + if (turnState.Temp.InputFiles.Any()) |
| 84 | + { |
| 85 | + reply = MessageFactory.Text($"There are {turnState.Temp.InputFiles.Count} attachments."); |
| 86 | + |
| 87 | + var imageData = Convert.ToBase64String(turnState.Temp.InputFiles[0].Content); |
| 88 | + reply.Attachments = [new Attachment() { Name = turnState.Temp.InputFiles[0].Filename, ContentType = "image/png", ContentUrl = $"data:image/png;base64,{imageData}" }]; |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + // Send at attachment to the user. |
| 93 | + reply = await HandleOutgoingAttachment(turnContext, turnContext.Activity, cancellationToken); |
| 94 | + } |
| 95 | + |
| 96 | + return reply; |
| 97 | + } |
| 98 | + |
| 99 | + // Returns a reply with the requested Attachment |
| 100 | + private static async Task<IActivity> HandleOutgoingAttachment(ITurnContext turnContext, IActivity activity, CancellationToken cancellationToken) |
| 101 | + { |
| 102 | + // Look at the user input, and figure out what kind of attachment to send. |
| 103 | + |
| 104 | + if (string.IsNullOrEmpty(activity.Text)) |
| 105 | + { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + IActivity reply = null; |
| 110 | + |
| 111 | + if (activity.Text.StartsWith('1')) |
| 112 | + { |
| 113 | + reply = MessageFactory.Text("This is an inline attachment."); |
| 114 | + reply.Attachments = [GetInlineAttachment()]; |
| 115 | + } |
| 116 | + else if (activity.Text.StartsWith('2')) |
| 117 | + { |
| 118 | + reply = MessageFactory.Text("This is an attachment from a HTTP URL."); |
| 119 | + reply.Attachments = [GetInternetAttachment()]; |
| 120 | + } |
| 121 | + else if (activity.Text.StartsWith('3')) |
| 122 | + { |
| 123 | + reply = MessageFactory.Text("This is an uploaded attachment."); |
| 124 | + |
| 125 | + // Get the uploaded attachment. |
| 126 | + var uploadedAttachment = await UploadAttachmentAsync(turnContext, activity.ServiceUrl, activity.Conversation.Id, cancellationToken); |
| 127 | + reply.Attachments = [uploadedAttachment]; |
| 128 | + } |
| 129 | + |
| 130 | + return reply; |
| 131 | + } |
| 132 | + |
| 133 | + // Creates an inline attachment sent from the bot to the user using a base64 string. |
| 134 | + // Using a base64 string to send an attachment will not work on all channels. |
| 135 | + // Additionally, some channels will only allow certain file types to be sent this way. |
| 136 | + // For example a .png file may work but a .pdf file may not on some channels. |
| 137 | + // Please consult the channel documentation for specifics. |
| 138 | + private static Attachment GetInlineAttachment() |
| 139 | + { |
| 140 | + var imagePath = Path.Combine(Environment.CurrentDirectory, @"Resources", "build-agents.png"); |
| 141 | + var imageData = Convert.ToBase64String(File.ReadAllBytes(imagePath)); |
| 142 | + |
| 143 | + return new Attachment |
| 144 | + { |
| 145 | + Name = @"Resources\build-agents.png", |
| 146 | + ContentType = "image/png", |
| 147 | + ContentUrl = $"data:image/png;base64,{imageData}", |
| 148 | + }; |
| 149 | + } |
| 150 | + |
| 151 | + // Creates an "Attachment" to be sent from the bot to the user from an uploaded file. |
| 152 | + private static async Task<Attachment> UploadAttachmentAsync(ITurnContext turnContext, string serviceUrl, string conversationId, CancellationToken cancellationToken) |
| 153 | + { |
| 154 | + if (string.IsNullOrWhiteSpace(serviceUrl)) |
| 155 | + { |
| 156 | + throw new ArgumentNullException(nameof(serviceUrl)); |
| 157 | + } |
| 158 | + |
| 159 | + if (string.IsNullOrWhiteSpace(conversationId)) |
| 160 | + { |
| 161 | + throw new ArgumentNullException(nameof(conversationId)); |
| 162 | + } |
| 163 | + |
| 164 | + var imagePath = Path.Combine(Environment.CurrentDirectory, @"Resources", "agents-sdk.png"); |
| 165 | + |
| 166 | + var connector = turnContext.Services.Get<IConnectorClient>(); |
| 167 | + |
| 168 | + // This only supports payloads smaller than 260k |
| 169 | + var response = await connector.Conversations.UploadAttachmentAsync( |
| 170 | + conversationId, |
| 171 | + new AttachmentData |
| 172 | + { |
| 173 | + Name = @"Resources\agents-sdk.png", |
| 174 | + OriginalBase64 = File.ReadAllBytes(imagePath), |
| 175 | + Type = "image/png", |
| 176 | + }, |
| 177 | + cancellationToken); |
| 178 | + |
| 179 | + var attachmentUri = connector.Attachments.GetAttachmentUri(response.Id); |
| 180 | + |
| 181 | + return new Attachment |
| 182 | + { |
| 183 | + Name = @"Resources\agents-sdk.png", |
| 184 | + ContentType = "image/png", |
| 185 | + ContentUrl = attachmentUri, |
| 186 | + }; |
| 187 | + } |
| 188 | + |
| 189 | + // Creates an <see cref="Attachment"/> to be sent from the bot to the user from a HTTP URL. |
| 190 | + private static Attachment GetInternetAttachment() |
| 191 | + { |
| 192 | + // ContentUrl must be HTTPS. |
| 193 | + return new Attachment |
| 194 | + { |
| 195 | + Name = @"Resources\introducing-agents-sdk.png", |
| 196 | + ContentType = "image/png", |
| 197 | + ContentUrl = "https://devblogs.microsoft.com/microsoft365dev/wp-content/uploads/sites/73/2024/11/word-image-23435-1.png", |
| 198 | + }; |
| 199 | + } |
| 200 | +} |
0 commit comments