PowerShellUniversal.Triggers.Slack.psm1

function Send-PSUSlackNotification {
    <#
    .SYNOPSIS
    Send a notification to Slack
     
    .DESCRIPTION
    Send a notification to Slack from triggers in PowerShell Universal.
     
    .PARAMETER Data
    The trigger data. Generated by PowerShell Universal.
 
    .PARAMETER Job
    The trigger data. Generated by PowerShell Universal.
    #>

    param(
        [Parameter(Mandatory = $true, ParameterSetName = "Data")]
        $Data,
        [Parameter(Mandatory = $true, ParameterSetName = "Job")]
        $Job
    )

    if ($SlackWebhookUrl -eq $null) {
        throw "Please create a variable called `SlackWebhookUrl` with the value of your Slack Webhook URL"
    }

    $Text = ""
    $Header = ""

    if ($Job) {
        $Text = Format-PSUJobDescription -Job $Job
        $Header = "[$($Job.Id)] $($Job.ScriptFullPath) $($Job.Status.ToString())"
    }

    if ($Text -eq "") {
        Write-Warning "Unknown trigger type."
        return
    }

    $SlackData = @{
        blocks = @(
            @{
                type = "header"
                text = @{
                    type = "plain_text"
                    text = $Header
                }
            }
            @{
                type      = "section"
                text      = @{
                    type = "mrkdwn"
                    text = $Text
                }
                accessory = @{
                    type      = "button"
                    text      = @{
                        type = "plain_text"
                        text = "View Job"
                    }
                    value     = "viewjob"
                    url       = "$ApiUrl/admin/automation/jobs/$($Job.Id)"
                    action_id = "button-action"
                }
            }
        )
    }

    Invoke-RestMethod -Uri $SlackWebhookUrl -Method Post -Body ($SlackData | ConvertTo-Json -Depth 10) -ContentType "application/json"

}