Forum Discussion
LimnaAdmin
Sep 18, 2023Copper Contributor
Send attachment from teams personal chat to teams group channel using bot framework
Hi All,
I am working on the Teams bot application and we wanted to send attachments from the team personal chat to the team group channel.
We have set supportedFiles to true in the manifest file and used the connector client to send the message from personal chat to the team group channel and vice versa.
ConnectorClient connectorClient = new ConnectorClient(new Uri(serviceUrl), microsoftAppCredentials);
connectorClient.Conversations.SendToConversationAsync(MessageActivity);
Message activity contains the attachment that personal chat has attached. But at the channel side the image is not visible or wrong image path,
- Sayali-MSFT
Microsoft
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
andappPassword
are the credentials of the bot,teamId
is the ID of the Teams channel,botId
is the ID of the bot, andtenantId
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.
- LimnaAdminCopper Contributor
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.
- LimnaAdminCopper Contributor
Sayali-MSFT , is there any alternative to send images as attachments from personal chat to teams channel and vice versa.
-
- Sayali-MSFT
Microsoft
LimnaAdmin- Thanks for reporting your issue.
We will check this at our end and will get back to you.