Public/BlockElements/New-SlackButtonElement.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
function New-SlackButtonElement {
<#
.SYNOPSIS
    Returns a Slack Button PSCustomObject.
.DESCRIPTION
    This function returns a Slack Button as a PSCustomObject ready to ConvertTo-Json and be sent to Slack.
.PARAMETER text
    The text for the button. Can only be of type: plain_text.
.PARAMETER action_id
    An identifier for this action.
.PARAMETER url
    A URL to load in the user's browser when the button is clicked.
.PARAMETER value
    The value to send along with the interaction payload.
.PARAMETER style
    Decorates buttons with alternative visual color schemes.
.PARAMETER confirm
    A confirm object that defines an optional confirmation dialog that appears after a date is selected.
.LINK
    https://api.slack.com/reference/messaging/block-elements#button
.EXAMPLE
    Get-SlackButtonElement -text "Yes" -action_id "affirmThis"
.EXAMPLE
    Get-SlackButtonElement -text "No" -action_id "denyThis" -value "0001" -style "danger"
#>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateLength(1, 75)]
        [string]
        $text,

        [Parameter(Mandatory)]
        [ValidateLength(1, 255)]
        [string]
        $action_id,

        [ValidateLength(1, 3000)]
        [string]
        $url,

        [ValidateLength(1, 2000)]
        [string]
        $value,

        [ValidateSet("primary", "danger")]
        [string]
        $style,

        [pscustomobject]
        $confirm
    )

    $SlackButton = [pscustomobject]@{
        type      = "button"
        text      = New-SlackTextObject -type plain_text -text $text -emoji $true
        action_id = $action_id
    }

    if ($url) {
        $SlackButton | Add-Member -NotePropertyName "url" -NotePropertyValue $url
    }
    if ($value) {
        $SlackButton | Add-Member -NotePropertyName "value" -NotePropertyValue $value
    }
    if ($style) {
        $SlackButton | Add-Member -NotePropertyName "style" -NotePropertyValue $style
    }
    if ($confirm) {
        $SlackButton | Add-Member -NotePropertyName "confirm" -NotePropertyValue $confirm
    }

    return $SlackButton
}