mirror of
https://git.freetards.xyz/array.in.a.matrix/TexLiLy.git
synced 2025-04-02 13:21:42 -04:00
[FIX] Oops, forgot about main.py!
This commit is contained in:
parent
928eeb3a9a
commit
47d46a7aeb
1 changed files with 68 additions and 0 deletions
68
main.py
Normal file
68
main.py
Normal file
|
@ -0,0 +1,68 @@
|
|||
from nio import *
|
||||
|
||||
import latex
|
||||
import asyncio
|
||||
import yaml
|
||||
import os
|
||||
|
||||
# Our message callback. It should be passed through a router.
|
||||
async def msg_cb(room: MatrixRoom, event: RoomMessageText) -> None:
|
||||
print("Message")
|
||||
if event.body.startswith("$"):
|
||||
filename = latex.render("", event.body)
|
||||
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)
|
||||
print("Uploaded")
|
||||
content = {
|
||||
"body": filename,
|
||||
"info": {
|
||||
"size": size,
|
||||
"mimetype": "image/png"
|
||||
},
|
||||
"msgtype": "m.image",
|
||||
"url": resp.content_uri,
|
||||
}
|
||||
await client.room_send(room.room_id, message_type="m.room.message", content=content)
|
||||
|
||||
# Our file callback.
|
||||
async def file_cb(room: MatrixRoom, event: RoomMessageFile) -> None:
|
||||
url = event.url
|
||||
filename = event.body
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# Load our config file.
|
||||
global client
|
||||
|
||||
config = {}
|
||||
with open('texlily.yaml', 'r') as conf:
|
||||
config = yaml.safe_load(conf)
|
||||
if config == None:
|
||||
print("No config file found. ")
|
||||
print("")
|
||||
print("Please create a file named texlily.yaml and read the")
|
||||
print("README.")
|
||||
return
|
||||
# Retrieve our configuration.
|
||||
homeserver = str(config["homeserver"])
|
||||
user = str(config["user"])
|
||||
token = str(config["token"])
|
||||
path = str(config["path"])
|
||||
|
||||
client = AsyncClient(homeserver)
|
||||
client.access_token = token
|
||||
client.user_id = user
|
||||
|
||||
# Bad kludge!
|
||||
await client.sync(timeout=300000)
|
||||
|
||||
# Register all of the callbacks
|
||||
client.add_event_callback(msg_cb, RoomMessageText)
|
||||
client.add_event_callback(file_cb, RoomMessageFile)
|
||||
|
||||
await client.sync_forever(timeout=10000, full_state=True)
|
||||
|
||||
asyncio.run(main())
|
Loading…
Add table
Reference in a new issue