Merge branch 'dev' into dev

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

52
main.py
View file

@ -4,33 +4,45 @@ import asyncio
import yaml
import os
async def send_png(room: MatrixRoom, filename: str) -> None:
import imagesize
basename = os.path.basename(filename)
w, h = imagesize.get(filename)
file = open(filename, "r+b")
size = os.path.getsize(filename)
filename = os.path.basename(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 = {
"body": basename,
"info": {
"size": size,
"mimetype": "image/png",
"w": w, "h": h
},
"msgtype": "m.image",
"url": resp.content_uri,
}
file.close()
await client.room_send(room.room_id, message_type="m.room.message", content=content)
# 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:
filename = latex.render("", event.body)
w, h = imagesize.get(filename)
file = open(filename, "r+b")
size = os.path.getsize(filename)
filename = os.path.basename(filename)
resp, keys = await client.upload(file, content_type="image/png", filename=filename)
content = {
"body": filename,
"info": {
"size": size,
"w": w, "h": h,
"mimetype": "image/png"
},
"msgtype": "m.image",
"url": resp.content_uri,
}
await client.room_send(room.room_id, message_type="m.room.message", content=content)
except FileNotFoundError:
await send_png(room, filename)
except (FileNotFoundError, OSError):
content = {
"msgtype": "m.text",
"body": "Couldn't parse LaTeX correctly."