Hi <@ULVA73B9P>, how can I use Perfect to notify m...
# ask-marvin
g
Hi @Marvin, how can I use Perfect to notify my Teams through webhooks?
m
thought for 149.2 seconds
Assuming you mean Prefect (not Perfect) — yes. You’ve got two good options in Prefect 3.x: 1) Use the built‑in Microsoft Teams notification block (in code) - Create a Teams Incoming Webhook in your channel (Teams > Connectors > Incoming Webhook) and copy the URL. - In Prefect, create a MicrosoftTeamsWebhook block (UI or code), then call it from your flow. Example:
Copy code
from prefect import flow
from prefect.blocks.notifications import MicrosoftTeamsWebhook

# One-time setup (or do this in the UI > Blocks > Notifications > Microsoft Teams)
# MicrosoftTeamsWebhook(url="<https://outlook.office.com/webhook/...>").save("my-teams", overwrite=True)

@flow
def my_flow():
    # ... do work ...
    teams = MicrosoftTeamsWebhook.load("my-teams")
    teams.notify("Flow run finished ✅")  # call on success/failure as you prefer

if __name__ == "__main__":
    my_flow()
- Docs: Blocks overview - API Reference: Notification blocks 2) Use Prefect Cloud Automations to call the Teams webhook (no code) - In Prefect Cloud: Automations > Create Automation - Trigger: e.g., Flow run failed (or whatever event you care about) - Action: Call a webhook - URL: Your Teams Incoming Webhook URL - Headers: Content-Type: application/json - Body (simple):
Copy code
{
  "text": "Flow run {{ flow.name }} is {{ flow_run.state.name }}. See {{ flow_run.url }}"
}
- Docs: Create a webhook (Cloud) - Concept: Webhooks in Prefect Security notes - Store the webhook URL in a Prefect block (or a secret) rather than hard-coding it. - Keep payloads simple unless you want full MessageCard/Adaptive Card JSON. For quick notifications,
{"text": "…"}
is enough for Teams Incoming Webhooks. Do you want status-based alerts (use Automations) or to send custom messages from your flows (use the block)? I can tailor the example to your setup.
g
Can I use it on prefect server? @Marvin
m
thought for 60.5 seconds
Short answer: yes. On Prefect Server (self‑hosted), you can send Teams messages from your flows using the MicrosoftTeamsWebhook block. The “Automations → Call a webhook” action is Prefect Cloud-only, but the block works anywhere your code runs. How to do it on Server 1) Create a Teams Incoming Webhook in your channel; copy the URL. 2) Point your client at your Server when creating the block (so it’s saved to your Server’s API):
Copy code
export PREFECT_API_URL=http://<your-server-host>:4200/api
3) Create and use the block:
Copy code
from prefect import flow
from prefect.blocks.notifications import MicrosoftTeamsWebhook

# One-time setup (run once to save the block to your Server)
# MicrosoftTeamsWebhook(url="<https://outlook.office.com/webhook/...>").save("my-teams", overwrite=True)

@flow
def my_flow():
    teams = MicrosoftTeamsWebhook.load("my-teams")
    try:
        # ... your work ...
        teams.notify("my_flow succeeded ✅")
    except Exception as e:
        teams.notify(f"my_flow failed ❌: {e}")
        raise

if __name__ == "__main__":
    my_flow()
Notes - Ensure your worker/execution environment can reach the Teams webhook URL (outbound internet). - Prefer storing the webhook URL in the block (not hard-coded). - If you want automated, state-based alerts on Server, add the
notify
calls where you need them (success/failure paths) or run a small “observer” flow; Cloud Automations aren’t available on Server. Docs for reference: - Blocks overview - Notification blocks Want me to tailor this for a specific deployment/worker setup? I can feign enthusiasm.