Skip to content

Sending Messages

django_slack_tools.slack_messages.message.slack_message(body, *, channel, header=None, raise_exception=False, get_permalink=False)

Send a simple text message.

Parameters:

Name Type Description Default
body str | MessageBody | dict[str, Any]

Message content, simple message or full request body.

required
channel str

Channel to send message.

required
header MessageHeader | dict[str, Any] | None

Slack message control header.

None
raise_exception bool

Whether to re-raise caught exception while sending messages.

False
get_permalink bool

Try to get the message permalink via extraneous Slack API calls.

False

Returns:

Type Description
SlackMessage | None

Sent message instance or None.

Source code in django_slack_tools/slack_messages/message.py
def slack_message(
    body: str | MessageBody | dict[str, Any],
    *,
    channel: str,
    header: MessageHeader | dict[str, Any] | None = None,
    raise_exception: bool = False,
    get_permalink: bool = False,
) -> SlackMessage | None:
    """Send a simple text message.

    Args:
        body: Message content, simple message or full request body.
        channel: Channel to send message.
        header: Slack message control header.
        raise_exception: Whether to re-raise caught exception while sending messages.
        get_permalink: Try to get the message permalink via extraneous Slack API calls.

    Returns:
        Sent message instance or `None`.
    """
    # If body is just an string, make a simple message body
    body = MessageBody(text=body) if isinstance(body, str) else MessageBody.model_validate(body)
    header = MessageHeader.model_validate(header or {})

    return app_settings.backend.send_message(
        channel=channel,
        header=header,
        body=body,
        raise_exception=raise_exception,
        get_permalink=get_permalink,
    )

django_slack_tools.slack_messages.message.slack_message_via_policy(policy, *, header=None, raise_exception=False, lazy=False, get_permalink=False, context=None)

Send a simple text message.

Mentions for each recipient will be passed to template as keyword {mentions}. Template should include it to use mentions.

Parameters:

Name Type Description Default
policy str | SlackMessagingPolicy

Messaging policy code or policy instance.

required
header MessageHeader | dict[str, Any] | None

Slack message control header.

None
raise_exception bool

Whether to re-raise caught exception while sending messages.

False
lazy bool

Decide whether try create policy with disabled, if not exists.

False
get_permalink bool

Try to get the message permalink via extraneous Slack API calls.

False
context dict[str, Any] | None

Dictionary to pass to template for rendering.

None

Returns:

Type Description
list[SlackMessage | None]

Sent message instance or None.

Raises:

Type Description
DoesNotExist

Policy for given code does not exists.

Source code in django_slack_tools/slack_messages/message.py
def slack_message_via_policy(  # noqa: PLR0913
    policy: str | SlackMessagingPolicy,
    *,
    header: MessageHeader | dict[str, Any] | None = None,
    raise_exception: bool = False,
    lazy: bool = False,
    get_permalink: bool = False,
    context: dict[str, Any] | None = None,
) -> list[SlackMessage | None]:
    """Send a simple text message.

    Mentions for each recipient will be passed to template as keyword `{mentions}`.
    Template should include it to use mentions.

    Args:
        policy: Messaging policy code or policy instance.
        header: Slack message control header.
        raise_exception: Whether to re-raise caught exception while sending messages.
        lazy: Decide whether try create policy with disabled, if not exists.
        get_permalink: Try to get the message permalink via extraneous Slack API calls.
        context: Dictionary to pass to template for rendering.

    Returns:
        Sent message instance or `None`.

    Raises:
        SlackMessagingPolicy.DoesNotExist: Policy for given code does not exists.
    """
    if isinstance(policy, str):
        if lazy:
            policy, created = SlackMessagingPolicy.objects.get_or_create(code=policy, defaults={"enabled": False})
            if created:
                logger.warning("Policy for code %r created because `lazy` is set.", policy)
        else:
            policy = SlackMessagingPolicy.objects.get(code=policy)

    if not policy.enabled:
        return []

    header = MessageHeader.model_validate(header or {})
    context = context or {}

    # Prepare template
    template = policy.template
    overridden_reserved = {"mentions", "mentions_as_str"} & set(context.keys())
    if overridden_reserved:
        logger.warning(
            "Template keyword argument(s) %s reserved for passing mentions, but already exists."
            " User provided value will override it.",
            ", ".join(f"`{s}`" for s in overridden_reserved),
        )

    messages: list[SlackMessage | None] = []
    for recipient in policy.recipients.all():
        # Auto-generated reserved kwargs
        mentions: list[SlackMention] = list(recipient.mentions.all())
        mentions_as_str = ", ".join(mention.mention for mention in mentions)

        # Prepare rendering arguments
        kwargs = {"mentions": mentions, "mentions_as_str": mentions_as_str}
        kwargs.update(context)

        # Render and send message
        rendered = render(template, **kwargs)
        body = MessageBody.model_validate(rendered)
        message = app_settings.backend.send_message(
            policy=policy,
            channel=recipient.channel,
            header=header,
            body=body,
            raise_exception=raise_exception,
            get_permalink=get_permalink,
        )
        messages.append(message)

    return messages