ForcePilot/backend/package/yuxi/channel/extensions/msteams/message_extension.py

134 lines
4.3 KiB
Python
Raw Normal View History

from __future__ import annotations
import logging
from typing import Any
from .types import BotFrameworkActivity
logger = logging.getLogger(__name__)
async def handle_compose_extension(name: str, activity: BotFrameworkActivity, ctx: Any | None = None) -> dict:
if name == "composeExtension/query":
return await _handle_me_query(activity, ctx)
if name == "composeExtension/fetchTask":
return await _handle_me_fetch_task(activity, ctx)
if name == "composeExtension/submitAction":
return await _handle_me_submit_action(activity, ctx)
if name == "composeExtension/queryLink":
return await _handle_me_query_link(activity, ctx)
return {"status": 200}
async def _handle_me_query(activity: BotFrameworkActivity, ctx: Any | None = None) -> dict:
value = activity.value or {}
query_text = value.get("queryText", "")
parameters = value.get("parameters", [])
results = await _search_content(query_text, parameters, ctx)
attachments = []
for item in results:
card = {
"type": "AdaptiveCard",
"version": "1.4",
"body": [
{"type": "TextBlock", "text": item["title"], "weight": "Bolder"},
{"type": "TextBlock", "text": item.get("description", ""), "wrap": True},
],
}
attachments.append({
"contentType": "application/vnd.microsoft.card.adaptive",
"content": card,
"preview": {
"contentType": "application/vnd.microsoft.card.thumbnail",
"content": {
"title": item["title"],
"subtitle": item.get("description", "")[:120],
}
},
})
return {
"composeExtension": {
"type": "result",
"attachmentLayout": "list",
"attachments": attachments,
}
}
async def _handle_me_fetch_task(activity: BotFrameworkActivity, ctx: Any | None = None) -> dict:
return {
"composeExtension": {
"type": "message",
"text": "操作已触发",
}
}
async def _handle_me_submit_action(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:me_action:{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": "messaging_extension_submit",
"action_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 messaging extension submit")
return {"status": 200}
async def _handle_me_query_link(activity: BotFrameworkActivity, ctx: Any | None = None) -> dict:
value = activity.value or {}
url = value.get("url", "")
card = await _build_link_unfurl_card(url, ctx)
attachment = {
"contentType": "application/vnd.microsoft.card.adaptive",
"content": card,
}
return {
"composeExtension": {
"type": "result",
"attachmentLayout": "list",
"attachments": [attachment],
}
}
async def _search_content(query_text: str, parameters: list, ctx: Any | None = None) -> list[dict]:
return [
{"title": f"搜索结果: {query_text}", "description": "这是一个示例搜索结果。"},
]
async def _build_link_unfurl_card(url: str, ctx: Any | None = None) -> dict:
return {
"type": "AdaptiveCard",
"version": "1.4",
"body": [
{"type": "TextBlock", "text": "链接预览", "weight": "Bolder"},
{"type": "TextBlock", "text": url, "wrap": True, "color": "Accent"},
],
}