Download: Send-ToSlack
As previously commented I had been exploring the use of Slack and integration with other tools. One use case was to send messages to channels within Slack when certain steps in an automated workflow were triggered and to provide notification of the status upon completion.
Therefore, I created a Windows PowerShell function that would post a message to a specified slack channel using the Web API methods available, which according to Slack:
The Slack Web API allows you to build applications that interact with Slack in more complex ways than the integrations we provide out of the box.
Currently, my requirement is only to post messages and therefore the function only provides support for the chat.PostMessage method to posts a message to a public channel, private group, or IM channel.
In order to provide authentication to the API this can be achieved by a bearer token to identify a single user which uses a generated full-access token, see ‘https://api.slack.com/web‘ for more details and to generate/retrieve your bearer token information.
If you plan to authenticate several users, it is recommended to use OAuth2 by registering your application. Currently, this method for authenticatin is not supported within the function.
The function uses the Invoke-WebRequest cmdlet to send a request to the URL ‘https://slack.com/api/chat.postMessage‘ and provide the arguments for generating the message that will be posted to the specified Slack channel. The protocol ‘https’ is specified as all methods must be called using this protocol.
The ‘chat.PostMessage’ method has mandatory requirements to specify the token, channel and text arguments. In addition, the function currently provides the ability to specify the ‘icon’ and ‘username’ arguments when posting a message.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generates POST message to be sent to the slack channel. | |
$PostMessage = @{token="$Token";channel="$Channel";text="$Text";username="$Bot";icon_url="$Icon"} |
The response will contain a JSON object, which contains a top-level boolean property which indicates success of failure. In the event of an error posting the message to the slack channel conditional logic is used to determine if the response contains an error ‘”ok”:false’ and processing the string to terminate with an error and return the machine-readable error code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Process | |
{ | |
Try | |
{ | |
# Sends HTTPS request to the Slack Web API service. | |
$WebRequest = Invoke-WebRequest -Uri $Uri -Body $PostMessage | |
# Conditional logic to generate custom error message if JSON object response contains a top-level error property. | |
If ($WebRequest.Content -like '*"ok":false*') | |
{ | |
# Terminates with error if the response contains an error property, parses the error code and stops processing of the command. | |
Throw ($WebRequest.Content -split '"error":"')[1] -replace '"}','' | |
} # If | |
} # Try | |
Catch | |
{ | |
# Terminates with error and stops processing of the command. | |
Throw ("Unable to send request to the web service with the following exception: " + $Error[0].Exception.Message ) | |
} # Catch | |
} # Process |
On successfully posting a message to a channel, you will receive a customised output to the console session to confirm. The success code is determined by the response containing the string ‘”ok”:true’.
Send-ToSlack -Channel "#apitest" -Text "This is a test message generated by the function Send-ToSlack" -Token "xoxp-17822671332-9811436111-15776151506-a5a9c3855" Successfully sent the message to the slack channel #apitest.
In addition, to the above the function also provides the ability to encrypt your bearer token using an encryption key and retrieve the string using the ‘System.Management.Automation.PSCredential’ class.