# Slack

## Send Message to Slack

This example uses [Universal API](https://docs.powershelluniversal.com/v1/api/about).

This example uses a custom Slack webhook to send a message from a Universal API.

```
Start-PSUServer -Port 8080 -Configuration {
    New-PSUEndpoint -Url "/slack" -Method POST -Endpoint {

        $Body = @{
            text = "Hello"
        } | ConvertTo-Json

        Invoke-RestMethod -Uri 'https://hooks.slack.com/services/0000000000/00000000/00000000000000000' -Body $Body -Method POST
    }
}
```

You can invoke this API by calling `Invoke-RestMethod`

```
Invoke-RestMethod http://localhost:8080/slack -Method POST
```

The following message will show up in Slack.

![](https://2374445323-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6jY7sXTmhiAIMGYw_m%2F-MN1HBwhgFufJnSUmjVD%2F-MN1HaK3yhTgG3dF7_Hu%2Fimage.png?alt=media\&token=5d97a78f-b6b6-42ad-99a4-6ac2ea89c7c8)

## Send Slack Message on Failed Job

This following example uses [Universal Automation](https://docs.powershelluniversal.com/v1/automation/about). This example requires a [license](https://docs.powershelluniversal.com/v1/get-started/licensing).

This example takes advantage of triggers to send a message to Slack when a job fails within PowerShell Universal. We define two scripts. The first script simply throws and error and is set to fail by using the `-ErrorAction Stop` setting. The second script receives the job that failed and sends a message to the team's Slack channel.

```
Start-PSUServer -Port 8080 -Configuration {

    Set-PSULicense -Key 'license'

    New-PSUScript -Name 'ScriptFailed' -ScriptBlock {
        $Body = @{
            text = "Job $($Job.Id) failed!!"
        } | ConvertTo-Json

        Invoke-RestMethod -Uri 'https://hooks.slack.com/services/00000000/00000000/000000000000000' -Body $Body -Method POST
    }

    New-PSUScript -Name 'FailingScript' -ScriptBlock {
        throw "NoooooO!"
    } -ErrorAction Stop

    New-PSUTrigger -Name 'ScriptFailed' -TriggerScript 'ScriptFailed.ps1' -EventType 'JobFailed'
}
```

When the failing script is running, it will report failure in the UI.

![](https://2374445323-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6jY7sXTmhiAIMGYw_m%2F-MN1ID5OrA7J7ItrzMQp%2F-MN1L3SUCaa-voM1NOxg%2Fimage.png?alt=media\&token=5faa1c14-2538-4aa2-a395-d401678994b6)

Due to the failure, the trigger will execute and send a message to Slack.

![](https://2374445323-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6jY7sXTmhiAIMGYw_m%2F-MN1ID5OrA7J7ItrzMQp%2F-MN1LCWI-ww2LQ8knA0R%2Fimage.png?alt=media\&token=79190680-53b5-431a-b451-b7b0717a7d12)
