ForcePilot/backend/package/yuxi/channels/adapters/googlechat/pairing.py

50 lines
1.5 KiB
Python
Raw Normal View History

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