ps/Modules/Alkami.DevOps.Common/Public/Get-SlackAttachment.ps1
2023-05-30 22:51:22 -07:00

87 lines
2.7 KiB
PowerShell

function Get-SlackAttachment {
<#
.SYNOPSIS
Used to create an attachment for a Slack Message that adds further context or additional information.
.DESCRIPTION
Content that can be attached to messages to include lower priority content - content that doesn't necessarily need to be seen to appreciate the intent of the message.
.PARAMETER Fallback
A plain text summary of the attachment used in clients that don't show formatted text (eg. IRC, mobile notifications).
The top-level text field from the message payload.
.PARAMETER Color
The color showcased in the message to show status.
.PARAMETER Fields
An array of field objects that get displayed in a table-like way. For best results, include no more than 2-3 field objects.
.PARAMETER Text
The main body text of the attachment.
.PARAMETER Timestamp
The attachment will display the additional timestamp value as part of the attachment's footer.
Your message's timestamp will be displayed in varying ways, depending on how far in the past or future it is, relative to the present. Form factors, like mobile versus desktop may also transform its rendered appearance.
.PARAMETER Actions
A set of actions to be taken for this attachment.
.PARAMETER ActionText
The text for an attachment.
.PARAMETER ActionUrl
The URL for an attachment.
#>
[CmdletBinding()]
[OutputType([object])]
param (
[Parameter(Mandatory = $true)]
[string]$Fallback,
[Parameter(Mandatory = $false)]
[string]$Color = $null,
[Parameter(Mandatory = $false)]
[string]$Text = $null,
[Parameter(Mandatory = $false)]
[DateTime]$Timestamp = (Get-Date),
[Parameter(Mandatory = $false)]
[object[]]$Fields = $null,
[Parameter(ParameterSetName = 'Actions')]
[object[]]$Actions = $null,
[Parameter(Mandatory = $true, ParameterSetName = 'ActionPair')]
[string]$ActionText,
[Parameter(Mandatory = $true, ParameterSetName = 'ActionPair')]
[string]$ActionUrl
)
$messageColor = Get-SlackMessageColor -Text $Color
Write-Verbose "Message Color: $messageColor"
$attachment = @{
fallback = $Fallback
color = $messageColor
ts = [Math]::Floor([decimal](Get-Date($Timestamp).ToUniversalTime() -uformat "%s"))
}
if ($null -ne $Fields) {
$attachment.fields = $Fields
}
if (![string]::IsNullOrWhiteSpace($Text)) {
$attachment.text = $Text
}
if ($PSCmdlet.ParameterSetName -eq 'ActionPair') {
$Actions = Get-SlackAction -Text $ActionText -Url $ActionUrl
}
if ($null -ne $Actions) {
$attachment.actions = @($Actions)
}
return $attachment
}