Send attached images in teams to another connected user or agent.

Brass Contributor

I am working on a Bot framework project.

We have a feature called 'Connect to Agent' for connecting the user to live teams agents. Once connected with the agent, the user can send text messages to the agent and vice versa using ConnectorClient.

 

We wanted to add a new feature to send screenshots/ attach images either from agent or user to the connected one. 

 

 

Aiswarya_Lakshmy_0-1693933433671.png

 We have used the below code to get the attachment content details,

 

foreach (var attachment in activity.Attachments)
{
if (attachment.ContentType == "text/html")
{
message = attachment.Content.ToString();
}
}

 

And to send the message ,

IMessageActivity messageActivity = utilities.CreateMessageActivity(recipient, message);

ConnectorClient.Conversations.SendToConversationAsync((Activity)MessageActivity);

 

Able to send the image to the connected user or agent , but its not displaying as the path is not correct.

 

Aiswarya_Lakshmy_2-1693935738862.png

 

 

Is there any other possible ways we can get the attached image and send to the connected teams channel (agent or user)

 

 

 

15 Replies
@Lakshmi_145-Thanks for reporting your issue.
We will check this at our end and will get back to you.

@Sayali-MSFT 

 

Is there any update on this.  Will be able to send images to connected user or agent using bot framework ?

 

@Lakshmi_145 

 

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 

 

Aiswarya_Lakshmy_0-1694275224372.png

 

 

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 .

@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 · OfficeDe... 

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&colon;image/png;base64,{imageData}",
};
}



 

@Sayali-MSFT 

 

Is it possible to send the images from teams bot personal chat and teams bot groups channel.

In our project we are using the connector client to send the message from personal chat to group channel and viceversa.

 

https://learn.microsoft.com/en-us/microsoftteams/platform/bots/how-to/bots-filesv4 

 

We have referred the above document and updated supportedFiles true in manifest file . But it is also not sending the message from personal chat to group channel using connector client

@Sayali-MSFT 

 

Is there any update on this question, can we able to send message from bot teams channel to bot teams personal chat using bot connector client

@Lakshmi_145 -

Yes, you can send a message from a bot in a Teams channel to a bot in a Teams personal chat using the Bot Connector client. To achieve this, you need to use the proactive messaging feature of the Teams platform.

Here is an example of how you can send a message from a bot in a Teams channel to a bot in a Teams personal chat using the Bot Connector client:

  1. Fetch the team roster to get the list of users in the team. You can use the Microsoft Teams API or the Bot Connector API to fetch the team roster.

  2. Iterate through the list of users and send a direct message to each user. You can use the Bot Connector API to send a direct message to a user.

Here is an example code snippet in C# that demonstrates how to send a message from a bot in a Teams channel to a bot in a Teams personal chat using the Bot Connector client:

using Microsoft.Bot.Connector;
using Microsoft.Bot.Connector.Teams;
using Microsoft.Bot.Connector.Authentication;

// Fetch the team roster
var connectorClient = new ConnectorClient(new Uri(serviceUrl), new MicrosoftAppCredentials(appId, appPassword));
var teamRoster = await connectorClient.Conversations.GetConversationMembersAsync(teamId);

// Send a direct message to each user in the team
foreach (var member in teamRoster)
{
    var conversationParameters = new ConversationParameters
    {
        Bot = new ChannelAccount { Id = botId },
        Members = new ChannelAccount[] { new ChannelAccount { Id = member.Id } },
        ChannelData = new TeamsChannelData { Tenant = new TenantInfo { Id = tenantId } }
    };

    var response = await connectorClient.Conversations.CreateConversationAsync(conversationParameters);
    var conversationId = response.Id;

    var message = Activity.CreateMessageActivity();
    message.From = new ChannelAccount { Id = botId };
    message.Conversation = new ConversationAccount { Id = conversationId };
    message.Text = "Hello from the bot in the Teams channel!";

    await connectorClient.Conversations.SendToConversationAsync(conversationId, (Activity)message);
}

In the above code, serviceUrl is the URL of the Bot Connector service, appId and appPassword are the credentials of the bot, teamId is the ID of the Teams channel, botId is the ID of the bot, and tenantId is the ID of the tenant.

Make sure to replace these placeholders with the actual values specific to your bot and Teams environment.

By using the Bot Connector client and the proactive messaging feature, you can send messages from a bot in a Teams channel to a bot in a Teams personal chat.

Can we use the same code for send images from bot teams channel to bot teams personal chat using bot connector client

@Lakshmi_145 -You can send image like below-

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

public static async Task SendMessageWithImage(string conversationId, string imageUrl)
{
    var botAppId = "YourBotAppId";
    var botAppPassword = "YourBotAppPassword";

    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("https://smba.trafficmanager.net/amer/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var token = await GetToken(botAppId, botAppPassword);
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

        var message = new
        {
            type = "message",
            attachments = new[]
            {
                new
                {
                    contentType = "image/png",
                    contentUrl = imageUrl
                }
            }
        };

        var response = await client.PostAsJsonAsync($"v3/conversations/{conversationId}/activities", message);
        response.EnsureSuccessStatusCode();
    }
}

private static async Task<string> GetToken(string botAppId, string botAppPassword)
{
    using (var client = new HttpClient())
    {
        var tokenEndpoint = $"https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token";
        var body = $"grant_type=client_credentials&client_id={botAppId}&client_secret={botAppPassword}&scope=https%3A%2F%2Fapi.botframework.com%2F.default";

        var response = await client.PostAsync(tokenEndpoint, new StringContent(body, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded"));
        response.EnsureSuccessStatusCode();

        var tokenResponse = await response.Content.ReadAsAsync<TokenResponse>();
        return tokenResponse.access_token;
    }
}

private class TokenResponse
{
    public string access_token { get; set; }
}

Does image url should be in base 64 address.
@Lakshmi_145- Yes. Here also sample available-https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-howto-add-media-attachments?view=azure-bot-service-4.0&tabs=csharp

@Sayali-MSFT 

 

Sample shows the image attachment from the current directory or directly attaching the image to the attachment.

In my scenario, personal chat user is sending a screenshot or attaching image from teams and sending to another user in teams channel. In this case , we need to send the same attachments in the sender to the recipient .

 

I coudnt see any samples sending the sender attachment to the recipient teams 

@Sayali-MSFT , is there any alternative solution to send image/screenshot attachments from personal chat to channel

@Lakshmi_145- We will check this with Internal team and let you know the update.
Please track this issue from here-https://techcommunity.microsoft.com/t5/teams-developer/send-attachment-from-teams-personal-chat-to-teams-group-channel/m-p/3975867#M8837