Download media, check mimetype, send accordingly

This commit is contained in:
Ceda EI 2019-05-16 15:53:03 +05:30
parent c0a5fb3d3e
commit 0b72b789ff
2 changed files with 39 additions and 5 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
config.py
Temec.session
Temec.session-journal
__pycache__/

43
bot.py
View File

@ -1,10 +1,41 @@
from telethon import TelegramClient, events
from matrix_client.client import MatrixClient
import config
import os
import magic
import logging
logging.basicConfig(level=logging.ERROR)
async def send_to_matrix(event, room):
room.send_text(event.raw_text)
def media_uploader(path, matrix_bot):
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'])
@ -16,8 +47,10 @@ client = TelegramClient('Temec', config.api_id, config.api_hash).start()
rooms = matrix_bot.get_rooms()
for chan_id, room_id in config.mappings:
client.add_event_handler(lambda x: send_to_matrix(x, rooms[room_id]),
events.NewMessage(chats=[chan_id]))
client.add_event_handler(
lambda x: send_to_matrix(x, rooms[room_id], matrix_bot),
events.NewMessage(chats=[chan_id])
)
print("Started listening")
client.run_until_disconnected()