Getting Web Chat Channel Keys from Azure Bot Service using Powershell Runbooks

Created in Code
1 min readFeb 10, 2019

--

In order to provide links to the example web chat channel provided by the bot service when you create a new bot channel registration you generally have to login to the portal and copy the embed code and keys provided.

Sometimes though, you want to automate this process for the end user — unfortunately I couldn’t find a simple way of doing this with a powershell runbook. There are a few examples online indicating that the REST API is the way to go, however none of them worked for me personally. After a bit of digging I did find an endpoint that seemed to do the trick.

https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroup/providers/Microsoft.BotService/botServices/$BotId/channels/WebChatChannel/properties?api-version=2017-12-01

The key can then be retrieved under:

$results.properties.properties.sites[0].key

So, putting it all together we get the full runbook looking something like this:

$accessDetails = .\Get-AzureManagementAccessToken.ps1$AccessToken = $accessDetails.AccessToken$SubscriptionId = $accessDetails.SubscriptionId$ResourceGroup = "resource-group-name"$BotId = "bot-id"$headerParams = @{‘Authorization’=”Bearer $AccessToken”}$url = “https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroup/providers/Microsoft.BotService/botServices/$BotId/channels/WebChatChannel/properties?api-version=2017-12-01"$results = Invoke-RestMethod -Uri $url -Headers $headerParams -Method Get -ContentType ‘application/json’Write-Output -InputObject $results.properties.properties.sites[0].key

Using a child runbook to get the access token. Details for Get-AzureManagementAccessToken can be found here: https://medium.com/@createdincode/making-azure-management-api-calls-with-azure-automation-runbooks-745c5ba541ee

You can then go ahead and use your key to link to the bot channel webchat with urls in this format:

https://webchat.botframework.com/embed/$BotId?s=$WebChatKey

--

--

Created in Code

C#/ASP.NET — Chatbots, AI, Machine Learning and the Web