Rob Lockyer Blog and Portfolio

Automatic Slack Notifications from Windows

I often execute long running scripts on my development machine and want to get notified when their done, even when I’m not in the room. I’d prefer to do this without installing anything extra, because nobody hates software more than software developers. I’m already running Slack, so one practical way to do this is by exploiting Slack’s Incoming Webhooks. Unfortunately, all Slack’s examples for web hooks use cURL and I’m developing on Windows 10. There are solutions for getting cURL on Windows, but I’d prefer something that works on a fresh install. This is where Powershell’s Invoke-RestMethod comes in.

First, lets set up the Incoming Webhooks integration in slack. Log into the web app, and click on your team’s name in the top left corner. Select Team settings from the dropdown.

In the menu on the left select Configure Apps. In the Apps Directory search bar at the top search for Incoming Webhooks.
Then hit Add Configuration.
In the dropdown select a default channel you’d like your automated posts to go to, don’t worry, you can change this programmatically in the future. Next hit the big green button: Add Incoming Webhooks integration
The next page you get has documentation and some settings you can adjust. Most of the settings here are just defaults which can be overridden by your scripts. The only thing we’re interested in at the moment is the Webhook URL. Copy it to your clipboard.
Go to the search bar in Windows 10 by hitting the Windows key and typing Powershell ISE to open the integrated powershell editor. Hit File > New to create your new script. Fill it with the following, but make sure you relplace the -Uri argument with your own Webhook URL

$payload = @{
	text = 'Hello World'
$json = $payload | ConvertTo-Json
Invoke-RestMethod -Uri -Method POST -Body $json

In PowerShell ISE hit the green Run Script button to test your script out, you should get ok as a return value, and you should see your message in Slack. Make sure you check the right channel!

You can do a lot more interesting stuff with this once you get the basics working, such as sending different notifications when builds fail or succeed. This script can be called on the command line with your Webhook URL and a bool to indicate success or failure:

param (

$message = 'Build Failed'
$emoji = ':rage:'

	$message = 'Build Succeeded'
	$emoji = ':nerd_face:'

$payload = @{

$json = $payload | ConvertTo-Json
$response = Invoke-RestMethod -Uri $uri -Method POST -Body $json

You can invoke it from plain old windows shell like so:

powershell -command .\buildStatus.ps1 0

I’d love to hear about the awesome things you automate after trying this out!