diff --git a/mathpix/mathpixplugin.py b/mathpix/mathpixplugin.py index 6d5f33a..09f4887 100644 --- a/mathpix/mathpixplugin.py +++ b/mathpix/mathpixplugin.py @@ -8,19 +8,20 @@ from maubot.matrix import MaubotMessageEvent from mautrix.crypto.attachments import decrypt_attachment from mautrix.types import EventType, MessageType, EncryptedFile +from mautrix.types.event.message import MediaMessageEventContent from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper def _build_json_response(response: dict) -> str: - html_output = '' + html_output += f' {key}: {v[:40]}\n' + html_output += '\n

Text:

\n' + html_output += f"
{response['text']}
\n" return html_output -def _parse_response(response: str) -> str: - return f"
{response}
" - class Config(BaseProxyConfig): def do_update(self, helper: ConfigUpdateHelper) -> None: @@ -30,6 +31,7 @@ def do_update(self, helper: ConfigUpdateHelper) -> None: class MathpixPlugin(Plugin): + config: BaseProxyConfig @classmethod def get_config_class(cls) -> Type[BaseProxyConfig]: @@ -50,33 +52,42 @@ async def start(self) -> None: @event.on(EventType.ROOM_MESSAGE) async def start_ocr(self, evt: MaubotMessageEvent): - self.log.debug("Mathpix ocr bot detect message!") if evt.content.msgtype != MessageType.IMAGE: + self.log.debug(f"Mathpix ocr bot detect message: type: {evt.content.msgtype}") return self.log.info("Mathpix ocr bot detect image message!") - file = evt.content.file # type: EncryptedFile + content = evt.content + assert isinstance(content, MediaMessageEventContent), f"Unexpected content type: {content}" + + file = content.file # type: EncryptedFile | None self.log.debug(f"Mathpix ocr bot received file meta: {file}") - await evt.react("👌") await evt.mark_read() - - enc_image_bytes = await self.client.download_media(file.url) - - image_bytes = decrypt_attachment( - enc_image_bytes, - key=file.key.key, hash=file.hashes["sha256"], iv=file.iv - ) + if file is None: + # this is the case for unencrypted room message + # retrieve image from content mcx url + assert content.url is not None, "No url in content" + image_bytes = await self.client.download_media(content.url) + await evt.respond("Note: You are sending message in an unencrypted room. Please consider enabling end-to-end encryption in this room.") + else: + # encrypted room message, retreive image from encrypted file + assert file.url is not None, "No url in encrypted file" + enc_image_bytes = await self.client.download_media(file.url) + image_bytes = decrypt_attachment( + enc_image_bytes, + key=file.key.key, hash=file.hashes["sha256"], iv=file.iv + ) + await evt.react("👌") + self.log.debug(f"Mathpix ocr bot received image: size {len(image_bytes)} bytes") try: response = await self.post_image(image_bytes) response1 = _build_json_response(response) - response2 = _parse_response(response["text"]) await evt.respond(response1, allow_html=True) - await evt.respond(response2, allow_html=True) except Exception: - await evt.respond("Mathpix ocr bot encountered an internal error.") + await evt.respond("Mathpix ocr bot encountered an internal error when querying ocr server.") async def post_image(self, image_bytes) -> dict: headers = { diff --git a/maubot.yaml b/maubot.yaml index 0dfbf24..33c25d5 100644 --- a/maubot.yaml +++ b/maubot.yaml @@ -1,6 +1,6 @@ maubot: 0.1.0 id: com.advancedsolver.mathpix -version: 1.0.0 +version: 1.0.1 license: MIT modules: - mathpix diff --git a/mbcbuild.sh b/mbcbuild.sh index a13f23e..12e0ec3 100755 --- a/mbcbuild.sh +++ b/mbcbuild.sh @@ -1 +1 @@ -zip -9r maubot-mathpix-v1.0.0.mbp mathpix maubot.yaml base-config.yaml upload.py +zip -9r maubot-mathpix-v1.0.1.mbp mathpix maubot.yaml base-config.yaml upload.py