def create_case_message()

in src/dispatch/plugins/dispatch_slack/case/messages.py [0:0]


def create_case_message(case: Case, channel_id: str) -> list[Block]:
    """
    Creates a Slack message for a given case.

    Args:
        case (Case): The case object containing details to be included in the message.
        channel_id (str): The ID of the Slack channel where the message will be sent.

    Returns:
        list[Block]: A list of Block objects representing the structure of the Slack message.
    """
    priority_color = map_priority_color(color=case.case_priority.color)

    title_prefix = "*Detection*" if case.signal_instances else "*Title*"
    title = f"{title_prefix} \n {case.title}."

    fields = [
        f"*Assignee* \n {case.assignee.individual.email}",
        f"*Status* \n {case.status}",
        f"*Case Type* \n {case.case_type.name}",
        f"*Case Priority* \n {priority_color} {case.case_priority.name}",
    ]

    if case.signal_instances:
        if variant := case.signal_instances[0].signal.variant:
            fields.append(f"*Variant* \n {variant}")

    case_description = (
        case.description if len(case.description) <= 2500 else f"{case.description[:2500]}..."
    )

    blocks = [
        Context(elements=[MarkdownText(text=f"* {case.name} - Case Details*")]),
        Section(
            text=title,
            accessory=Button(
                text="View in Dispatch",
                action_id="button-link",
                url=f"{DISPATCH_UI_URL}/{case.project.organization.slug}/cases/{case.name}",
            ),
        ),
        Section(text=f"*Description* \n {case_description}"),
        Section(fields=fields),
        Section(text="*Actions*"),
    ]

    button_metadata = SubjectMetadata(
        type=CaseSubjects.case,
        organization_slug=case.project.organization.slug,
        id=case.id,
        project_id=case.project.id,
        channel_id=channel_id,
    ).json()

    if case.has_channel:
        action_buttons = [
            Button(
                text=":slack: Case Channel",
                style="primary",
                url=case.conversation.weblink if case.conversation else "",
            )
        ]
        blocks.extend([Actions(elements=action_buttons)])
    elif case.status == CaseStatus.escalated:
        blocks.extend(
            [
                Actions(
                    elements=[
                        Button(
                            text=":siren: Join Incident",
                            action_id=CaseNotificationActions.join_incident,
                            style="primary",
                            value=button_metadata,
                        )
                    ]
                )
            ]
        )
    elif case.status == CaseStatus.closed:
        blocks.extend(
            [
                Section(text=f"*Resolution reason* \n {case.resolution_reason}"),
                Section(
                    text=f"*Resolution description* \n {case.resolution}"[:MAX_SECTION_TEXT_LENGTH]
                ),
                Actions(
                    elements=[
                        Button(
                            text="Re-open",
                            action_id=CaseNotificationActions.reopen,
                            style="primary",
                            value=button_metadata,
                        )
                    ]
                ),
            ]
        )
    else:
        action_buttons = [
            Button(
                text=":white_check_mark: Resolve",
                action_id=CaseNotificationActions.resolve,
                value=button_metadata,
            ),
            Button(
                text=":pencil: Edit",
                action_id=CaseNotificationActions.edit,
                value=button_metadata,
            ),
            Button(
                text=":slack: Create Channel",
                action_id=CaseNotificationActions.migrate,
                value=button_metadata,
            ),
            Button(
                text=":fire: Escalate",
                action_id=CaseNotificationActions.escalate,
                value=button_metadata,
            ),
        ]
        if case.status == CaseStatus.new:
            action_buttons.insert(
                0,
                Button(
                    text=":mag: Triage",
                    action_id=CaseNotificationActions.triage,
                    value=button_metadata,
                ),
            )
        blocks.extend([Actions(elements=action_buttons)])

    return Message(blocks=blocks).build()["blocks"]