"""Bot's callbacks implementation""" import asyncio import glob import html import traceback from mab import MatrixBot from mab.types import VideoFileProperties from mab.filters.text import TextFilter, TextStartsWithFilter, TextEndsWithFilter from nio import MatrixRoom, RoomMessageText # # PRIVATE DATA # _bot: MatrixBot # # CALLBACKS # async def command_help(room: MatrixRoom, text: RoomMessageText) -> None: """!help""" if text.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) async def command_http_https(room: MatrixRoom, text: RoomMessageText) -> None: """The message starts with `http://` or `https://`""" if text.sender == _bot.get_client().user_id: return resp = "Сообщение начинается с http:// или с " resp += "https://. Ещё не реализовано." await _bot.send_text_to_room(room.room_id, resp, True) async def command_test(room: MatrixRoom, text: RoomMessageText) -> None: """The message ends with ` TesT` (case matters)""" if text.sender == _bot.get_client().user_id: return # glob patterns to use glob_img = ["*.png", "*.jpg", ".jpeg", "*.gif", "*.webp"] glob_vid = ["*.mp4", "*.avi", "*.mkv"] # TODO glob_aud = ["*.mp3", "*.m4a", ".wav", "*.ogg", "*.flac"] # help message resp = "Сообщение заканчивается на TesT.
" resp += "Сейчас будут отправлены все файлы из директории test_media" resp += ", у которых следующие расширения:
" resp += f"" resp += f"" await _bot.send_text_to_room(room.room_id, resp, is_html=True) 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, file, caption=f"Картинка {html.escape(file)}", is_html=True ) except: await _bot.send_text_to_room(room.room_id, traceback.format_exc(), False) 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, file, caption=f"Видео {html.escape(file)}", is_html=True ) except: await _bot.send_text_to_room(room.room_id, traceback.format_exc(), False) await asyncio.sleep(0.5) # # PUBLIC # async def initialize(bot: MatrixBot) -> None: """This function initializes the bot. It does not start it.""" global _bot _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()) async def deinitizalize(bot: MatrixBot) -> None: """This function deinitializes the bot. It does not stop it.""" pass