Skip to content
This repository has been archived by the owner on Oct 21, 2023. It is now read-only.

Commit

Permalink
feat: basic RichMessage & ForwardMessage parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueGlassBlock committed Apr 3, 2023
1 parent 5bf2b31 commit 752ba71
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ jobs:
with:
publish-dir: './build/html'
production-branch: master
production-deploy: ${{ env.RELEASE == 'true' }}
github-token: ${{ secrets.GITHUB_TOKEN }}
deploy-message: "Deploy from GitHub Actions"
enable-pull-request-comment: false
Expand Down
58 changes: 58 additions & 0 deletions python/ichika/message/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ class MusicShare(Element):
music_url: str
brief: str

def __str__(self) -> str:
return f"[{self.kind}音乐分享: {self.title}]"


@dataclass
class LightApp(Element):
Expand All @@ -140,6 +143,41 @@ def __str__(self) -> str:
return "[小程序]"


_T_Forward = TypeVar("_T_Forward", bound=Optional[str], default=str)


@dataclass(init=False)
class ForwardMessage(Generic[_T_Forward], Element):
"""合并转发消息,本质为 XML 卡片"""

# TODO: download from ForwardMessage

res_id: _T_Forward
file_name: _T_Forward
brief: _T_Forward

def __init__(self, res_id: _T_Forward = None, file_name: _T_Forward = None, brief: _T_Forward = None) -> None:
self.res_id = res_id
self.file_name = file_name
self.brief = brief

def __str__(self) -> str:
return "[合并转发]"


@dataclass
class RichMessage(Element):
"""卡片消息"""

service_id: int
"""服务 ID"""
content: str
"""卡片内容"""

def __str__(self) -> str:
return "[富文本卡片]"


T_Audio = TypeVar("T_Audio", bound=Optional[SealedAudio], default=SealedAudio)


Expand Down Expand Up @@ -349,4 +387,24 @@ def _light_app_deserializer(**data) -> Element:
return LightApp(content=data["content"])


def _rich_msg_deserializer(**data) -> Element:
from contextlib import suppress

service_id: int = data["service_id"]
content: str = data["content"]

with suppress(IndexError, KeyError, ValueError, AttributeError):
from xml.dom import minidom

root: minidom.Document = minidom.parseString(data["content"])
if isinstance(msg_elem := root.getElementsByTagName("msg")[0], minidom.Element):
res_id: str = msg_elem.getAttribute("m_resid")
file_name: str = msg_elem.getAttribute("m_fileName")
brief: str = msg_elem.getAttribute("brief")
return ForwardMessage(res_id=res_id, file_name=file_name, brief=brief)

return RichMessage(service_id=service_id, content=content)


DESERIALIZE_INV["LightApp"] = _light_app_deserializer
DESERIALIZE_INV["RichMessage"] = _rich_msg_deserializer
7 changes: 7 additions & 0 deletions src/message/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ pub fn serialize_element(py: Python, e: RQElem) -> PyResult<Option<&PyDict>> {
content: app.content
}
}
RQElem::RichMsg(rich) => {
dict! {py,
type: "RichMessage",
service_id: rich.service_id,
content: rich.template1
}
}
RQElem::Other(_) => {
return Ok(None);
}
Expand Down

0 comments on commit 752ba71

Please sign in to comment.