Forum Discussion
Send attachment from teams personal chat to teams group channel using bot framework
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.
My requirement is for sending attachments (images/ screenshots/smiley) from team's channel to the personal chat and vice versa . (not directly attaching a png/jpeg file. Need to send the same attachment from the sender to the recipient )
The example shared works fine while sending message but note for attachments (images/screenhots/smiley) But while adding the attachment from user activity to teams channel activity it is throwing exception as 'Bad Request'
I want to send the screenshot from personal chat to teams channel and vice versa
var message = Activity.CreateMessageActivity();
message.From = new ChannelAccount { Id = recipient.Bot.Id };
message.Conversation = new ConversationAccount { Id = conversationId };
message.Text = "Hello from the bot in the Teams channel!";
message.Attachments = messageActivity.Attachments; ('messageActivity' will be the sender and 'message' should be the activity send to the recepient with the attachment send from sender )
'messageActivity' will be the sender and 'message' should be the activity send to the recipient with the attachment send from sender.
- LimnaAdminSep 26, 2023Copper Contributor
Sayali-MSFT , is there any alternative to send images as attachments from personal chat to teams channel and vice versa.
- LimnaAdminSep 28, 2023Copper Contributor
@Sayali-MSFT , is there any update on this query
- Sayali-MSFTSep 28, 2023
Microsoft
Could you please look into this sample-https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-howto-add-media-attachments?view=azure-bot-service-4.0&tabs=csharp