152 lines
5.2 KiB
Python
152 lines
5.2 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import logging
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
from .types import BotFrameworkActivity
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
def _build_task_card(task_type: str, data: dict) -> dict:
|
||
|
|
cards: dict[str, dict] = {
|
||
|
|
"settings": {
|
||
|
|
"title": "设置",
|
||
|
|
"body": [
|
||
|
|
{"type": "TextBlock", "text": "配置选项", "weight": "Bolder"},
|
||
|
|
{"type": "Input.Text", "id": "setting_value", "placeholder": "输入配置值"},
|
||
|
|
],
|
||
|
|
},
|
||
|
|
"feedback": {
|
||
|
|
"title": "反馈",
|
||
|
|
"body": [
|
||
|
|
{"type": "TextBlock", "text": "您的反馈", "weight": "Bolder"},
|
||
|
|
{"type": "Input.Text", "id": "feedback_text", "placeholder": "请输入反馈内容", "isMultiline": True},
|
||
|
|
],
|
||
|
|
},
|
||
|
|
"default": {
|
||
|
|
"title": "操作",
|
||
|
|
"body": [
|
||
|
|
{"type": "TextBlock", "text": "请填写以下信息", "weight": "Bolder"},
|
||
|
|
{"type": "Input.Text", "id": "user_input", "placeholder": "输入内容"},
|
||
|
|
],
|
||
|
|
},
|
||
|
|
}
|
||
|
|
return cards.get(task_type, cards["default"])
|
||
|
|
|
||
|
|
|
||
|
|
async def handle_task_fetch(activity: BotFrameworkActivity, ctx: Any | None = None) -> dict:
|
||
|
|
value = activity.value or {}
|
||
|
|
task_type = value.get("data", {}).get("task_type", "default")
|
||
|
|
|
||
|
|
card = _build_task_card(task_type, value.get("data", {}))
|
||
|
|
return {
|
||
|
|
"task": {
|
||
|
|
"type": "continue",
|
||
|
|
"value": {
|
||
|
|
"title": card.get("title", "操作"),
|
||
|
|
"height": "large",
|
||
|
|
"width": "large",
|
||
|
|
"card": {
|
||
|
|
"type": "AdaptiveCard",
|
||
|
|
"version": "1.4",
|
||
|
|
"body": card.get("body", []),
|
||
|
|
"actions": [
|
||
|
|
{"type": "Action.Submit", "title": "提交", "data": {"task_type": task_type, "submit": True}}
|
||
|
|
],
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
async def handle_task_submit(activity: BotFrameworkActivity, ctx: Any | None = None) -> dict:
|
||
|
|
value = activity.value or {}
|
||
|
|
task_type = value.get("data", {}).get("task_type", "")
|
||
|
|
|
||
|
|
if ctx and hasattr(ctx, "queue") and ctx.queue:
|
||
|
|
try:
|
||
|
|
from yuxi.channel.message.models import MessageType, PeerInfo, UnifiedMessage
|
||
|
|
from yuxi.channel.routing.models import PeerKind
|
||
|
|
|
||
|
|
unified_msg = UnifiedMessage(
|
||
|
|
msg_id=f"msteams:task:{activity.id}",
|
||
|
|
channel_type="msteams",
|
||
|
|
account_id=getattr(ctx, "account_id", ""),
|
||
|
|
content="",
|
||
|
|
sender=PeerInfo(id=activity.from_id, kind=PeerKind.DIRECT),
|
||
|
|
message_type=MessageType.EVENT,
|
||
|
|
raw_payload=activity.raw,
|
||
|
|
metadata={
|
||
|
|
"event_type": "task_submit",
|
||
|
|
"task_type": task_type,
|
||
|
|
"task_data": value.get("data", {}),
|
||
|
|
},
|
||
|
|
conversation_label=f"msteams:{activity.conversation_id}",
|
||
|
|
native_channel_id=activity.conversation_id,
|
||
|
|
)
|
||
|
|
ctx.queue.put_nowait(unified_msg)
|
||
|
|
except Exception:
|
||
|
|
logger.exception("Failed to enqueue task submit")
|
||
|
|
|
||
|
|
return {
|
||
|
|
"task": {
|
||
|
|
"type": "message",
|
||
|
|
"value": "操作已提交",
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
async def handle_dialog_fetch(activity: BotFrameworkActivity, ctx: Any | None = None) -> dict:
|
||
|
|
value = activity.value or {}
|
||
|
|
dialog_data = value.get("data", {})
|
||
|
|
|
||
|
|
card = dialog_data.get("card") or {
|
||
|
|
"type": "AdaptiveCard",
|
||
|
|
"version": "1.4",
|
||
|
|
"body": [
|
||
|
|
{"type": "TextBlock", "text": "对话框", "weight": "Bolder"},
|
||
|
|
{"type": "Input.Text", "id": "dialog_input", "placeholder": "输入内容"},
|
||
|
|
],
|
||
|
|
"actions": [
|
||
|
|
{"type": "Action.Submit", "title": "提交", "data": {"dialog_submit": True}}
|
||
|
|
],
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
"dialog": {
|
||
|
|
"title": dialog_data.get("title", "操作"),
|
||
|
|
"card": card,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
async def handle_dialog_submit(activity: BotFrameworkActivity, ctx: Any | None = None) -> dict:
|
||
|
|
value = activity.value or {}
|
||
|
|
|
||
|
|
if ctx and hasattr(ctx, "queue") and ctx.queue:
|
||
|
|
try:
|
||
|
|
from yuxi.channel.message.models import MessageType, PeerInfo, UnifiedMessage
|
||
|
|
from yuxi.channel.routing.models import PeerKind
|
||
|
|
|
||
|
|
unified_msg = UnifiedMessage(
|
||
|
|
msg_id=f"msteams:dialog:{activity.id}",
|
||
|
|
channel_type="msteams",
|
||
|
|
account_id=getattr(ctx, "account_id", ""),
|
||
|
|
content="",
|
||
|
|
sender=PeerInfo(id=activity.from_id, kind=PeerKind.DIRECT),
|
||
|
|
message_type=MessageType.EVENT,
|
||
|
|
raw_payload=activity.raw,
|
||
|
|
metadata={
|
||
|
|
"event_type": "dialog_submit",
|
||
|
|
"dialog_data": value.get("data", {}),
|
||
|
|
},
|
||
|
|
conversation_label=f"msteams:{activity.conversation_id}",
|
||
|
|
native_channel_id=activity.conversation_id,
|
||
|
|
)
|
||
|
|
ctx.queue.put_nowait(unified_msg)
|
||
|
|
except Exception:
|
||
|
|
logger.exception("Failed to enqueue dialog submit")
|
||
|
|
|
||
|
|
return {"status": 200}
|