Can I define a slack incoming webhook block in cod...
# ask-community
l
Can I define a slack incoming webhook block in code?
Copy code
"""Incoming Webhook Block for #prefect-alerts Slack Channel."""
import os

from prefect.blocks.notifications import SlackWebhook


class PrefectAlerts(SlackWebhook):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._block_document_name = "prefect-alerts-incoming-webhook"
    
    url = os.getenv("PREFECT_INCOMING_WEBHOOK__PREFECT_ALERTS")
is this how i would give it a name?
n
hi @Leon Kozlowski - a couple things to note • yes you can define blocks in code • you don't need to subclass anything if you don't want a new type of block ◦ in general if you do actually want your own type, i wouldn't override dunder init for blocks unless you know you need to, because they are pydantic models and they do special dataclass type things in the init • to simply use a slack-webhook block programmatically you can take the suggestion from the UI
l
got it - this is what i have right now
Copy code
"""Incoming Webhook Block for #prefect-alerts Slack Channel."""
import os

from prefect_slack import SlackWebhook


if __name__ == "__main__":
    SlackWebhook(
        url=os.getenv("PREFECT_INCOMING_WEBHOOK__PREFECT_ALERTS")
    ).save(name="incoming-webhook-prefect-alerts")
block^
Copy code
from prefect.automations import Automation
from prefect.events.schemas.automations import EventTrigger
from prefect_slack import SlackWebhook

if __name__ == "__main__":
    webhook = SlackWebhook.load(name="incoming-webhook-prefect-alerts")
    automation = Automation(
        name="failure-prefect-alerts",
        trigger=EventTrigger(
            expect={
                "prefect.flow-run.Failed",
            },
            match={
                "prefect.resource.id": "prefect.flow-run.*"
            },
            posture="Reactive",
            for_each={
                "prefect.resource.id"
            },
            threshold=1,
            within=0,
        ),
        actions=[webhook]
    ).create()
automation^
can i set the subject and message somewhere in there?
similar to this?
n
so
actions
needs to take a known / defined action, like
RunDeployment
/
PauseDeployment
or
SendNotification
l
got it
thank you
👍 1
n
you'll want to import and provide the client version of this , which cloud will support