Search code examples
azurebotframeworkmicrosoft-teamsazure-bot-servicemicrosoft-graph-teams

Get AADObject Id of the user @mentioned to the Microsoft Teams Bot


I am developing a Microsoft Teams bot using Azure Bot service, I have a use case where I need fetch the @mentioned users AAD object Id and use this Id to get other enterprises specific user information. I have tried to use turnContext object's, GetMentions() API which returns Channel Account object, however, the AAD Object property of channel account is empty, though it has an Id property which I believe corresponds to the Bot Service channel's (Microsoft Teams) user.

Given my research, could someone please let me know how to fetch the users AAD object Id or point me to the right documentation?


Solution

  • You could get the id of the user using Bot Context using the id you receive from GetMentions(29:id). Please try this code and let us know if you face any issues:

      private static async Task<string> GetUserEmailId(Activity activity, string id)
        {
            var teamId = context.Activity.GetChannelData<TeamsChannelData>().Team.Id;
            ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
            var members1 = await connector.Conversations.GetConversationMembersAsync(teamId);
            var mem = members1.Where(m => m.Id == id).First().AsTeamsChannelAccount().ObjectId;
            return mem;
        }