# Slack

#### Creating a Slack App

To send messages to Slack channels or users, you first need to create a Slack App and configure an **Incoming Webhook**.

{% stepper %}
{% step %}

### Create a new app

Go to the Slack API Apps page and click **Create New App → From scratch**.
{% endstep %}

{% step %}

### Name and workspace

Choose a name for the app and select the Slack workspace where it will be installed.
{% endstep %}

{% step %}

### Enable Incoming Webhooks

In the app settings, navigate to **Incoming Webhooks** and enable them.
{% endstep %}

{% step %}

### Add a webhook to a channel

Click **Add New Webhook to Workspace** and choose the channel you want the app to post to.
{% endstep %}

{% step %}

### Copy the webhook URL

Copy the **Webhook URL** — you’ll use this to send messages.
{% endstep %}
{% endstepper %}

For more information, refer to the official Slack Webhook documentation.

{% hint style="warning" %}
Keep the webhook URL secret — it authenticates requests to your workspace.
{% endhint %}

Example Webhook URL

```
https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
```

#### Notification Implementation

Once you have your webhook URL, you can post messages to Slack using a simple HTTP POST request.

```python
import polars as pl
import requests
import os
import json

SLACK_WEBHOOK_URL = os.environ["SLACK_WEBHOOK_URL"]

def send_slack_message(message, channel=None):
    payload = {
        "text": message
    }
    if channel:
        payload["channel"] = channel

    response = requests.post(
        SLACK_WEBHOOK_URL,
        data=json.dumps(payload),
        headers={"Content-Type": "application/json"}
    )
    print(response.text)
    return response.json()


def transform():
    text = """
    ⚠️ *New Alert!*
    """
    response = send_slack_message(text)
    return pl.LazyFrame(response)
```

#### Example Message Formatting

<details>

<summary>Formatting and examples</summary>

Slack supports basic formatting using Markdown-like syntax:

* `*bold*` → **bold**
* `_italic_` → *italic*
* `` `code` `` → `code`
* `<https://example.com|Custom Link>` → Custom Link

You can also include emojis and attachments for richer notifications.

</details>
