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

倖福魔咒の 提交于 2019-12-08 07:53:02

问题


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>();

回答1:


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/



来源:https://stackoverflow.com/questions/54370426/how-to-get-user-context-from-teams-private-message-using-ms-bot-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!