From 25061b6140401316ad4ac3d4a69edefdd0654b56 Mon Sep 17 00:00:00 2001 From: "Dr.Caduceus" Date: Mon, 13 Nov 2023 11:51:58 +0530 Subject: [PATCH] Bump to v1.5 --- bot/modules/telegram.py | 70 ++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/bot/modules/telegram.py b/bot/modules/telegram.py index 7b500ec..452fb6e 100644 --- a/bot/modules/telegram.py +++ b/bot/modules/telegram.py @@ -1,56 +1,68 @@ -from pyrogram.types import Message +from telethon.events import NewMessage +from telethon.tl.custom import Message from datetime import datetime from mimetypes import guess_type from bot import TelegramBot from bot.config import Telegram from bot.server.error import abort -async def get_message(message_id: int): +async def get_message(message_id: int) -> Message | None: message = None try: - message = await TelegramBot.get_messages( - chat_id=Telegram.CHANNEL_ID, - message_ids=message_id - ) - if message.empty: message = None + message = await TelegramBot.get_messages(Telegram.CHANNEL_ID, ids=message_id) except Exception: pass return message -async def get_file_properties(msg: Message): - attributes = ( - 'document', - 'video', - 'audio', - 'voice', - 'photo', - 'video_note' +async def send_message(message:Message, send_to:int = Telegram.CHANNEL_ID) -> Message: + message.forward + return await TelegramBot.send_message( + entity=send_to, + message=message ) - for attribute in attributes: - media = getattr(msg, attribute, None) - if media: - file_type = attribute - break - - if not media: abort(400, 'Unknown file type.') +def filter_files(update: NewMessage.Event | Message): + return bool( + ( + update.document + or update.photo + or update.video + or update.video_note + or update.audio + or update.gif + ) + and not update.sticker + ) - file_name = getattr(media, 'file_name', None) - file_size = getattr(media, 'file_size', 0) +def get_file_properties(message: Message): + file_name = message.file.name + file_size = message.file.size or 0 + mime_type = message.file.mime_type if not file_name: - file_format = { + attributes = { 'video': 'mp4', 'audio': 'mp3', 'voice': 'ogg', 'photo': 'jpg', 'video_note': 'mp4' - }.get(attribute) + } + + for attribute in attributes: + media = getattr(message, attribute, None) + if media: + file_type, file_format = attribute, attributes[attribute] + break + + if not media: + abort(400, 'Invalid media type.') + date = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") file_name = f'{file_type}-{date}.{file_format}' - - mime_type = guess_type(file_name)[0] or 'application/octet-stream' - + + if not mime_type: + mime_type = guess_type(file_name)[0] or 'application/octet-stream' + return file_name, file_size, mime_type