<@ULVA73B9P> is it possible to trigger a flow run ...
# marvin-ai
s
@Marvin is it possible to trigger a flow run from a slack command in prefect-2?
m
âś… thought for 265.2 seconds
🙌 1
prefect rocket 1
Short answer: yes. In Prefect 2.x you can trigger a deployment run from a Slack slash command. There are two common approaches: 1) Prefect Cloud (no custom server): Slack → Prefect Webhook → Automation → Run Deployment - Create a Webhook in Prefect Cloud and copy its URL. - Configure the webhook’s Jinja template to map Slack’s form payload (e.g.,
text
,
user_name
,
channel_id
) into an event. - Create an Automation with an Event trigger that listens for your webhook event. - Add a Run Deployment action and (optionally) pass parameters using Jinja from the event payload. - Set your Slack command’s Request URL to the Prefect webhook URL. Notes: - Prefect Cloud webhooks + automations are Cloud-only in 2.x; they are not available in self-hosted Server. - Slack slash commands expect a 200 response within 3s. Prefect’s webhook will return 200, but you won’t get a pretty message back in Slack unless you build that yourself (see option 2). You can still use the Slack
response_url
from within your flow to post a follow-up message. - Security: Prefect webhooks do not verify Slack’s signing secret. Treat the webhook URL as a secret and consider filtering in your Automation (e.g., only react if
team_id
or
user_id
matches). - Docs: Webhooks, Automations, Events Example webhook template idea (maps Slack’s body fields):
Copy code
{
  "event": "slack.command",
  "resource": {
    "prefect.resource.id": "slack.command.{{ body.command }}",
    "user": "{{ body.user_name }}",
    "channel": "{{ body.channel_id }}",
    "text": "{{ body.text }}"
  }
}
Then in the Automation’s Run Deployment action, set parameters like:
Copy code
{
  "prompt": "{{ event.resource.text }}",
  "requested_by": "{{ event.resource.user }}"
}
2) Any environment (Cloud or Server): Small HTTP endpoint that calls the Prefect API/Client - Build a minimal FastAPI/Flask/Lambda endpoint as the Slack command target. - Verify Slack’s signature and respond quickly (so Slack shows a friendly response). - From that endpoint, create a flow run via the Prefect client or CLI. Python (async) client example for 2.x:
Copy code
import asyncio
from prefect.client.orchestration import get_client

async def trigger_run(deployment_id, params=None):
    async with get_client() as client:
        fr = await client.create_flow_run_from_deployment(
            deployment_id=deployment_id,
            parameters=params or {}
        )
        return fr.id

# asyncio.run(trigger_run("00000000-0000-0000-0000-000000000000", {"name": "world"}))
CLI alternative your service can shell out to:
Copy code
prefect deployment run "my-flow/my-deployment" -p name=world --watch
Verified command: run
prefect deployment run --help
shows parameters and usage. Questions to tailor a concrete setup for you: - Are you on Prefect Cloud or self-hosted Prefect Server? - Do you want to pass parameters from the Slack command (via the
text
input)? - Do you need a visible Slack confirmation message immediately after the command?