Forum Discussion
Send attached images in teams to another connected user or agent.
We will check this at our end and will get back to you.
- Lakshmi_145Sep 08, 2023Iron Contributor
Is there any update on this. Will be able to send images to connected user or agent using bot framework ?
- Lakshmi_145Sep 09, 2023Iron Contributor
I also tried the below code,
IMessageActivity forwardedMessage = activity; foreach (var attachment in activity.Attachments) { if (attachment.ContentType.StartsWith("image/")) { forwardedMessage = Activity.CreateMessageActivity(); forwardedMessage.Attachments = new List<Attachment> { attachment }; forwardedMessage.ApplyConversationReference(recipient); } }and send the message activity using connector client. Got the same image invalid in recipient teams bot
Also tried to access the content url we are getting in the activity attachment , but it shows the below authorization error message
{"message":"Authorization has been denied for this request."}We are trying to send message from teams bot personal chat to the teams groups chat.
Image was getting received at group chat side when manually edited the attachment conent url with a jpeg image .
- Sayali-MSFTSep 11, 2023
Microsoft
Lakshmi_145 -Could you please try the below code for image attachment-
Also refer the sample -Microsoft-Teams-Samples/samples/bot-file-upload/csharp/Bots/TeamsFileUploadBot.cs at main ยท OfficeDev/Microsoft-Teams-Samples (github.com)private async Task ProcessInlineImage(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken) { var attachment = turnContext.Activity.Attachments[0]; var client = _clientFactory.CreateClient(); // Get Bot's access token to fetch inline image. var token = await new MicrosoftAppCredentials(microsoftAppId, microsoftAppPassword).GetTokenAsync(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); var responseMessage = await client.GetAsync(attachment.ContentUrl); // Save the inline image to Files directory. var filePath = Path.Combine("Files", "ImageFromUser.png"); using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) { await responseMessage.Content.CopyToAsync(fileStream); } // Create reply with image. var reply = MessageFactory.Text($"Attachment of {attachment.ContentType} type and size of {responseMessage.Content.Headers.ContentLength} bytes received."); reply.Attachments = new List<Attachment>() { GetInlineAttachment() }; await turnContext.SendActivityAsync(reply, cancellationToken); } private static Attachment GetInlineAttachment() { var imagePath = Path.Combine("Files", "ImageFromUser.png"); var imageData = Convert.ToBase64String(File.ReadAllBytes(imagePath)); return new Attachment { Name = @"ImageFromUser.png", ContentType = "image/png", ContentUrl = $"data:image/png;base64,{imageData}", }; }