ForcePilot/backend/package/yuxi/channel/extensions/msteams/message_extension.py
Kris 94444ced96 feat(channel): 添加 Microsoft Teams 渠道扩展
新增 Microsoft Teams 渠道扩展,支持在 Yuxi 平台中集成 Microsoft Teams 协作平台。

包含以下功能模块:
- sdk: Bot Framework SDK 封装
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- webhook: Webhook 事件处理
- outbound: 外发消息管理
- streaming: 流式消息处理
- pairing: 用户配对与绑定
- security: 安全校验
- auth: JWT 认证
- jwks: JWKS 密钥管理
- dedupe: 消息去重
- monitor: 渠道状态监控
- status: 会话状态管理
- session: 会话管理
- state: 状态管理
- runtime: 运行时管理
- actions: 动作处理
- adaptive_card: 自适应卡片
- task_modules: 任务模块
- message_extension: 消息扩展
- proactive: Proactive Messaging
- graph: Microsoft Graph API 集成
- graph_teams: Teams 操作
- graph_members: 成员管理
- graph_messages: 消息获取
- graph_thread: 线程管理
- graph_users: 用户管理
- graph_upload: 文件上传
- files: 文件处理
- file_consent: 文件授权
- conversations: 会话存储
- mentions: @提及处理
- threading: 线程管理
- reactions: 表情反应
- polls: 投票功能
- meetings: 会议集成
- feedback: 反馈处理
- sso: 单点登录
- deep_links: 深层链接
- incoming_webhook: 入站 Webhook
- localization: 本地化
- user_agent: 用户代理
- sent_message_cache: 消息缓存
- types: 类型定义
2026-05-21 11:28:42 +08:00

134 lines
4.3 KiB
Python

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"},
],
}