slack
slack
¶
Slack notification implementation using httpx.
Provides Slack webhook notifications for Marianne job events. Messages are formatted with rich Slack Block Kit formatting.
Phase 5 of Marianne implementation: Missing README features.
Classes¶
SlackNotifier
¶
SlackNotifier(webhook_url=None, webhook_url_env='SLACK_WEBHOOK_URL', channel=None, username='Marianne AI Compose', icon_emoji=':musical_score:', events=None, timeout=10.0)
Slack notification implementation using webhooks.
Sends notifications to Slack channels via incoming webhooks. Supports rich formatting with Slack Block Kit attachments.
Example usage
notifier = SlackNotifier( webhook_url="https://hooks.slack.com/services/...", channel="#alerts", events={NotificationEvent.JOB_COMPLETE, NotificationEvent.JOB_FAILED}, ) await notifier.send(context)
Configuration from YAML
notifications: - type: slack on_events: [job_complete, job_failed, sheet_failed] config: webhook_url_env: SLACK_WEBHOOK_URL channel: "#marianne-alerts" username: "Marianne Bot" timeout: 10
Initialize the Slack notifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
webhook_url
|
str | None
|
Direct webhook URL. If not provided, reads from env var. |
None
|
webhook_url_env
|
str
|
Environment variable containing webhook URL. |
'SLACK_WEBHOOK_URL'
|
channel
|
str | None
|
Override channel (optional, uses webhook default if not set). |
None
|
username
|
str
|
Bot username displayed in Slack. |
'Marianne AI Compose'
|
icon_emoji
|
str
|
Emoji for bot avatar. |
':musical_score:'
|
events
|
set[NotificationEvent] | None
|
Set of events to subscribe to. Defaults to job-level events. |
None
|
timeout
|
float
|
HTTP request timeout in seconds. |
10.0
|
Source code in src/marianne/notifications/slack.py
Attributes¶
Functions¶
from_config
classmethod
¶
Create SlackNotifier from YAML configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
on_events
|
list[str]
|
List of event name strings from config. |
required |
config
|
dict[str, Any] | None
|
Optional dict with Slack-specific settings: - webhook_url: Direct webhook URL - webhook_url_env: Env var for webhook URL (default: SLACK_WEBHOOK_URL) - channel: Override channel - username: Bot username - icon_emoji: Bot avatar emoji - timeout: Request timeout in seconds |
None
|
Returns:
| Type | Description |
|---|---|
SlackNotifier
|
Configured SlackNotifier instance. |
Example
notifier = SlackNotifier.from_config( on_events=["job_complete", "job_failed"], config={ "webhook_url_env": "MY_SLACK_WEBHOOK", "channel": "#alerts", }, )
Source code in src/marianne/notifications/slack.py
send
async
¶
Send a Slack notification.
Posts to the configured Slack webhook with rich formatting. Fails gracefully if webhook is unavailable or request fails.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
NotificationContext
|
Notification context with event details. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if notification was sent, False if unavailable or failed. |
Source code in src/marianne/notifications/slack.py
close
async
¶
Clean up HTTP client resources.
Called when the NotificationManager is shutting down.
Source code in src/marianne/notifications/slack.py
MockSlackNotifier
¶
Mock Slack notifier for testing.
Records all notifications sent without making HTTP calls. Useful for testing notification integration.
Initialize mock notifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
set[NotificationEvent] | None
|
Set of events to subscribe to. |
None
|
Source code in src/marianne/notifications/slack.py
Attributes¶
Functions¶
set_fail_next
¶
Configure the next send() call to fail.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
should_fail
|
bool
|
If True, next send() returns False. |
True
|
send
async
¶
Record notification without making HTTP call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
NotificationContext
|
Notification context. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True unless set_fail_next was called. |
Source code in src/marianne/notifications/slack.py
close
async
¶
get_notification_count
¶
get_notifications_for_event
¶
Get all notifications for a specific event type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
NotificationEvent
|
Event type to filter by. |
required |
Returns:
| Type | Description |
|---|---|
list[NotificationContext]
|
List of matching notification contexts. |