47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
|
|
_STICKER_CONTENT_KEY = "body"
|
|
_STICKER_INFO_KEY = "info"
|
|
|
|
|
|
def is_sticker_event(event_source: dict[str, Any]) -> bool:
|
|
content = event_source.get("content", {})
|
|
return content.get("msgtype") == "m.sticker"
|
|
|
|
|
|
def parse_sticker(event_source: dict[str, Any]) -> dict[str, Any] | None:
|
|
content = event_source.get("content", {})
|
|
info = content.get("info", {})
|
|
mxc_url = content.get("url", "")
|
|
return {
|
|
"type": "sticker",
|
|
"body": content.get(_STICKER_CONTENT_KEY, ""),
|
|
"url": mxc_url,
|
|
"mimetype": info.get("mimetype", ""),
|
|
"width": info.get("w", 0),
|
|
"height": info.get("h", 0),
|
|
"text": f"[贴纸] {content.get(_STICKER_CONTENT_KEY, '')}" if content.get(_STICKER_CONTENT_KEY) else "[贴纸]",
|
|
}
|
|
|
|
|
|
def build_outbound_sticker(
|
|
sticker_url: str,
|
|
body: str = "",
|
|
mimetype: str = "image/png",
|
|
width: int = 0,
|
|
height: int = 0,
|
|
) -> dict[str, Any]:
|
|
content: dict[str, Any] = {
|
|
"msgtype": "m.sticker",
|
|
"body": body or "sticker",
|
|
"url": sticker_url,
|
|
"info": {"mimetype": mimetype},
|
|
}
|
|
if width and height:
|
|
content["info"]["w"] = width
|
|
content["info"]["h"] = height
|
|
return content
|