76 lines
2.4 KiB
Python
76 lines
2.4 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import logging
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
from .client import NextcloudTalkClient
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
PAIRING_MESSAGE = (
|
||
|
|
"Hello! Your account is not yet authorized to interact with this bot.\n\n"
|
||
|
|
"Your Nextcloud user id: `{user_id}`\n\n"
|
||
|
|
"Please ask an administrator to approve your access by adding your user id "
|
||
|
|
"to the paired users list using:\n\n"
|
||
|
|
"```\n"
|
||
|
|
"from yuxi.channels.adapters.nextcloudtalk.security import pair_user\n"
|
||
|
|
'pair_user("{user_id}")\n'
|
||
|
|
"```\n\n"
|
||
|
|
"Once approved, send another message to start interacting with the bot."
|
||
|
|
)
|
||
|
|
|
||
|
|
APPROVED_MESSAGE = "Your account `{user_id}` has been approved! Send a message to start."
|
||
|
|
|
||
|
|
|
||
|
|
async def send_pairing_challenge(
|
||
|
|
client: NextcloudTalkClient,
|
||
|
|
api_base: str,
|
||
|
|
token: str,
|
||
|
|
user_id: str,
|
||
|
|
config: dict[str, Any] | None = None,
|
||
|
|
) -> bool:
|
||
|
|
try:
|
||
|
|
message = PAIRING_MESSAGE.format(user_id=user_id)
|
||
|
|
payload = {
|
||
|
|
"token": token,
|
||
|
|
"message": message,
|
||
|
|
"referenceId": f"pairing-{user_id}",
|
||
|
|
}
|
||
|
|
result = await client.post(f"{api_base}/chat/{token}", json_data=payload)
|
||
|
|
ocs = result.get("ocs", {})
|
||
|
|
meta = ocs.get("meta", {})
|
||
|
|
if meta.get("statuscode") == 201:
|
||
|
|
logger.info(f"[NextcloudTalk] Pairing challenge sent to {user_id} in {token}")
|
||
|
|
return True
|
||
|
|
logger.warning(f"[NextcloudTalk] Failed to send pairing challenge: {meta}")
|
||
|
|
return False
|
||
|
|
except Exception as e:
|
||
|
|
logger.warning(f"[NextcloudTalk] Failed to send pairing challenge: {e}")
|
||
|
|
return False
|
||
|
|
|
||
|
|
|
||
|
|
async def send_pairing_approved(
|
||
|
|
client: NextcloudTalkClient,
|
||
|
|
api_base: str,
|
||
|
|
token: str,
|
||
|
|
user_id: str,
|
||
|
|
config: dict[str, Any] | None = None,
|
||
|
|
) -> bool:
|
||
|
|
try:
|
||
|
|
message = APPROVED_MESSAGE.format(user_id=user_id)
|
||
|
|
payload = {
|
||
|
|
"token": token,
|
||
|
|
"message": message,
|
||
|
|
"referenceId": f"pairing-approved-{user_id}",
|
||
|
|
}
|
||
|
|
result = await client.post(f"{api_base}/chat/{token}", json_data=payload)
|
||
|
|
ocs = result.get("ocs", {})
|
||
|
|
meta = ocs.get("meta", {})
|
||
|
|
if meta.get("statuscode") == 201:
|
||
|
|
logger.info(f"[NextcloudTalk] Pairing approved notification sent to {user_id}")
|
||
|
|
return True
|
||
|
|
return False
|
||
|
|
except Exception as e:
|
||
|
|
logger.warning(f"[NextcloudTalk] Failed to send pairing approved: {e}")
|
||
|
|
return False
|