ForcePilot/backend/package/yuxi/channels/adapters/slack/modals.py

107 lines
2.7 KiB
Python
Raw Normal View History

from __future__ import annotations
from typing import Any
def build_modal(
title: str,
*blocks: dict[str, Any],
submit_text: str = "提交",
close_text: str = "取消",
callback_id: str = "",
clear_on_close: bool = False,
notify_on_close: bool = False,
external_id: str = "",
) -> dict[str, Any]:
view: dict[str, Any] = {
"type": "modal",
"title": {"type": "plain_text", "text": title},
"blocks": list(blocks),
"submit": {"type": "plain_text", "text": submit_text},
"close": {"type": "plain_text", "text": close_text},
}
if callback_id:
view["callback_id"] = callback_id
if clear_on_close:
view["clear_on_close"] = True
if notify_on_close:
view["notify_on_close"] = True
if external_id:
view["external_id"] = external_id
return view
def build_home_tab(
*blocks: dict[str, Any],
callback_id: str = "",
external_id: str = "",
) -> dict[str, Any]:
view: dict[str, Any] = {
"type": "home",
"blocks": list(blocks),
}
if callback_id:
view["callback_id"] = callback_id
if external_id:
view["external_id"] = external_id
return view
def build_confirmation_dialog(
title: str,
text: str,
confirm_text: str = "确认",
deny_text: str = "取消",
style: str = "",
) -> dict[str, Any]:
dialog: dict[str, Any] = {
"title": {"type": "plain_text", "text": title},
"text": {"type": "mrkdwn", "text": text},
"confirm": {"type": "plain_text", "text": confirm_text},
"deny": {"type": "plain_text", "text": deny_text},
}
if style in ("primary", "danger"):
dialog["style"] = style
return dialog
def build_view_error_section(message: str) -> list[dict[str, Any]]:
return [
{
"type": "section",
"text": {"type": "mrkdwn", "text": f":warning: *错误*\n>{message}"},
},
]
def build_approval_modal(
title: str,
detail: str,
exec_command: str = "",
callback_id: str = "exec_approval",
) -> dict[str, Any]:
blocks = [
{
"type": "section",
"text": {"type": "mrkdwn", "text": f"*{title}*\n\n{detail}"},
},
{"type": "divider"},
]
if exec_command:
blocks.append(
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"```{exec_command}```",
},
}
)
return build_modal(
"Exec 审批",
*blocks,
submit_text="确认执行",
close_text="取消",
callback_id=callback_id,
)