SlackMessage

pydantic model safir.slack.blockkit.SlackMessage

Message to post to Slack.

The message attribute will be the initial part of the message.

All fields in fields will be shown below that message, formatted in two columns. Order of fields is preserved; they will be laid out left to right and then top to bottom in the order given. Then, blocks will be added, if any, in one column below the fields. Finally, attachments will be added to the end as attachments, which get somewhat different formatting (for example, long attachments are collapsed by default).

At most ten elements are allowed in fields. They should be used for short information, generally a single half-line at most. Longer information should go into blocks or attachments.

Parameters:

data (Any)

Show JSON schema
{
   "title": "SlackMessage",
   "description": "Message to post to Slack.\n\nThe ``message`` attribute will be the initial part of the message.\n\nAll fields in ``fields`` will be shown below that message, formatted in\ntwo columns. Order of ``fields`` is preserved; they will be laid out left\nto right and then top to bottom in the order given. Then, ``blocks`` will\nbe added, if any, in one column below the fields. Finally, ``attachments``\nwill be added to the end as attachments, which get somewhat different\nformatting (for example, long attachments are collapsed by default).\n\nAt most ten elements are allowed in ``fields``. They should be used for\nshort information, generally a single half-line at most.  Longer\ninformation should go into ``blocks`` or ``attachments``.",
   "type": "object",
   "properties": {
      "message": {
         "title": "Message",
         "type": "string"
      },
      "verbatim": {
         "default": true,
         "title": "Verbatim",
         "type": "boolean"
      },
      "fields": {
         "default": [],
         "items": {
            "$ref": "#/$defs/SlackBaseField"
         },
         "title": "Fields",
         "type": "array"
      },
      "blocks": {
         "default": [],
         "items": {
            "$ref": "#/$defs/SlackBaseBlock"
         },
         "title": "Blocks",
         "type": "array"
      },
      "attachments": {
         "default": [],
         "items": {
            "$ref": "#/$defs/SlackBaseBlock"
         },
         "title": "Attachments",
         "type": "array"
      }
   },
   "$defs": {
      "SlackBaseBlock": {
         "description": "Base class for any Slack Block Kit block.",
         "properties": {},
         "title": "SlackBaseBlock",
         "type": "object"
      },
      "SlackBaseField": {
         "description": "Base class for Slack Block Kit blocks for the ``fields`` section.",
         "properties": {},
         "title": "SlackBaseField",
         "type": "object"
      }
   },
   "required": [
      "message"
   ]
}

Fields:
Validators:
field attachments: list[SlackBaseBlock] = []

Longer sections to include as attachments.

Notes

Slack has marked attachments as legacy and warns that future changes may reduce their visibility or utility. Unfortunately, there is no other way to attach possibly-long text where Slack will hide long content by default but allow the user to expand it. We therefore continue to use attachments for long text for want of a better alternative.

field blocks: list[SlackBaseBlock] = []

Additional text blocks to include in the message (after fields).

field fields: list[SlackBaseField] = []

Short key/value fields to include in the message (at most 10).

Validated by:
  • _validate_fields

field message: str [Required]

Main part of the message.

field verbatim: bool = True

Whether the main part of the message should be marked verbatim.

Verbatim messages in Slack don’t expand channel references or create user notifications. This is the default, but can be set to False to allow any such elements in the message to be recognized by Slack. Do not set this to False with untrusted input.

to_slack()

Convert to a Slack Block Kit message.

Returns:

A Slack Block Kit data structure suitable for serializing to JSON and sending to Slack.

Return type:

dict