Noizu.Slack.Blocks (Noizu.Slack v0.1.0)

Copy Markdown View Source

Block Kit builders and limit validation. Builders return plain maps (atom keys) ready for Noizu.Slack.Chat.post_message/4's :blocks.

alias Noizu.Slack.Blocks

blocks = [
  Blocks.header("Deploy finished"),
  Blocks.section("*api* rolled out", fields: ["*Env*\nprod", "*SHA*\n`abc123`"]),
  Blocks.divider(),
  Blocks.context(["triggered by <@U123>"]),
  Blocks.actions([Blocks.button("Open", "open_run", url: "https://example.com")])
]

{:ok, blocks} = Blocks.validate(blocks)

Limits enforced by validate/1

  • at most 50 blocks per message — exceeding is an error;
  • section text ≤ 3000 chars, each section field ≤ 2000 chars, header text ≤ 150 chars — overlong text is truncated with a …(truncated) marker;
  • at most 10 fields per section — exceeding is an error.

Summary

Functions

An actions block of interactive elements.

A button element. Options: :value, :url, :style ("primary" / "danger").

A context block; strings become mrkdwn elements, maps pass through.

A divider block.

A list of mrkdwn text objects for a section's :fields.

A header block (plain text).

An image block.

Block limit per message (50).

Section text limit (3000 chars).

A mrkdwn text object.

A plain_text text object (emoji enabled).

A section block. text may be a string (mrkdwn) or a text object. Options: :fields (strings or text objects), :accessory (an element, e.g. button/3), :block_id.

Truncates text to max characters (graphemes), ending with the marker when cut.

Truncation marker appended to overlong text.

Validates (and normalizes) a block list against Slack's limits. Overlong text is truncated with the marker; structural violations return {:error, %Error{tag: :blocks}}. Works on atom- or string-keyed blocks.

Types

block()

@type block() :: map()

text_object()

@type text_object() :: %{type: String.t(), text: String.t()}

Functions

actions(elements)

@spec actions([map()]) :: block()

An actions block of interactive elements.

button(text, action_id, opts \\ [])

@spec button(String.t(), String.t(), keyword()) :: map()

A button element. Options: :value, :url, :style ("primary" / "danger").

context(elements)

@spec context([String.t() | map()]) :: block()

A context block; strings become mrkdwn elements, maps pass through.

divider()

@spec divider() :: block()

A divider block.

fields(list)

@spec fields([String.t() | map()]) :: [map()]

A list of mrkdwn text objects for a section's :fields.

header(text)

@spec header(String.t()) :: block()

A header block (plain text).

image(url, alt_text)

@spec image(String.t(), String.t()) :: block()

An image block.

max_blocks()

Block limit per message (50).

max_section_text()

Section text limit (3000 chars).

mrkdwn(text)

@spec mrkdwn(String.t()) :: text_object()

A mrkdwn text object.

plain_text(text)

@spec plain_text(String.t()) :: map()

A plain_text text object (emoji enabled).

section(text, opts \\ [])

@spec section(String.t() | map(), keyword()) :: block()

A section block. text may be a string (mrkdwn) or a text object. Options: :fields (strings or text objects), :accessory (an element, e.g. button/3), :block_id.

truncate(text, max)

@spec truncate(String.t(), pos_integer()) :: String.t()

Truncates text to max characters (graphemes), ending with the marker when cut.

truncation_marker()

Truncation marker appended to overlong text.

validate(blocks)

@spec validate([block()]) :: {:ok, [block()]} | {:error, Noizu.Slack.Error.t()}

Validates (and normalizes) a block list against Slack's limits. Overlong text is truncated with the marker; structural violations return {:error, %Error{tag: :blocks}}. Works on atom- or string-keyed blocks.