Public/Send-RyverMessage.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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
function Send-RyverMessage { <# .SYNOPSIS Send a Ryver message. .DESCRIPTION Send a Ryver message. .INPUTS System.String .NOTES - Troy Lindsay - Twitter: @troylindsay42 - GitHub: tlindsay42 .EXAMPLE Send-RyverMessage -Message 'Test' If the default incoming webhook URI is set in the running configuration (available via Get-PSRyverConfig), then the message 'Test' is posted in the configured channel; otherwise, it will generate an invalid request error. .EXAMPLE Send-RyverMessage -ID 12345678 -Type 'Team' -Message 'Test' Posts the message 'Test' in private team channel 12345678. .EXAMPLE Send-RyverMessage -IncomingWebhookUri 'https://example.ryver.com/application/webhook/4z-_G76nj7Fic-M' -Message 'Test' Posts the message 'Test' to the channel configured in the specified incoming webhook URI. .EXAMPLE Send-RyverMessage -ID 12345678 -Type 'Forum' -Message 'Test' -FromDisplayName 'PSRyver' -FromAvatar 'https://pbs.twimg.com/profile_images/825987046992814080/7VZkFQA9_400x400.jpg' Posts the message 'Test' to public forum channel 12345678 with a display name override of 'PSRyver', and an avatar override defined by the URI. .EXAMPLE Send-RyverMessage -ID 12345678 -Type 'Forum' -Message 'Test' -Ephemeral -Raw -Credential ( Get-Credential ) Updates the $Script:PSRyver.Authorization value storing the basic authentication authorization header to use for all requests and then posts the temporary message 'Test' in public forum channel 12345678, and the resultant output will not be formatted. The temporary message will disappear for each user after he or she reads the message and refresh. .EXAMPLE Get-RyverUser -ID 12345678 | Send-RyverMessage -Message 'Hello!' Queries for the user with ID 12345678 and sends the message 'Hello!'. .EXAMPLE 'All your base are belong to us.', 'You have no chance to survive make your time.' | Send-RyverMessage -ID 12345678 -Type 'Forum' -FromDisplayName 'CATS' -FromAvatar 'https://upload.wikimedia.org/wikipedia/en/0/03/Aybabtu.png' Posts 'All your base are belong to us.' and 'You have not chance to survive make your time.' in the Ryver public forum channel with the display name of CATS and the Zero Wing CATS avatar image. .EXAMPLE Send-RyverMessage 12345678 'User' 'Test' Posts the message 'Test' in a direct message to user 12345678. .LINK https://tlindsay42.github.io/PSRyver/Public/Send-RyverMessage/ .LINK https://github.com/tlindsay42/PSRyver/blob/master/PSRyver/Public/Send-RyverMessage.ps1 .LINK https://api.ryver.com/ryvrest_api_examples.html#create-chat-message .FUNCTIONALITY Ryver #> [CmdletBinding( HelpUri = 'https://tlindsay42.github.io/PSRyver/Public/Send-RyverMessage/', DefaultParameterSetName = 'IncomingWebhookUri' )] [OutputType( [PSCustomObject[]], [PSCustomObject], ParameterSetName = 'ID' )] [OutputType( [Void], ParameterSetName = 'IncomingWebhookUri' )] param ( <# Specifies the ID of the public forum channel, private team channel, or user direct message where the chat message will be posted. #> [Parameter( ParameterSetName = 'ID', Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true )] [UInt64] $ID, <# Specifies the type of channel to post the message in: - Public forum - Private team - User direct message #> [Parameter( ParameterSetName = 'ID', Mandatory = $true, Position = 1, ValueFromPipelineByPropertyName = $true )] [ValidateSet( 'Forum', 'Team', 'User' )] [String] $Type, <# Specifies the Ryver format chat message incoming webhook URI. https://api.ryver.com/ryvhooks_simple_incoming.html#chat-message #> [Parameter( ParameterSetName = 'IncomingWebhookUri', Position = 0, ValueFromPipelineByPropertyName = $true )] [ValidateScript( { $_.IsAbsoluteUri -eq $true -and $_.Scheme -eq 'https' } )] [Uri] $IncomingWebhookUri = $Script:PSRyver.IncomingWebhookUri, # Specifies the message content to post. [Parameter( ParameterSetName = 'ID', Mandatory = $true, Position = 2, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true )] [Parameter( ParameterSetName = 'IncomingWebhookUri', Mandatory = $true, Position = 1, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true )] [ValidateNotNullOrEmpty()] [String] $Message, <# Optional flag to indicate that you want the message to be ephemeral, meaning it will be displayed to people in the Ryver client when the message comes in, but the message will not be stored in chat history. Once a user refreshes, the message will go away. #> [Parameter( ParameterSetName = 'ID', Position = 3 )] [Parameter( ParameterSetName = 'IncomingWebhookUri', Position = 2 )] [Switch] $Ephemeral, <# Specifies the From name displayed for the message. Anyone that clicks on the from name or reviews the chat history will see the actual user account's display name. #> [Parameter( ParameterSetName = 'ID', Position = 4 )] [Parameter( ParameterSetName = 'IncomingWebhookUri', Position = 3 )] [ValidateNotNullOrEmpty()] [String] $FromDisplayName, # Specifies the From avatar displayed for the message. [Parameter( ParameterSetName = 'ID', Position = 5 )] [Parameter( ParameterSetName = 'IncomingWebhookUri', Position = 4 )] [ValidateScript( { $_.IsAbsoluteUri -eq $true } )] [Uri] $FromAvatar, # Specifies that objects should not be formatted. [Parameter( ParameterSetName = 'ID', Position = 6 )] [Parameter( ParameterSetName = 'IncomingWebhookUri', Position = 5 )] [Switch] $Raw, <# Credentials to use for the Ryver API. Default value is the value set by Set-PSRyverConfig. #> [Parameter( ParameterSetName = 'ID', Position = 7 )] [PSCredential] $Credential ) begin { $function = $MyInvocation.MyCommand.Name Write-Verbose -Message "Beginning: '${function}'." if ( $PSBoundParameters.ContainsKey( 'Credential' ) ) { $Script:PSRyver.Authorization = ConvertTo-Authorization -Credential $Credential Remove-Variable -Name 'Credential' } } process { Write-Verbose -Message ( "Processing: '${function}' with ParameterSetName '$( $PSCmdlet.ParameterSetName )' and Parameters: " + ( $PSBoundParameters | Remove-SensitiveData | Format-Table -AutoSize | Out-String ) ) #region Build Body $body = @{ # Key names are case sensitive body = $Message isEphemeral = [Boolean] $Ephemeral } if ( $PSBoundParameters.ContainsKey( 'FromDisplayName' ) -eq $true -or $PSBoundParameters.ContainsKey( 'FromAvatar' ) -eq $true ) { $body.Add( # Key name is case sensitive 'createSource', @{ # Key names are case sensitive avatar = $FromAvatar displayName = $FromDisplayName } ) } $body = $body | ConvertTo-Json -ErrorAction 'Stop' #endregion if ( $PSCmdlet.ParameterSetName -eq 'ID' ) { #region init $path = '/' #endregion Assert-RyverApiConfig #region Build Path switch ( $Type ) { 'Forum' { $path += "forums(${ID})" } 'Team' { $path += "workrooms(${ID})" } 'User' { $path += "users(${ID})" } } $path += '/Chat.PostMessage()' #endregion #region Send message $splat = @{ Path = $path Method = 'Post' Body = $body } $response = Invoke-RyverRestMethod @splat #endregion #region Process response $return = $response | Select-Object -ExpandProperty 'D' if ( $Raw -eq $true ) { $return } else { [PSCustomObject] @{ ID = $return.ID } } #endregion } elseif ( $IncomingWebhookUri ) { $splat = @{ Uri = $IncomingWebhookUri Headers = @{ Accept = 'application/json' 'Content-Type' = 'application/json' } Method = 'Post' Body = $body } $response = Invoke-WebRequest @splat $response | Out-String | Write-Verbose $response.Content | ConvertFrom-Json } else { $splat = @{ Message = 'Invalid request: Please either specify ID & Type or an incoming webhook URI.' ErrorAction = 'Stop' } Write-Error @splat } } end { Write-Verbose -Message "Ending: '${function}'." } } |