notes.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. """
  2. MIT License
  3. Copyright (c) 2023 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. from re import findall
  21. from pyrogram import filters
  22. from wbb import SUDOERS, USERBOT_ID, USERBOT_PREFIX, app, app2, eor
  23. from wbb.core.decorators.errors import capture_err
  24. from wbb.core.decorators.permissions import adminsOnly
  25. from wbb.core.keyboard import ikb
  26. from wbb.utils.dbfunctions import delete_note, get_note, get_note_names, save_note
  27. from wbb.utils.functions import extract_text_and_keyb
  28. __MODULE__ = "Notes"
  29. __HELP__ = """/notes To Get All The Notes In The Chat.
  30. /save [NOTE_NAME] To Save A Note.
  31. Supported note types are Text, Animation, Photo, Document, Video, video notes, Audio, Voice
  32. To change caption of any files use.\n/save [NOTE_NAME] [NEW_CAPTION]
  33. #NOTE_NAME To Get A Note.
  34. /delete [NOTE_NAME] To Delete A Note.
  35. Checkout /markdownhelp to know more about formattings and other syntax.
  36. """
  37. def extract_urls(reply_markup):
  38. urls = []
  39. if reply_markup.inline_keyboard:
  40. buttons = reply_markup.inline_keyboard
  41. for i, row in enumerate(buttons):
  42. for j, button in enumerate(row):
  43. if button.url:
  44. name = "\n~\nbutton" if i * len(row) + j + 1 == 1 else f"button{i * len(row) + j + 1}"
  45. urls.append((f"{name}", button.text, button.url))
  46. return urls
  47. @app2.on_message(
  48. filters.command("save", prefixes=USERBOT_PREFIX)
  49. & ~filters.forwarded
  50. & ~filters.via_bot
  51. & SUDOERS
  52. )
  53. @app.on_message(filters.command("save") & ~filters.private)
  54. @adminsOnly("can_change_info")
  55. async def save_notee(_, message):
  56. if len(message.command) < 2 or not message.reply_to_message:
  57. await eor(
  58. message,
  59. text="**Usage:**\nReply to a message with /save [NOTE_NAME] to save a new note.",
  60. )
  61. else:
  62. name = message.text.split(None, 1)[1].strip()
  63. if not name:
  64. return await eor(message, text="**Usage**\n__/save [NOTE_NAME]__")
  65. replied_message = message.reply_to_message
  66. text = name.split(" ", 1)
  67. if len(text) > 1:
  68. name = text[0]
  69. data = text[1].strip()
  70. if replied_message.sticker or replied_message.video_note:
  71. data = None
  72. else:
  73. if replied_message.sticker or replied_message.video_note:
  74. data = None
  75. elif not replied_message.text and not replied_message.caption:
  76. data = None
  77. else:
  78. data = replied_message.text.markdown if replied_message.text else replied_message.caption.markdown
  79. if replied_message.text:
  80. _type = "text"
  81. file_id = None
  82. if replied_message.sticker:
  83. _type = "sticker"
  84. file_id = replied_message.sticker.file_id
  85. if replied_message.animation:
  86. _type = "animation"
  87. file_id = replied_message.animation.file_id
  88. if replied_message.photo:
  89. _type = "photo"
  90. file_id = replied_message.photo.file_id
  91. if replied_message.document:
  92. _type = "document"
  93. file_id = replied_message.document.file_id
  94. if replied_message.video:
  95. _type = "video"
  96. file_id = replied_message.video.file_id
  97. if replied_message.video_note:
  98. _type = "video_note"
  99. file_id = replied_message.video_note.file_id
  100. if replied_message.audio:
  101. _type = "audio"
  102. file_id = replied_message.audio.file_id
  103. if replied_message.voice:
  104. _type = "voice"
  105. file_id = replied_message.voice.file_id
  106. if replied_message.reply_markup:
  107. if not "~" in data:
  108. urls = extract_urls(replied_message.reply_markup)
  109. if urls:
  110. response = "\n".join([f"{name}=[{text}, {url}]" for name, text, url in urls])
  111. data = data + response
  112. note = {
  113. "type": _type,
  114. "data": data,
  115. "file_id": file_id,
  116. }
  117. prefix = message.text.split()[0][0]
  118. chat_id = message.chat.id if prefix != USERBOT_PREFIX else USERBOT_ID
  119. await save_note(chat_id, name, note)
  120. await eor(message, text=f"__**Saved note {name}.**__")
  121. @app2.on_message(
  122. filters.command("notes", prefixes=USERBOT_PREFIX)
  123. & ~filters.forwarded
  124. & ~filters.via_bot
  125. & SUDOERS
  126. )
  127. @app.on_message(filters.command("notes") & ~filters.private)
  128. @capture_err
  129. async def get_notes(_, message):
  130. prefix = message.text.split()[0][0]
  131. is_ubot = bool(prefix == USERBOT_PREFIX)
  132. chat_id = USERBOT_ID if is_ubot else message.chat.id
  133. _notes = await get_note_names(chat_id)
  134. if not _notes:
  135. return await eor(message, text="**No notes in this chat.**")
  136. _notes.sort()
  137. msg = f"List of notes in {'USERBOT' if is_ubot else message.chat.title}\n"
  138. for note in _notes:
  139. msg += f"**-** `{note}`\n"
  140. await eor(message, text=msg)
  141. @app2.on_message(
  142. filters.command("get", prefixes=USERBOT_PREFIX)
  143. & ~filters.forwarded
  144. & ~filters.via_bot
  145. & SUDOERS
  146. )
  147. async def get_one_note_userbot(_, message):
  148. if len(message.text.split()) < 2:
  149. return await eor(message, text="Invalid arguments")
  150. name = message.text.split(None, 1)[1]
  151. _note = await get_note(USERBOT_ID, name)
  152. if not _note:
  153. return await eor(message, text="No such note.")
  154. if _note["type"] == "text":
  155. data = _note["data"]
  156. await eor(
  157. message,
  158. text=data,
  159. disable_web_page_preview=True,
  160. )
  161. else:
  162. await message.reply_sticker(_note["data"])
  163. @app.on_message(filters.regex(r"^#.+") & filters.text & ~filters.private)
  164. @capture_err
  165. async def get_one_note(_, message):
  166. name = message.text.replace("#", "", 1)
  167. if not name:
  168. return
  169. _note = await get_note(message.chat.id, name)
  170. if not _note:
  171. return
  172. type = _note["type"]
  173. data = _note["data"]
  174. file_id = _note["file_id"]
  175. keyb = None
  176. if data:
  177. if findall(r"\[.+\,.+\]", data):
  178. keyboard = extract_text_and_keyb(ikb, data)
  179. if keyboard:
  180. data, keyb = keyboard
  181. replied_message = message.reply_to_message
  182. if replied_message:
  183. if replied_message.from_user.id != message.from_user.id:
  184. message = replied_message
  185. if type == "text":
  186. await message.reply_text(
  187. text=data,
  188. reply_markup=keyb,
  189. disable_web_page_preview=True,
  190. )
  191. if type == "sticker":
  192. await message.reply_sticker(
  193. sticker=file_id,
  194. )
  195. if type == "animation":
  196. await message.reply_animation(
  197. animation=file_id,
  198. caption=data,
  199. reply_markup=keyb,
  200. )
  201. if type == "photo":
  202. await message.reply_photo(
  203. photo=file_id,
  204. caption=data,
  205. reply_markup=keyb,
  206. )
  207. if type == "document":
  208. await message.reply_document(
  209. document=file_id,
  210. caption=data,
  211. reply_markup=keyb,
  212. )
  213. if type == "video":
  214. await message.reply_video(
  215. video=file_id,
  216. caption=data,
  217. reply_markup=keyb,
  218. )
  219. if type == "video_note":
  220. await message.reply_video_note(
  221. video_note=file_id,
  222. )
  223. if type == "audio":
  224. await message.reply_audio(
  225. audio=file_id,
  226. caption=data,
  227. reply_markup=keyb,
  228. )
  229. if type == "voice":
  230. await message.reply_voice(
  231. voice=file_id,
  232. caption=data,
  233. reply_markup=keyb,
  234. )
  235. @app2.on_message(
  236. filters.command("delete", prefixes=USERBOT_PREFIX)
  237. & ~filters.forwarded
  238. & ~filters.via_bot
  239. & SUDOERS
  240. )
  241. @app.on_message(filters.command("delete") & ~filters.private)
  242. @adminsOnly("can_change_info")
  243. async def del_note(_, message):
  244. if len(message.command) < 2:
  245. return await eor(message, text="**Usage**\n__/delete [NOTE_NAME]__")
  246. name = message.text.split(None, 1)[1].strip()
  247. if not name:
  248. return await eor(message, text="**Usage**\n__/delete [NOTE_NAME]__")
  249. prefix = message.text.split()[0][0]
  250. is_ubot = bool(prefix == USERBOT_PREFIX)
  251. chat_id = USERBOT_ID if is_ubot else message.chat.id
  252. deleted = await delete_note(chat_id, name)
  253. if deleted:
  254. await eor(message, text=f"**Deleted note {name} successfully.**")
  255. else:
  256. await eor(message, text="**No such note.**")