Search code examples
c#botframeworkchatbotmicrosoft-teams

How get user email from MS Teams with a Bot using SDK4?


We deployed our bot application on the MS-Teams channel so for Authorization purposes, we required the emailId of the MSTeams logged-in user, so initially, We are facing an issue for getting emailId from the Activity object. After doing some analysis, we found one solution, that emailId will get from the BotFrameworkAdapter class in ms-bot v4 using C#


Solution

  • Private async  Task GetUserProfile(TurnContext context,CancellationToken cancellationToken)
    {
        BotFrameworkAdapter botAdapter = (BotFrameworkAdapter)context.Adapter;           
        var conversation = await botAdapter.GetConversationMembersAsync(context, cancellationToken);
    }
    

    We will get the following response from the conversation variable

    { "id": "UserConversationId", "name": "Full Name of the user", "aadObjectId": null, "role": null, "objectId": "Object Id", "givenName": "FirstName", "surname": "LastNamr", "email": "OrganizationEmailId", "userPrincipalName": "UserPrincipalName", "tenantId": "TenantId", "memberRole": "user" }