Public/Chat/Send-SlackMessage.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
function Send-SlackMessage {
    <#
    .SYNOPSIS
        Sends a chat message to Slack
    .DESCRIPTION
        This function will send a message to a designated Slack Channel, public, private, or direct messages.
        Blocks, attachments, or text can be passed in. Requires a Slack Token with appropriate scopes.
    .PARAMETER token
        Authentication token bearing required scopes.
    .PARAMETER channel
        Channel, private group, or IM channel to send message to. Can be an encoded ID, or a name.
    .PARAMETER text
        Text of the message to send. See below for an explanation of formatting. This field is usually required, unless you're providing only attachments instead.
    .PARAMETER as_user
        Pass true to post the message as the authed user, instead of as a bot. Defaults to false.
    .PARAMETER attachments
        An array of structured attachments
    .PARAMETER blocks
        An array of structured blocks
    .PARAMETER icon_emoji
        Emoji to use as the icon for this message. Overrides icon_url. Must be used in conjunction with as_user set to false, otherwise ignored.
    .PARAMETER icon_url
        URL to an image to use as the icon for this message. Must be used in conjunction with as_user set to false, otherwise ignored.
    .PARAMETER link_names
        Find and link channel names and usernames.
    .PARAMETER mrkdwn
        Disable Slack markup parsing by setting to false. Enabled by default.
    .PARAMETER parse
        Change how messages are treated. Defaults to none.
    .PARAMETER reply_broadcast
        Used in conjunction with thread_ts and indicates whether reply should be made visible to everyone in the channel or conversation. Defaults to false.
    .PARAMETER thread_ts
        Provide another message's ts value to make this message a reply. Avoid using a reply's ts value; use its parent instead.
    .PARAMETER unfurl_links
        Pass true to enable unfurling of primarily text-based content.
    .PARAMETER unfurl_media
        Pass false to disable unfurling of media content.
    .PARAMETER username
        Set your bot's user name. Must be used in conjunction with as_user set to false, otherwise ignored.
    .LINK
        https://api.slack.com/methods/chat.postMessage
    .EXAMPLE
        Send-SlackMessage -token '' -channel '#mychannel' -text 'Hello there.'
    #>

    [cmdletbinding()]
    param(
        [string]
        $token,
    
        [string]
        $channel,

        [ValidateLength(1, 4000)]
        [string]
        $text = "no text set",

        [switch]
        $as_user,

        [pscustomobject[]]
        $attachments,

        [pscustomobject[]]
        $blocks,

        [ValidateScript( { $_.StartsWith(":") -and $_.EndsWith(":") })]
        [string]
        $icon_emoji,

        [string]
        $icon_url,

        [switch]
        $link_names,

        [bool]
        $mrkdwn = $true,

        [string]
        $parse = "none",

        [switch]
        $reply_broadcast,

        [string]
        $thread_ts,

        [switch]
        $unfurl_links,

        [bool]
        $unfurl_media = $true,

        [string]
        $username
    )

    $root = (Get-Item $PSScriptRoot).Parent.Parent.FullName

    if(!$env:GITHUB_REPOSITORY){
        if (!$token) {
            $content = Get-Content "$root\pslickpslackconfig.json" | ConvertFrom-Json
            $token = $content.userToken
        }
        if (!$channel) {
            $content = Get-Content "$root\pslickpslackconfig.json" | ConvertFrom-Json
            $channel = $content.defaultChannel
        }
    }
    else {
        $token = $env:SLACK_TOKEN
        $channel = $env:SLACK_CHANNEL
    }
     
    $Headers = @{
        Authorization = "Bearer $token"
    }

    # Construct the Body based on parameters
    $Body = [PSCustomObject]@{
        channel = $channel
        text    = $text
    }



    if ($as_user -and !$username) {
        $Body | Add-Member -NotePropertyName "as_user" -NotePropertyValue $true
    }
    elseif ($as_user -and $username) {
        Write-Error "as_user cannot be set with a username."
    }

    if ($attachments) {
        $Body | Add-Member -NotePropertyName "attachments" -NotePropertyValue $attachments
    }

    if ($blocks) {
        $Body | Add-Member -NotePropertyName "blocks" -NotePropertyValue $blocks
    }

    if ($icon_emoji -and !$as_user) {
        $Body | Add-Member -NotePropertyName "icon_emoji" -NotePropertyValue $icon_emoji
    }
    elseif ($icon_emoji -and $as_user) {
        Write-Error "icon_emoji cannot be used with as_user set to true"
    }

    if ($icon_url -and !$as_user) {
        $Body | Add-Member -NotePropertyName "icon_url" -NotePropertyValue $icon_url
    }
    elseif ($icon_url -and $as_user) {
        Write-Error
    }

    if ($username -and !$as_user) {
        $Body | Add-Member -NotePropertyName "username" -NotePropertyValue $username
    }
    elseif ($username -and $as_user) {
        Write-Error "Username cannot be set with as_user set to true."
    }

    Invoke-RestMethod -Method Post -Uri 'https://slack.com/api/chat.postMessage' -Headers $Headers -ContentType 'application/json;charset=iso-8859-1' -Body ($Body | ConvertTo-Json -Depth 100)
}