新增 Google Chat 对接的全套工具模块,包括: - 会话线程管理、消息解析与格式化 - Pub/Sub 消息解码、提及和命令识别 - 权限审批、目录管理和审计日志 - 消息缓存、媒体上传下载和 SSFR 防护 - 策略配置、卡片构建和流式回复支持
50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from googleapiclient.discovery import Resource
|
|
|
|
from yuxi.channels.models import DeliveryResult
|
|
from yuxi.utils.logging_config import logger
|
|
|
|
_PAIRING_CHALLENGE_MESSAGE = (
|
|
"{agent_name} Approval Required\n\n"
|
|
"To continue, send the following:\n"
|
|
"```\n/approve {user_id}\n```\n"
|
|
"This confirmation is required to pair with this bot account."
|
|
)
|
|
|
|
|
|
async def send_pairing_challenge(
|
|
chat_service: Resource,
|
|
space_name: str,
|
|
user_id: str,
|
|
agent_name: str = "Bot",
|
|
) -> DeliveryResult:
|
|
content = _PAIRING_CHALLENGE_MESSAGE.format(
|
|
agent_name=agent_name,
|
|
user_id=user_id,
|
|
)
|
|
body = {"text": content}
|
|
try:
|
|
result = chat_service.spaces().messages().create(parent=space_name, body=body).execute()
|
|
msg_id = result.get("name", "")
|
|
logger.info(f"Pairing challenge sent to {space_name}: user_id={user_id}")
|
|
return DeliveryResult(success=True, message_id=msg_id)
|
|
except Exception as e:
|
|
logger.warning(f"Failed to send pairing challenge to {space_name}: {e}")
|
|
return DeliveryResult(success=False, error=str(e))
|
|
|
|
|
|
async def check_pairing_approval(
|
|
chat_service: Resource,
|
|
space_name: str,
|
|
user_id: str,
|
|
message_text: str,
|
|
) -> bool:
|
|
approved = f"/approve {user_id}" in message_text.strip().lower()
|
|
if approved:
|
|
logger.info(f"Pairing approved for {space_name}: user_id={user_id}")
|
|
return approved
|