<@ULVA73B9P> give me an example of sending MS Team...
# ask-marvin
b
@Marvin give me an example of sending MS Teams message from a flow using an existing webhook block
m
thought for 54.2 seconds
Yep — if you already have a saved
MicrosoftTeamsWebhook
block, you can load it in a flow and call
notify()
. Example:
Copy code
python
from prefect import flow
from prefect.blocks.notifications import MicrosoftTeamsWebhook


@flow
def send_teams_message():
    teams_webhook = MicrosoftTeamsWebhook.load("my-teams-webhook")

    teams_webhook.notify(
        body="Hello from Prefect! This message was sent from a flow.",
        subject="Prefect notification",
    )


if __name__ == "__main__":
    send_teams_message()
If you want to send it from inside a task:
Copy code
python
from prefect import flow, task
from prefect.blocks.notifications import MicrosoftTeamsWebhook


@task
def notify_teams(message: str):
    teams_webhook = MicrosoftTeamsWebhook.load("my-teams-webhook")
    teams_webhook.notify(
        body=message,
        subject="Task update",
    )


@flow
def my_flow():
    notify_teams("Pipeline finished successfully.")


if __name__ == "__main__":
    my_flow()
A few notes: - The block class is
MicrosoftTeamsWebhook
- Import path:
from prefect.blocks.notifications import MicrosoftTeamsWebhook
- The saved block name in these examples is
my-teams-webhook
— replace that with your actual block name - In Prefect 3.x, this block is part of core Prefect, so no extra integration package should be needed If you haven’t created the block yet, it looks like this:
Copy code
python
from prefect.blocks.notifications import MicrosoftTeamsWebhook

block = MicrosoftTeamsWebhook(
    url="<https://your-teams-workflow-url>"
)
block.save("my-teams-webhook", overwrite=True)
One important caveat: this block expects a Microsoft Workflows / Power Automate webhook URL, not the old-style Teams incoming webhook URL. Relevant references: - Prefect docs -
prefect.blocks.notifications.MicrosoftTeamsWebhook
in the Prefect notifications blocks API If you want, I can also give you: - an example that only sends on flow failure - an async version using
anotify()
- an example using variables like flow run name / state in the Teams message