Share via

Proactive messaging

Behrouz Houshmandi 20 Reputation points Microsoft Employee
2024-01-26T18:15:06.6566667+00:00
  1. Consider a scenario where your bot wants to initiate communication with a user for the first time, but only has the user's AAD ID and not the Teams ID. I understand that, typically, the Teams ID is required for sending a message to a user—please correct me if I'm wrong. What steps does the bot has to take to send a proactive message to this user?
  2. Is there a way to obtain Teams User ID for a given user AAD ID?
  3. Is there a way to obtain Channel IDs and Tag IDs for a given Teams Team??
Microsoft Teams | Development
Microsoft Teams | Development

Building, integrating, or customizing apps and workflows within Microsoft Teams using developer tools and APIs

Microsoft Teams | Microsoft Teams for business | Other

Answer accepted by question author

Prasad-MSFT 10,491 Reputation points Microsoft External Staff Moderator
2024-01-29T06:12:18.9533333+00:00
  1. 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 403 error with a ForbiddenOperationException message.
  • Retrieve the aadObjectId of the user using the Microsoft Graph API. The aadObjectId is 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 the aadObjectId, tenantId, and serviceUrl. The serviceUrl can 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 conversationId that 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;
  1. 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];
  1. 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.GetTeamChannelsAsync method to get the list of channels. This method requires the teamId and serviceUrl.
  • Iterate through the list of channels and extract the Channel IDs.
  • To obtain the Tag IDs, you can use the TeamsInfo.GetTeamMembersAsync method to get the list of members in the team. This method also requires the teamId and serviceUrl.
  • 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.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.