Theodore - a bot for creating a team in Microsoft Teams
My last posts were all about getting up to speed with the Microsoft Teams PowerShell cmdlets released two weeks ago. Running those cmdlets in an Azure Function creates some nice options in terms of creating a team in Teams (still, the wording is just awkward). I’m sure you read about all the governance discussion that services like Teams or Groups start in each environment. Only with proper tooling, you can manage the process of orchestrating your different collaboration features in Office365. To take this idea to the next level, what about using a bot in Teams to handling a users request of creating a new team. This post describes how you can create a bot for your users that will ask for some data in order to create a new team in Microsoft Teams. The bot will call an http triggered Azure Function that is using PowerShell to handle the requests and has the new Teams cmdlets deployed to be able to create a new team in our tenant. To code in the git hub repo is based on an example project (https://github.com/Microsoft/BotBuilder-Samples) of a dot.net bot that shows how the different dialogs in a bot can be wired together. The solution is quite simple and of course no way production ready: An example dialog of the bot looks like this: To create this kind of dialog you simply start the interaction with the user with code like this:private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result) { PromptDialog.Choice( context, this.AfterChoiceSelected, new\[\] { ShowMyTeamsOption, CreateTeamOption }, "Hi I'm Theodor your Team automation but. How can I help?", "I am sorry but I didn't understand that. I need you to select one of the options below", attempts: 2); }
This will create our two different buttons and will guide the user in the directions we want them to choose from. After we have all the data from the user, in our case just an alias and a display name, we can now call the Azure Function handling our request.```
Dictionary<string, string> dictionary = new Dictionary<string, string>
{
{ “displayName”, this.displayname },
{ “alias”, this.alias }
};
string json = JsonConvert.SerializeObject(dictionary);
var requestData = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync(String.Format(TEAMS\_FUNCTION\_URI+NEW\_TEAM\_CMD), requestData);
var teams\_result = await response.Content.ReadAsStringAsync();
This example works with a command parameter in the function endpoint and some basic json data in the body of the request. The PowerShell script is also pretty small and simple at the moment:
switch($REQ_QUERY_cmd){
“GetTeam”
{
#$body = “GetTeam”
Connect-MicrosoftTeams -Credential $mycreds
$teams = Get-Team -User “user@tenant.onmicrosoft.com”
$body = $teams | ConvertTo-Json
}
“NewTeam”
{
#$body = Get-Content $req
Write-Output Get-Content $req
$content = Get-Content $req -Raw | ConvertFrom-Json
Write-Output “trying to create team with Displayname: " + $content.displayName + “and alias: " + $content.alias
Connect-MicrosoftTeams -Credential $mycreds
$newTeam = New-Team -DisplayName $content.displayName -Alias $content.alias
$body = $newTeam.GroupId
}
default
{
$body = "CMD param contains no known command"
}
}