Love it or hate it, Slack has become an incredibly popular communication and collaboration tool in the enterprise. For many of us it’s even become a place to socialise with friends outside of the workplace. As our Slack workspaces and channels grow, so to does Slack become our go to app for information.
Rather than fight this like some, I’ve looked to embrace it. One way has been looking at ways to replace scripted email notifications and status updates of automation and maintenance tasks, by using Slack Apps / Bots leveraging Incoming Webhooks.
Webhooks are similar to APIs, just a little simpler in use. In the case of Slack and Incoming Webhooks. It’s a way for us to have an app, script, or just an automated process trigger an event that will post a message to a Slack channel. When we create an incoming webhook we are generated a unique URL. When can then post to that URL using a JSON payload.
So instead of having to deal with the legacy approach of SMTP servers and email addresses to send notifications and status updates of automation tasks and processes. I can move all that into Slack massages and have those scripts start posting to channels using some fairly basic PowerShell code. Slack members then have the ability to subscribe (or unsubscribe) to these channels at their own will.
The process of creating a Slack App / Bot using an Incoming Webhook and getting PowerShell to send a message is extremely simple. Let’s start off with first creating the App / Bot and then some PowerShell code to post a Slack message.
Go to https://api.slack.com/apps and sign in to your Slack Workspace you intend to use.
Once signed in click Create New App. Give the App / Bot a name and select the Workspace you want to use. Then click Create App
Next we define what type of App we are creating. Click on Incoming Webhooks
Activate Incoming Webhooks by moving the Slider setting to On.
We now have to add a Webhook to our workspace and channel which will generate a URL for us to use. Click on Add New WebHook to Workspace.
The App requires a workspace and channel that it has permission to post into. Select a Channel in your workspace and click Allow. In my case I select #status_updates
A Webhook URL will now be generated for you to use when posting. This URL contains a secret for posting to your Slack Channel. So protect it and don’t publish it out.
As far as configuring the App that’s all that is required.
The next step now is to actually write the PowerShell code that will send the Slack messages. There are a number of modules and sample scripts on the web on how to do this. But the process is honestly fairly simple and can easily be integrate into any current PowerShell script you have (assuming you have internet access).
All we need is three pieces of information which we will use to ‘Post’ using the Invoke-RestMethod Cmdlet in PowerShell.
The first piece of information is the WebHook itself, this is our personalised URL generated above. The second is ContentType, which is application/json. And the last is Body which contains our payload (message) to post to slack.
Here is a simple Hello World which will post a message to our Slack channel we authorized in the earlier steps. We can execute this directly in PowerShell or our script editor. Remember to replace $Webhook with your own URL.
$Webhook = "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"
$ContentType= 'application/json'
$Body = @"
{
"text": "Hello World",
}
"@
Invoke-RestMethod -uri $Webhook -Method Post -body $Body -ContentType $ContentType
If we jump over to our slack channel we created we should see a new message from our App / Bot.
As you can see the actually PowerShell code is only a few lines and can easily be integrate into a PowerShell script. We can even pass in values from other parts of a script to the $Body of the Slack message to provide a very customised message.
In the example below, we capture a number of variables from another part of a PowerShell script and use that to construct a more complex $body payload for the slack notification message of a server being built.
$Body = @"
{
"text": "A server has been built",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Build of server *$Server* has been completed in *$Cloud* by user *$Username*"
}
},
{
"type": "section",
"block_id": "section567",
"text": {
"type": "mrkdwn",
"text": "<$ShareToMobileLink|Share to Mobile Link> \n IP Address: *$IP* \n OS Type: *$osType* \n Lease: *$lease*"
},
"accessory": {
"type": "image",
"image_url": "$icon",
"alt_text": "$osType Icon"
}
}
]
}
"@
Using this new payload, what you get back over in Slack is a very customised message with hyperlinks, icons, and formatted text.
Using Incoming Webhooks in Slack is a great way to integrate app event notifications without having to learn a whole bunch of APIs. Hopefully this has shown the basics to get started. I’m sure there’s a bunch of different use cases you can think of.
Hi,
When I attempt this, it kicks back the following error, “Invoke-RESTmethod : The remote server returned an error: (400) Bad Request.”
Any advice?
Thanks!
Thank you for providing an excellent example of the json code.
I have been searching for such a helpful article for days.