mirror of https://gitlab.com/ceda_ei/temac
Download media, check mimetype, send accordingly
This commit is contained in:
parent
c0a5fb3d3e
commit
0b72b789ff
|
@ -1,3 +1,4 @@
|
||||||
config.py
|
config.py
|
||||||
Temec.session
|
Temec.session
|
||||||
|
Temec.session-journal
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
|
43
bot.py
43
bot.py
|
@ -1,10 +1,41 @@
|
||||||
from telethon import TelegramClient, events
|
from telethon import TelegramClient, events
|
||||||
from matrix_client.client import MatrixClient
|
from matrix_client.client import MatrixClient
|
||||||
import config
|
import config
|
||||||
|
import os
|
||||||
|
import magic
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.ERROR)
|
||||||
|
|
||||||
|
|
||||||
async def send_to_matrix(event, room):
|
def media_uploader(path, matrix_bot):
|
||||||
room.send_text(event.raw_text)
|
mime = magic.from_file(path, mime=True)
|
||||||
|
with open(path, 'rb') as f:
|
||||||
|
mxc = matrix_bot.upload(f.read(), mime)
|
||||||
|
return mxc, mime.split('/')[0]
|
||||||
|
|
||||||
|
|
||||||
|
async def send_to_matrix(event, room, matrix_bot):
|
||||||
|
media = await event.download_media()
|
||||||
|
if media is not None:
|
||||||
|
paths = media.split("\n")
|
||||||
|
mxcs = list(map(
|
||||||
|
lambda x: media_uploader(x, matrix_bot),
|
||||||
|
paths
|
||||||
|
))
|
||||||
|
for (mxc, mime), path in zip(mxcs, paths):
|
||||||
|
if mime == "image":
|
||||||
|
room.send_image(mxc, path)
|
||||||
|
elif mime == "audio":
|
||||||
|
room.send_audio(mxc, path)
|
||||||
|
elif mime == "video":
|
||||||
|
room.send_video(mxc, path)
|
||||||
|
else:
|
||||||
|
room.send_file(mxc, path)
|
||||||
|
os.remove(path)
|
||||||
|
|
||||||
|
if event.raw_text != "":
|
||||||
|
room.send_text(event.raw_text)
|
||||||
|
|
||||||
|
|
||||||
matrix_bot = MatrixClient(config.matrix_creds['server'])
|
matrix_bot = MatrixClient(config.matrix_creds['server'])
|
||||||
|
@ -16,8 +47,10 @@ client = TelegramClient('Temec', config.api_id, config.api_hash).start()
|
||||||
rooms = matrix_bot.get_rooms()
|
rooms = matrix_bot.get_rooms()
|
||||||
|
|
||||||
for chan_id, room_id in config.mappings:
|
for chan_id, room_id in config.mappings:
|
||||||
client.add_event_handler(lambda x: send_to_matrix(x, rooms[room_id]),
|
client.add_event_handler(
|
||||||
events.NewMessage(chats=[chan_id]))
|
lambda x: send_to_matrix(x, rooms[room_id], matrix_bot),
|
||||||
|
events.NewMessage(chats=[chan_id])
|
||||||
|
)
|
||||||
|
|
||||||
|
print("Started listening")
|
||||||
client.run_until_disconnected()
|
client.run_until_disconnected()
|
||||||
|
|
Loading…
Reference in New Issue