Public/MessageLayoutBlocks/New-SlackSectionBlock.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
function New-SlackSectionBlock {
    <#
    .SYNOPSIS
        Returns a Slack Section Block PSCustomObject
    .DESCRIPTION
        This function returns a Slack Section Block as a PSCustomObject ready to ConvertTo-Json and send to Slack.
    .PARAMETER text
        The top level text of the Slack Block.
    .PARAMETER block_id
        Specifies the block_id of the Slack Block for reference by the Slack APIs.
    .PARAMETER fields
        An array of SlackTextObjects.
    .PARAMETER accessory
        A Slack Block element object
    .LINK
        https://api.slack.com/reference/messaging/blocks#section
    .EXAMPLE
        Get-SlackSectionBlock -text "The Text for this block." -block_id "123ABC"
    #>

    [cmdletbinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateLength(1,3000)]
        [string]
        $text,

        [ValidateLength(1,255)]
        [string]
        $block_id,

        [ValidateCount(1,10)]
        [PSCustomObject[]]
        $fields,

        [pscustomobject]
        $accessory
    )

    $SlackSectionBlock = [pscustomobject]@{
        type = "section"
        text = New-SlackTextObject -type "mrkdwn" -text $text
    }
    if($block_id){
        $SlackSectionBlock | Add-Member -NotePropertyName "block_id" -NotePropertyValue $block_id
    }
    if($fields){
        $SlackSectionBlock | Add-Member -NotePropertyName "fields" -NotePropertyValue $fields
    }
    if($accessory){
        $SlackSectionBlock | Add-Member -NotePropertyName "accessory" -NotePropertyValue $accessory
    }

    return $SlackSectionBlock
}