Public/New-SlackMessage.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
function New-SlackMessage { <# .SYNOPSIS Construct a new Slack message .DESCRIPTION Construct a new Slack message Note that this does not send a message It produces a message to send with Send-SlackMessage .PARAMETER Channel Channel, private group, or IM channel to send message to. Can be an encoded ID, or a name. .PARAMETER Text Text of the message to send See formatting spec for more information. https://api.slack.com/docs/formatting .PARAMETER Username Set your bot's user name. Must be used in conjunction with as_user set to false, otherwise ignored See authorship details: https://api.slack.com/methods/chat.postMessage#authorship .PARAMETER IconUrl URL to an image to use as the icon for this message. If using a token, must be used in conjunction with as_user set to false, otherwise ignored. See authorship details: https://api.slack.com/methods/chat.postMessage#authorship .PARAMETER IconEmoji Emoji to use as the icon for this message. Overrides icon_url. If using a token, must be used in conjunction with as_user set to false, otherwise ignored .PARAMETER AsUser Use true to post the message as the authed user, instead of as a bot. Defaults to false. Only used when authorizing with a token See authorship details: https://api.slack.com/methods/chat.postMessage#authorship .PARAMETER LinkNames Find and link channel names and usernames. .PARAMETER Parse Change how messages are treated. Defaults to none If set to full, channels like #general and usernames like @bob will be linkified. More details here: https://api.slack.com/docs/formatting#linking_to_channels_and_users .PARAMETER UnfurlLinks Use true to enable unfurling of primarily text-based content. .PARAMETER UnfurlMedia Use false to disable unfurling of media content. .PARAMETER Attachments Optional rich structured message attachments. Provide one or more hash tables created using New-SlackMessageAttachment See attachments spec https://api.slack.com/docs/attachments .EXAMPLE # This is a simple example illustrating some common options # when constructing a message attachment # giving you a richer message $Token = 'A token. maybe from https://api.slack.com/docs/oauth-test-tokens' New-SlackMessageAttachment -Color $([System.Drawing.Color]::red) ` -Title 'The System Is Down' ` -TitleLink https://www.youtube.com/watch?v=TmpRs7xN06Q ` -Text 'Please Do The Needful' ` -Pretext 'Everything is broken' ` -AuthorName 'SCOM Bot' ` -AuthorIcon 'http://ramblingcookiemonster.github.io/images/tools/wrench.png' ` -Fallback 'Your client is bad' | New-SlackMessage -Channel '@wframe' ` -IconEmoji :bomb: | Send-SlackMessage -Token $Token # Create a message attachment with details about an alert # Attach this to a slack message sending to the devnull channel # Send the newly created message using a token .EXAMPLE # This example demonstrates that you can chain new attachments # together to form a multi-attachment message $Token = 'A token. maybe from https://api.slack.com/docs/oauth-test-tokens' New-SlackMessageAttachment -Color $([System.Drawing.Color]::red) ` -Title 'The System Is Down' ` -TitleLink https://www.youtube.com/watch?v=TmpRs7xN06Q ` -Text 'Everybody panic!' ` -Pretext 'Everything is broken' ` -Fallback 'Your client is bad' | New-SlackMessageAttachment -Color $([System.Drawing.Color]::Orange) ` -Title 'The Other System Is Down' ` -TitleLink https://www.youtube.com/watch?v=TmpRs7xN06Q ` -Text 'Please Do The Needful' ` -Fallback 'Your client is bad' | New-SlackMessage -Channel '@wframe' ` -IconEmoji :bomb: ` -AsUser ` -Username 'SCOM Bot' | Send-SlackMessage -Token $Token # Create an attachment, create another attachment, # add these to a message, # and send with a token .EXAMPLE # This example illustrates a pattern where you might # want to send output from a script; you might # include errors, successful items, or other output # Pretend we're in a script, and caught an exception of some sort $Fail = [pscustomobject]@{ samaccountname = 'bob' operation = 'Remove privileges' status = "An error message" timestamp = (Get-Date).ToString() } # Create an array from the properties in our fail object $Fields = @() foreach($Prop in $Fail.psobject.Properties.Name) { $Fields += @{ title = $Prop value = $Fail.$Prop short = $true } } $Token = 'A token. maybe from https://api.slack.com/docs/oauth-test-tokens' # Construct and send the message! New-SlackMessageAttachment -Color $([System.Drawing.Color]::Orange) ` -Title 'Failed to process account' ` -Fields $Fields ` -Fallback 'Your client is bad' | New-SlackMessage -Channel 'devnull' | Send-SlackMessage -Uri $uri # We build up a pretend error object, and send each property to a 'Fields' array # Creates an attachment with the fields from our error # Creates a message fromthat attachment and sents it with a uri .FUNCTIONALITY Slack #> [CmdletBinding()] [OutputType([System.Collections.Hashtable],[String])] Param ( [string]$Channel, [string]$Text, [string]$Username, [string]$IconUrl, [string]$IconEmoji, [switch]$AsUser, [switch]$LinkNames, [validateset('full','none')] [string]$Parse, [validateset($True, $False)] [bool]$UnfurlLinks, [validateset($True, $False)] [bool]$UnfurlMedia, [Parameter(Mandatory=$true, ValueFromPipeline = $true, Position=1)] [PSTypeName('PSSlack.MessageAttachment')] [System.Collections.Hashtable[]] $Attachments ) Begin { $AllAttachments = @() } Process { foreach($Attachment in $Attachments) { $AllAttachments += $Attachments } } End { $body = @{} switch ($psboundparameters.keys) { 'channel' { $body.channel = $Channel} 'text' { $body.text = $text} 'username' { $body.username = $username} 'as_user' { $body.asuser = $AsUser} 'iconurl' { $body.icon_url = $iconurl} 'iconemoji' { $body.icon_emoji = $iconemoji} 'linknames' { $body.link_names = 1} 'Parse' { $body.Parse = $Parse} 'UnfurlLinks' { $body.Unfurl_Links = $UnfurlLinks} 'UnfurlMedia' { $body.Unfurl_Media = $UnfurlMedia} 'iconurl' { $body.icon_url = $iconurl} 'attachments' { $body.attachments = @($AllAttachments)} } Add-ObjectDetail -InputObject $body -TypeName PSSlack.Message } } |