# Slack

## Send Message to Slack

This example uses [Universal API](/v1/api/about.md).

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.

![](/files/-MN1HaK3yhTgG3dF7_Hu)

## Send Slack Message on Failed Job

This following example uses [Universal Automation](/v1/automation/about.md). This example requires a [license](/v1/get-started/licensing.md).

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.

![](/files/-MN1L3SUCaa-voM1NOxg)

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

![](/files/-MN1LCWI-ww2LQ8knA0R)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.powershelluniversal.com/v1/examples/slack.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
