Public/BlockElements/New-SlackDatePickerElement.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
function New-SlackDatePickerElement {
    <#
    .SYNOPSIS
        Returns a Slack Date Picker.
    .DESCRIPTION
        This function returns a Slack Date Picker Element as a PSCustomObject ready to combine with other Slack Blocks,
        ConvertTo-Json, and send to Slack.
    .PARAMETER action_id
        An identifier for the action triggered when a menu option is selected.
    .PARAMETER placeholder
        Text that defines the placeholder text shown on the datepicker.
    .PARAMETER initial_date
        The initial date that is selected when the element is loaded. This should be in the format YYYY-MM-DD.
    .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#datepicker
    .EXAMPLE
        New-SlackDatePickerElement -action_id "date789" -placeholder "05/04/1971" -initial_date $dateTime
    #>

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

        [ValidateLength(1, 150)]
        [string]
        $placeholder,

        [string] #add checks for datetime format. I tried but took too long.
        $initial_date,

        [pscustomobject]
        $confirm
    )

    $SlackDatePicker = [pscustomobject]@{
        type       = "datepicker"
        action_id  = $action_id
    }

    if($placeholder){
        $SlackDatePicker | Add-Member -NotePropertyName "placeholder" -NotePropertyValue $placeholder
    }
    if ($initial_date) {
        $SlackDatePicker | Add-Member -NotePropertyName "initial_date" -NotePropertyValue $initial_date
    }
    if ($confirm) {
        $SlackDatePicker | Add-Member -NotePropertyName "confirm" -NotePropertyValue $confirm
    }

    return $SlackDatePicker
}