Friday, May 26, 2017

Azure Bot Service: Automation Integration via WebHooks

If you have been following along, we now have a bot that can chat on a slack channel and interact with them via prompt dialogs.  The goal of this post is to briefly discuss one method of integration with Azure Automation, which is using webhooks.

Webhooks are an effective way of starting Azure automation runbooks remotely (without the need of accessing the portal).  Essentially, using webhooks you create a magic URL that points specifically to an Azure Automation runbook.  You can configure certain run settings on the automation side, but other than that, everything else is parsed via the POST body of the request.

On the client side, one would just need to make use of the HTTPClient class to create a post request and send it to the magic link.  In some cases, you may want to augment the code below to pass in an object that is then converted to JSON.  This would be useful when trying to pass in parameters on the fly.


                    case SnapshotAllOption:
                        await context.PostAsync("Snapshotting all!");
                        using (var client = new HttpClient())
                        {
                            client.BaseAddress = new Uri(BotWebhookAddress);
                            client.DefaultRequestHeaders.Accept.Clear();
                            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                            var response = await client.PostAsJsonAsync(BotWebhookToken,new object());
                            if (response.IsSuccessStatusCode)
                            {
                                await context.PostAsync("Successfully sent request");
                            }
                        }
                            break;


The above example simply calls out to the automation runbook when a user selects the "Snapshot All" option via the bot prompt dialog.  It inspects the result and lets the user know that the automation runbook was kicked off successfully.

This integration is quite simple, and that is the beauty of it.  The more challenging part will be how the runbook talks back to the bot to provide status.  I plan to tackle that in an upcoming post.  The following image shows what this interaction looks like in the webchat interface.


No comments:

Post a Comment