Public/Send-SlackAPI.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
function Send-SlackApi
{
    <#
    .SYNOPSIS
        Send a message to the Slack API endpoint
 
    .DESCRIPTION
        Send a message to the Slack API endpoint
 
        This function is used by other PSSlack functions.
        It's a simple wrapper you could use for calls to the Slack API
 
    .PARAMETER Method
        Slack API method to call.
 
        Reference: https://api.slack.com/methods
 
    .PARAMETER Body
        Hash table of arguments to send to the Slack API.
 
    .PARAMETER Token
        Slack token to use
 
    .PARAMETER Proxy
        Proxy server to use
 
    .FUNCTIONALITY
        Slack
    #>

    [OutputType([String])]
    [cmdletbinding()]
    param (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$Method,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [hashtable]$Body = @{ },

        [ValidateNotNullOrEmpty()]
        [ValidateScript({
            if (-not $_ -and -not $Script:PSSlack.Token)
            {
                throw 'Please supply a Slack Api Token with Set-SlackApiToken.'
            }
            else
            {
                $true
            }
        })]
        [string]$Token = $Script:PSSlack.Token,

        [string]$Proxy = $Script:PSSlack.Proxy
    )
    $Params = @{
        Uri = "https://slack.com/api/$Method"
        ErrorAction = 'Stop'
    }
    if($Proxy) {
        $Params['Proxy'] = $Proxy
    }
    $Body.token = $Token

    try {
        $Response = $null
        $Response = Invoke-RestMethod @Params -Body $Body
    }
    catch {
        # (HTTP 429 is "Too Many Requests")
        if ($_.Exception.Response.StatusCode -eq 429) {

            # Get the time before we can try again.
            if( $_.Exception.Response.Headers -and $_.Exception.Response.Headers.Contains('Retry-After') ) {
                $RetryPeriod = $_.Exception.Response.Headers.GetValues('Retry-After')
                if($RetryPeriod -is [string[]]) {
                    $RetryPeriod = [int]$RetryPeriod[0]
                }
            }
            else {
                $RetryPeriod = 2
            }
            Write-Verbose "Sleeping [$RetryPeriod] seconds due to Slack 429 response"
            Start-Sleep -Seconds $RetryPeriod
            Send-SlackApi @PSBoundParameters

        }
        elseif ($_.ErrorDetails.Message -ne $null) {
            # Convert the error-message to an object. (Invoke-RestMethod will not return data by-default if a 4xx/5xx status code is generated.)
            $_.ErrorDetails.Message | ConvertFrom-Json | Parse-SlackError -Exception $_.Exception -ErrorAction Stop

        }
        else {
            Write-Error -Exception $_.Exception -Message "Slack API call failed: $_"
        }
    }

    # Check to see if we have confirmation that our API call failed.
    # (Responses with exception-generating status codes are handled in the "catch" block above - this one is for errors that don't generate exceptions)
    if ($Response -ne $null -and $Response.ok -eq $False) {
        $Response | Parse-SlackError
    }
    elseif($Response) {
        Write-Output $Response
    }
    else {
        Write-Verbose "Something went wrong. `$Response is `$null"
    }
}