surfaces/adapter/matrix/reactions.py

68 lines
2 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from __future__ import annotations
from typing import Any
from nio import AsyncClient
from sdk.interface import UserSettings
CONFIRM_REACTION = "👍"
CANCEL_REACTION = ""
SKILL_REACTIONS = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
REACTION_TO_INDEX = {emoji: idx + 1 for idx, emoji in enumerate(SKILL_REACTIONS)}
def build_skills_text(settings: UserSettings) -> str:
lines: list[str] = ["🧩 Скиллы"]
for idx, (name, enabled) in enumerate(settings.skills.items(), start=1):
state = "" if enabled else ""
emoji = SKILL_REACTIONS[idx - 1] if idx - 1 < len(SKILL_REACTIONS) else f"{idx}."
lines.append(f"{state} {emoji} {name}")
lines.append("")
lines.append("Реакции 1⃣-9⃣ переключают навыки.")
return "\n".join(lines)
def build_confirmation_text(description: str) -> str:
return "\n".join(
[
"🤖 Lambda",
description,
"",
f"{CONFIRM_REACTION} подтвердить · {CANCEL_REACTION} отменить",
"!yes — подтвердить · !no — отменить",
]
)
def reaction_to_skill_index(key: str) -> int | None:
return REACTION_TO_INDEX.get(key)
async def add_reaction(client: AsyncClient, room_id: str, event_id: str, key: str) -> Any:
return await client.room_send(
room_id,
"m.reaction",
{
"m.relates_to": {
"rel_type": "m.annotation",
"event_id": event_id,
"key": key,
}
},
)
async def remove_reaction(client: AsyncClient, room_id: str, event_id: str, key: str) -> Any:
return await client.room_send(
room_id,
"m.reaction",
{
"m.relates_to": {
"rel_type": "m.annotation",
"event_id": event_id,
"key": key,
},
"undo": True,
},
)