Sep 05 2023 10:42 AM
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.
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.
Is there any other possible ways we can get the attached image and send to the connected teams channel (agent or user)
Sep 05 2023 10:08 PM
Sep 08 2023 08:56 AM
Is there any update on this. Will be able to send images to connected user or agent using bot framework ?
Sep 09 2023 09:01 AM - edited Sep 09 2023 09:54 AM
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 .
Sep 11 2023 02:19 AM
@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:image/png;base64,{imageData}",
};
}
Sep 11 2023 03:44 AM
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
Sep 13 2023 10:17 AM
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
Sep 14 2023 12:06 AM
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:
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.
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.
Sep 14 2023 02:17 AM
Sep 14 2023 02:40 AM
@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; }
}
Sep 14 2023 03:18 AM
Sep 18 2023 02:18 AM
Sep 22 2023 09:29 AM
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
Sep 26 2023 03:44 AM
@Sayali-MSFT , is there any alternative solution to send image/screenshot attachments from personal chat to channel
Sep 29 2023 06:34 AM - edited Nov 09 2023 01:02 AM
Nov 09 2023 01:03 AM