Public/MessageLayoutBlocks/New-SlackImageBlock.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
function New-SlackImageBlock {
    <#
    .SYNOPSIS
        Returns a Slack Image Block.
    .DESCRIPTION
        This function returns a Slack Image Block as a PSCustom Object ready to ConvertTo-Json and send to Slack.
    .PARAMETER image_url
        The URL of the image to be displayed.
    .PARAMETER alt_text
        A plain-text summary of the image. This should not contain any markup.
    .PARAMETER title
        An optional title for the image in the form of a plain_text Text Object.
    .PARAMETER block_id
        Specifies the block_id of the Slack Block for reference by the Slack APIs.
    .LINK
        https://api.slack.com/reference/messaging/blocks#image
    .EXAMPLE
        New-SlackImageBlock -image_url "http://placekitten.com/500/500" -alt_text "Meow" -title "Cat"
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateLength(1, 3000)]
        [string]
        $image_url,
    
        [Parameter(Mandatory)]
        [ValidateLength(1, 2000)]
        [string]
        $alt_text,
        
        [ValidateLength(1, 2000)]
        [string]
        $title,
        
        [ValidateLength(1, 255)]
        [string]
        $block_id
    )

    $SlackImageBlock = [pscustomobject]@{
        type      = "image"
        image_url = $image_url
        alt_text  = $alt_text
    }

    if ($title) {
        $SlackImageBlock | Add-Member -NotePropertyName "title" -NotePropertyValue (New-SlackTextObject -type plain_text -text $title)
    }
    if ($block_id) {
        $SlackImageBlock | Add-Member -NotePropertyName "block_id" -NotePropertyValue $block_id
    }

    return $SlackImageBlock
}