Building, integrating, or customizing apps and workflows within Microsoft Teams using developer tools and APIs
- In the scenario where your bot wants to initiate communication with a user for the first time and only has the user's AAD ID, you can still send a proactive message to the user. However, please note that sending proactive messages using the AAD ID is supported only in the personal scope.
To send a proactive message to the user, you need to follow these steps:
- Ensure that your bot is installed in the personal scope before sending a proactive message using the AAD ID. If the bot is not installed in the personal scope, the bot will return a
403error with aForbiddenOperationExceptionmessage. - Retrieve the
aadObjectIdof the user using the Microsoft Graph API. TheaadObjectIdis unique to the user and can be used to create a new conversation in the personal chat. - Once you have the
aadObjectId, you can create a new conversation with the user by providing theaadObjectId,tenantId, andserviceUrl. TheserviceUrlcan be obtained from an incoming activity triggering the proactive scenario or using one of the global service URLs mentioned in the documentation. - After creating the conversation, you will receive a
conversationIdthat you can use to send proactive messages to the user.
var conversationParameters = new ConversationParameters
{
Bot = new ChannelAccount { Id = botId },
Members = new List<ChannelAccount> { new ChannelAccount { AadObjectId = aadObjectId } }
};
var connectorClient = new ConnectorClient(new Uri(serviceUrl));
var conversationResourceResponse = await connectorClient.Conversations.CreateConversationAsync(conversationParameters);
var conversationId = conversationResourceResponse.Id;
- Yes, there is a way to obtain the Teams User ID for a given user AAD ID. You can retrieve the Teams User ID by creating a conversation with the user and then extracting the User ID from the conversation object.
var conversationParameters = new ConversationParameters
{
Bot = new ChannelAccount { Id = botId },
Members = new List<ChannelAccount> { new ChannelAccount { AadObjectId = aadObjectId } }
};
var connectorClient = new ConnectorClient(new Uri(serviceUrl));
var conversationResourceResponse = await connectorClient.Conversations.CreateConversationAsync(conversationParameters);
var conversationId = conversationResourceResponse.Id;
// Extract the Teams User ID from the conversation object
var teamsUserId = conversationId.Split(';')[0];
- Yes, there is a way to obtain the Channel IDs and Tag IDs for a given Teams Team. You can retrieve the list of channels and members of a team where your app is installed.
Here are the steps to obtain the Channel IDs and Tag IDs:
- Retrieve the list of channels in the team where your app is installed. You can use the
TeamsInfo.GetTeamChannelsAsyncmethod to get the list of channels. This method requires theteamIdandserviceUrl. - Iterate through the list of channels and extract the Channel IDs.
- To obtain the Tag IDs, you can use the
TeamsInfo.GetTeamMembersAsyncmethod to get the list of members in the team. This method also requires theteamIdandserviceUrl. - Iterate through the list of members and extract the Tag IDs.
var channels = await TeamsInfo.GetTeamChannelsAsync(teamId, serviceUrl);
var channelIds = channels.Select(channel => channel.Id).ToList();
var members = await TeamsInfo.GetTeamMembersAsync(teamId, serviceUrl);
var tagIds = members.Select(member => member.Id).ToList();
Thanks,
Prasad Das ************************************************************************* If the response is helpful, please click "Accept Answer" and upvote it. You can share your feedback via Microsoft Teams Developer Feedback link. Click here to escalate.