prefect_slack.messages
Tasks for sending Slack messages.
send_chat_message(channel, slack_credentials, text=None, attachments=None, slack_blocks=None)
async
Sends a message to a Slack channel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
channel
|
str
|
The name of the channel in which to post the chat message (e.g. #general). |
required |
slack_credentials
|
SlackCredentials
|
Instance of |
required |
text
|
Optional[str]
|
Contents of the message. It's a best practice to always provide a |
None
|
attachments
|
Optional[Sequence[Union[Dict, Attachment]]]
|
List of objects defining secondary context in the posted Slack message. The Slack API docs provide guidance on building attachments. |
None
|
slack_blocks
|
Optional[Sequence[Union[Dict, Block]]]
|
List of objects defining the layout and formatting of the posted message. The Slack API docs provide guidance on building messages with blocks. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Dict |
Dict
|
Response from the Slack API. Example response structures can be found in the Slack API docs. |
Example
Post a message at the end of a flow run.
from prefect import flow
from prefect.context import get_run_context
from prefect_slack import SlackCredentials
from prefect_slack.messages import send_chat_message
@flow
def example_send_message_flow():
context = get_run_context()
# Run other tasks and subflows here
token = "xoxb-your-bot-token-here"
send_chat_message(
slack_credentials=SlackCredentials(token),
channel="#prefect",
text=f"Flow run {context.flow_run.name} completed :tada:"
)
example_send_message_flow()
Source code in prefect_slack/messages.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
|
send_incoming_webhook_message(slack_webhook, text=None, attachments=None, slack_blocks=None)
async
Sends a message via an incoming webhook.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
slack_webhook
|
SlackWebhook
|
Instance of |
required |
text
|
Optional[str]
|
Contents of the message. It's a best practice to always provide a |
None
|
attachments
|
Optional[Sequence[Union[Dict, Attachment]]]
|
List of objects defining secondary context in the posted Slack message. The Slack API docs provide guidance on building attachments. |
None
|
slack_blocks
|
Optional[Sequence[Union[Dict, Block]]]
|
List of objects defining the layout and formatting of the posted message. The Slack API docs provide guidance on building messages with blocks. |
None
|
Example
Post a message at the end of a flow run.
from prefect import flow
from prefect_slack import SlackWebhook
from prefect_slack.messages import send_incoming_webhook_message
@flow
def example_send_message_flow():
# Run other tasks and subflows here
webhook_url = "https://hooks.slack.com/XXX"
send_incoming_webhook_message(
slack_webhook=SlackWebhook(
url=webhook_url
),
text="Warehouse loading flow completed :sparkles:"
)
example_send_message_flow()
Source code in prefect_slack/messages.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
|