简体   繁体   中英

How to get user context from Teams private message using MS bot framework

I am currently implementing a Teams bot that has to get the user name (first name and last name) and the user's email address of the person communicating with the bot via a personal chat.

I am using SDK v4 of the bot framework and tried to implement the approach mentioned here ( https://github.com/OfficeDev/BotBuilder-MicrosoftTeams-dotnet ). The only parameter returned when fetching the teams context is the Tenant Id. Both channel and team are null (I presume this is because I am in a private chat?).

Since I now have the tenant id from the Teams context, how do I use it to retrieve the user's info?

To retrieve the Teams context I'm calling the following:

ITeamsContext teamsContext = turnContext.TurnState.Get<ITeamsContext>();

Once you retrieve the IDs using the ITeamsContext object, you need to use those Ids to fully populate the Teams object. You can do so using the Operations.FetchTeamDetailsAsync method.

To get a roster of members in the conversation, you'll use the GetConversationParametersForCreateOrGetDirectConversation() method. #epicmethodname.

using Microsoft.Bot.Builder.Teams;
using Microsoft.Bot.Schema.Teams;
using Microsoft.Bot.Connector.Teams;
...
ConversationList channels = await teamsContext.Operations.FetchChannelListAsync(incomingTeamId);

TeamDetails teamInfo = await teamsContext.Operations.FetchTeamDetailsAsync(incomingTeamId);

var roster = teamsContext.GetConversationParametersForCreateOrGetDirectConversation(turnContext.Activity.From).Members;

List<TeamsChannelAccount> rosterTC = roster.ToList().ConvertAll(member =>
  {
    return teamsContext.AsTeamsChannelAccount(member);
  });

await turnContext.SendActivityAsync($"You have {roster.Count} number of people in this group. You are {from.Name}");

You can find some getting started help, and additional resources here: https://developer.microsoft.com/en-us/office/blogs/preview-release-of-net-teams-bot-builder-v4-sdk/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM