diff --git a/logic.py b/logic.py index 8743003..100d0d9 100644 --- a/logic.py +++ b/logic.py @@ -8,7 +8,7 @@ import shutil import traceback from mab import MatrixBot -from mab.types import VideoFileProperties +from mab.types import RoomEventData from mab.filters.text import TextFilter, TextStartsWithFilter, TextEndsWithFilter from nio import MatrixRoom, RoomMessageText @@ -30,47 +30,49 @@ async def worker_download(room: MatrixRoom, text: RoomMessageText) -> None: f"Скачивание видео по " f"вашей ссылке" ) - await _bot.send_text_to_room(room.room_id, response, True) + await _bot.send_text(room, response) # get formats d = Downloader(text.body) try: result = await d.download_video("temp") except: - await _bot.send_text_to_room(room.room_id, traceback.format_exc(), False) + await _bot.send_text(room, traceback.format_exc()) return - await _bot.send_text_to_room(room.room_id, "Скачано, загрузка в Matrix...", True) + await _bot.send_text(room, "Скачано, загрузка в Matrix...") # upload try: - await _bot.send_video_to_room(room.room_id, result, caption=f"Скачано из {text.body}") + await _bot.send_video(room, result, text=f"Скачано из {text.body}") except: - await _bot.send_text_to_room(room.room_id, traceback.format_exc(), False) + await _bot.send_text(room.room_id, traceback.format_exc()) finally: os.unlink(result) # # CALLBACKS # -async def command_help(room: MatrixRoom, text: RoomMessageText) -> None: +async def command_help(e: RoomEventData) -> None: """!help""" - if text.sender == _bot.get_client().user_id: + if e.event.sender == _bot.get_client().user_id: return resp = "2026-matrix-down
" resp += "Этот бот используется для скачивания видео/аудио с хостингов, " resp += "поддерживаемых yt-dlp. Просто отправьте ссылку в эту " resp += "комнату, чтобы бот предложил варианты загрузки." - await _bot.send_text_to_room(room.room_id, resp, True) + await e.bot.send_text(e.room, resp) -async def command_http_https(room: MatrixRoom, text: RoomMessageText) -> None: +async def command_http_https(e: RoomEventData) -> None: """The message starts with `http://` or `https://`""" - if text.sender == _bot.get_client().user_id: + if e.event.sender == _bot.get_client().user_id: return - t = asyncio.create_task(worker_download(room, text)) + if not isinstance(e.event, RoomMessageText): + return + t = asyncio.create_task(worker_download(e.room, e.event)) _tasks.add(t) t.add_done_callback(_tasks.discard) -async def command_test(room: MatrixRoom, text: RoomMessageText) -> None: +async def command_test(e: RoomEventData) -> None: """The message ends with ` TesT` (case matters)""" - if text.sender == _bot.get_client().user_id: + if e.event.sender == _bot.get_client().user_id: return # glob patterns to use glob_img = ["*.png", "*.jpg", ".jpeg", "*.gif", "*.webp"] @@ -83,35 +85,35 @@ async def command_test(room: MatrixRoom, text: RoomMessageText) -> None: resp += ", у которых следующие расширения:
" resp += f"" resp += f"" - await _bot.send_text_to_room(room.room_id, resp, is_html=True) + await _bot.send_text(e.room, resp) await asyncio.sleep(1) # send images for g in [f"test_media/{g}" for g in glob_img]: imgs = glob.glob(g) for file in imgs: try: - await _bot.send_image_to_room( - room.room_id, + await _bot.send_image( + e.room, file, - caption=f"Картинка {html.escape(file)}", + text=f"Картинка {html.escape(file)}", is_html=True ) except: - await _bot.send_text_to_room(room.room_id, traceback.format_exc(), False) + await _bot.send_text(e.room, traceback.format_exc()) await asyncio.sleep(0.5) # send videos for g in [f"test_media/{g}" for g in glob_vid]: imgs = glob.glob(g) for file in imgs: try: - await _bot.send_video_to_room( - room.room_id, + await _bot.send_video( + e.room, file, - caption=f"Видео {html.escape(file)}", + text=f"Видео {html.escape(file)}", is_html=True ) except: - await _bot.send_text_to_room(room.room_id, traceback.format_exc(), False) + await _bot.send_text(e.room, traceback.format_exc()) await asyncio.sleep(0.5) # @@ -123,9 +125,12 @@ async def initialize(bot: MatrixBot) -> None: shutil.rmtree("temp", ignore_errors=True) os.makedirs("temp", exist_ok=True) _bot = bot - bot.add_event_callback(command_http_https, TextStartsWithFilter(["https://", "http://"])) - bot.add_event_callback(command_test, TextEndsWithFilter(" TesT", any_case=False)) - bot.add_event_callback(command_help, TextFilter()) + bot.add_callback( + TextStartsWithFilter(["https://", "http://"]), command_http_https) + bot.add_callback( + TextEndsWithFilter(" TesT", any_case=False), command_test) + bot.add_callback( + TextFilter(), command_help) async def deinitizalize(bot: MatrixBot) -> None: """This function deinitializes the bot. It does not stop it.""" diff --git a/main.py b/main.py index 061566e..1fd99e2 100644 --- a/main.py +++ b/main.py @@ -42,7 +42,7 @@ async def main() -> None: # setup the bot bot = MatrixBot(bot_config) await logic.initialize(bot) - bot.start() + await bot.start() # wait for termination logging.info("You may use Ctrl+C to terminate the app") diff --git a/requirements.txt b/requirements.txt index 21693a4..6b0e95f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,7 +16,7 @@ hyperframe==6.1.0 idna==3.19 jsonschema==4.26.0 jsonschema-specifications==2025.9.1 -mab @ git+https://git.tyukalov.su/nikita/mab@v0.2.0 +mab @ git+https://git.tyukalov.su/nikita/mab@v0.3.0 matrix-nio==0.26.0 multidict==6.7.1 packaging==26.3