""" MIT License Copyright (c) 2023 TheHamkerCat Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from re import findall from pyrogram import filters from wbb import SUDOERS, USERBOT_ID, USERBOT_PREFIX, app, app2, eor from wbb.core.decorators.errors import capture_err from wbb.core.decorators.permissions import adminsOnly from wbb.core.keyboard import ikb from wbb.utils.dbfunctions import delete_note, get_note, get_note_names, save_note from wbb.utils.functions import extract_text_and_keyb __MODULE__ = "Notes" __HELP__ = """/notes To Get All The Notes In The Chat. /save [NOTE_NAME] To Save A Note. Supported note types are Text, Animation, Photo, Document, Video, video notes, Audio, Voice To change caption of any files use.\n/save [NOTE_NAME] [NEW_CAPTION] #NOTE_NAME To Get A Note. /delete [NOTE_NAME] To Delete A Note. Checkout /markdownhelp to know more about formattings and other syntax. """ def extract_urls(reply_markup): urls = [] if reply_markup.inline_keyboard: buttons = reply_markup.inline_keyboard for i, row in enumerate(buttons): for j, button in enumerate(row): if button.url: name = "\n~\nbutton" if i * len(row) + j + 1 == 1 else f"button{i * len(row) + j + 1}" urls.append((f"{name}", button.text, button.url)) return urls @app2.on_message( filters.command("save", prefixes=USERBOT_PREFIX) & ~filters.forwarded & ~filters.via_bot & SUDOERS ) @app.on_message(filters.command("save") & ~filters.private) @adminsOnly("can_change_info") async def save_notee(_, message): if len(message.command) < 2 or not message.reply_to_message: await eor( message, text="**Usage:**\nReply to a message with /save [NOTE_NAME] to save a new note.", ) else: name = message.text.split(None, 1)[1].strip() if not name: return await eor(message, text="**Usage**\n__/save [NOTE_NAME]__") replied_message = message.reply_to_message text = name.split(" ", 1) if len(text) > 1: name = text[0] data = text[1].strip() if replied_message.sticker or replied_message.video_note: data = None else: if replied_message.sticker or replied_message.video_note: data = None elif not replied_message.text and not replied_message.caption: data = None else: data = replied_message.text.markdown if replied_message.text else replied_message.caption.markdown if replied_message.text: _type = "text" file_id = None if replied_message.sticker: _type = "sticker" file_id = replied_message.sticker.file_id if replied_message.animation: _type = "animation" file_id = replied_message.animation.file_id if replied_message.photo: _type = "photo" file_id = replied_message.photo.file_id if replied_message.document: _type = "document" file_id = replied_message.document.file_id if replied_message.video: _type = "video" file_id = replied_message.video.file_id if replied_message.video_note: _type = "video_note" file_id = replied_message.video_note.file_id if replied_message.audio: _type = "audio" file_id = replied_message.audio.file_id if replied_message.voice: _type = "voice" file_id = replied_message.voice.file_id if replied_message.reply_markup: if not "~" in data: urls = extract_urls(replied_message.reply_markup) if urls: response = "\n".join([f"{name}=[{text}, {url}]" for name, text, url in urls]) data = data + response note = { "type": _type, "data": data, "file_id": file_id, } prefix = message.text.split()[0][0] chat_id = message.chat.id if prefix != USERBOT_PREFIX else USERBOT_ID await save_note(chat_id, name, note) await eor(message, text=f"__**Saved note {name}.**__") @app2.on_message( filters.command("notes", prefixes=USERBOT_PREFIX) & ~filters.forwarded & ~filters.via_bot & SUDOERS ) @app.on_message(filters.command("notes") & ~filters.private) @capture_err async def get_notes(_, message): prefix = message.text.split()[0][0] is_ubot = bool(prefix == USERBOT_PREFIX) chat_id = USERBOT_ID if is_ubot else message.chat.id _notes = await get_note_names(chat_id) if not _notes: return await eor(message, text="**No notes in this chat.**") _notes.sort() msg = f"List of notes in {'USERBOT' if is_ubot else message.chat.title}\n" for note in _notes: msg += f"**-** `{note}`\n" await eor(message, text=msg) @app2.on_message( filters.command("get", prefixes=USERBOT_PREFIX) & ~filters.forwarded & ~filters.via_bot & SUDOERS ) async def get_one_note_userbot(_, message): if len(message.text.split()) < 2: return await eor(message, text="Invalid arguments") name = message.text.split(None, 1)[1] _note = await get_note(USERBOT_ID, name) if not _note: return await eor(message, text="No such note.") if _note["type"] == "text": data = _note["data"] await eor( message, text=data, disable_web_page_preview=True, ) else: await message.reply_sticker(_note["data"]) @app.on_message(filters.regex(r"^#.+") & filters.text & ~filters.private) @capture_err async def get_one_note(_, message): name = message.text.replace("#", "", 1) if not name: return _note = await get_note(message.chat.id, name) if not _note: return type = _note["type"] data = _note["data"] file_id = _note["file_id"] keyb = None if data: if findall(r"\[.+\,.+\]", data): keyboard = extract_text_and_keyb(ikb, data) if keyboard: data, keyb = keyboard replied_message = message.reply_to_message if replied_message: if replied_message.from_user.id != message.from_user.id: message = replied_message if type == "text": await message.reply_text( text=data, reply_markup=keyb, disable_web_page_preview=True, ) if type == "sticker": await message.reply_sticker( sticker=file_id, ) if type == "animation": await message.reply_animation( animation=file_id, caption=data, reply_markup=keyb, ) if type == "photo": await message.reply_photo( photo=file_id, caption=data, reply_markup=keyb, ) if type == "document": await message.reply_document( document=file_id, caption=data, reply_markup=keyb, ) if type == "video": await message.reply_video( video=file_id, caption=data, reply_markup=keyb, ) if type == "video_note": await message.reply_video_note( video_note=file_id, ) if type == "audio": await message.reply_audio( audio=file_id, caption=data, reply_markup=keyb, ) if type == "voice": await message.reply_voice( voice=file_id, caption=data, reply_markup=keyb, ) @app2.on_message( filters.command("delete", prefixes=USERBOT_PREFIX) & ~filters.forwarded & ~filters.via_bot & SUDOERS ) @app.on_message(filters.command("delete") & ~filters.private) @adminsOnly("can_change_info") async def del_note(_, message): if len(message.command) < 2: return await eor(message, text="**Usage**\n__/delete [NOTE_NAME]__") name = message.text.split(None, 1)[1].strip() if not name: return await eor(message, text="**Usage**\n__/delete [NOTE_NAME]__") prefix = message.text.split()[0][0] is_ubot = bool(prefix == USERBOT_PREFIX) chat_id = USERBOT_ID if is_ubot else message.chat.id deleted = await delete_note(chat_id, name) if deleted: await eor(message, text=f"**Deleted note {name} successfully.**") else: await eor(message, text="**No such note.**")