https://github.com/microsoft/botframework/blob/master/whats-new.md#november-2019-ignite
One thing which I am very happy about is that building Bots for Microsoft Teams has become way easier now!
This wasn't always the case. Previously, the Bot Framework was separate to the Teams Bot Builder and they both didn't play nice all the time. Throw in more stuff like Bot Framework v3/v4, Messaging extensions, Adaptive Cards and it lead to tweets like this:
Interesting couple of weeks with Bot Framework v4, Teams Bot Builder, Messaging Extensions and user delegated auth! Not everything wants to play nice with each other but finally getting somewhere!— Vardhaman Deshpande (@vrdmn) August 30, 2019
But now, the Teams Bot Builder is part of the the Bot Framework SDK itself which is very good news. If you are just getting started building Bots for Microsoft Teams, you only need to install the Bot Framework package. Apart from that, the code to work with Microsoft Teams has been simplified as well.
Imagine you are building a Teams Bot and it needs to interact with the Office 365 Group which underpins the Team. Or maybe the Bot needs to store or retrieve some data from the SharePoint site associated with the Team. Let's see how easy it is now to achieve this:
Before we look at the code, make sure you are using the Microsoft.Bot.Builder 4.6+ version of packages in your Bot:
https://github.com/Microsoft/botbuilder-dotnet/#packages
https://www.nuget.org/packages/Microsoft.Bot.Builder/
We will also need the Microsoft Graph .NET SDK:
https://docs.microsoft.com/en-us/graph/sdks/sdk-installation
And here is the code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken) | |
{ | |
//Part of the Bot Framework 4.6 update | |
TeamDetails teamDetails = await TeamsInfo.GetTeamDetailsAsync(turnContext); | |
//The O365 Group id of the current team | |
string office365GroupId = teamDetails.AadGroupId; | |
//Get an object of the GraphServiceClient. See here for more details: https://docs.microsoft.com/en-us/graph/sdks/create-client?tabs=CS | |
var graphClient = await GetGraphServiceClient(); | |
//Once you have the O365 Group Id, you can get further details through the Microsoft Graph, including the SharePoint site url | |
Site site = await graphClient.Groups[office365GroupId].Sites["root"].Request().Select("webUrl").GetAsync(); | |
//Bot echo | |
await turnContext.SendActivityAsync(MessageFactory.Text($"Current Office 365 Group Id is '{office365GroupId}' and the SharePoint site url is '{site.WebUrl}'"), cancellationToken); | |
} |
We are using the new TeamsInfo class available in the Bot Framework to get the current team details. This class also has a few other helper methods which you might find useful:
https://github.com/microsoft/botbuilder-dotnet/blob/master/libraries/Microsoft.Bot.Builder/Teams/TeamsInfo.cs
Internally the TeamsInfo.GetTeamDetailsAsync method calls the `/v3/teams/{team-id}` API endpoint to get the Office 365 Group id (a.k.a AADGroupId in the API). We can then use the Microsoft Graph API to get the other details including the SharePoint site url.
Hope this helps! For more details on building bots for Microsoft Teams, have a look at the official docs: https://docs.microsoft.com/en-us/microsoftteams/platform/bots/how-to/create-a-bot-for-teams