Merge branch 'dev' into dev

This commit is contained in:
LoaD Accumulator 2023-06-12 17:40:33 +02:00
commit f0d8dd1c7b

36
main.py
View file

@ -4,33 +4,45 @@ import asyncio
import yaml import yaml
import os import os
# Our message callback. It should be passed through a router.
async def msg_cb(room: MatrixRoom, event: RoomMessageText) -> None:
if event.body.startswith("$"):
import imagesize
import latex
try: async def send_png(room: MatrixRoom, filename: str) -> None:
filename = latex.render("", event.body) import imagesize
basename = os.path.basename(filename)
w, h = imagesize.get(filename) w, h = imagesize.get(filename)
file = open(filename, "r+b") file = open(filename, "r+b")
size = os.path.getsize(filename) size = os.path.getsize(filename)
filename = os.path.basename(filename) filename = os.path.basename(filename)
resp, keys = await client.upload(file, content_type="image/png", filename=filename) resp, keys = await client.upload(file, content_type="image/png", filename=basename)
if not isinstance(resp, UploadResponse):
raise OSError("Couldn't upload file.")
content = { content = {
"body": filename, "body": basename,
"info": { "info": {
"size": size, "size": size,
"w": w, "h": h, "mimetype": "image/png",
"mimetype": "image/png" "w": w, "h": h
}, },
"msgtype": "m.image", "msgtype": "m.image",
"url": resp.content_uri, "url": resp.content_uri,
} }
file.close()
await client.room_send(room.room_id, message_type="m.room.message", content=content) await client.room_send(room.room_id, message_type="m.room.message", content=content)
except FileNotFoundError:
# Our message callback. It should be passed through a router.
async def msg_cb(room: MatrixRoom, event: RoomMessageText) -> None:
if event.body.startswith("$"):
import latex
try:
filename = latex.render("", event.body)
await send_png(room, filename)
except (FileNotFoundError, OSError):
content = { content = {
"msgtype": "m.text", "msgtype": "m.text",
"body": "Couldn't parse LaTeX correctly." "body": "Couldn't parse LaTeX correctly."