filters.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. """
  2. MIT License
  3. Copyright (c) 2024 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. import re
  21. from pyrogram import filters
  22. from pyrogram.types import (
  23. CallbackQuery,
  24. InlineKeyboardButton,
  25. InlineKeyboardMarkup,
  26. )
  27. from wbb import app
  28. from wbb.core.decorators.errors import capture_err
  29. from wbb.core.decorators.permissions import adminsOnly
  30. from wbb.core.keyboard import ikb
  31. from wbb.modules.admin import member_permissions
  32. from wbb.modules.notes import extract_urls
  33. from wbb.utils.dbfunctions import (
  34. delete_filter,
  35. deleteall_filters,
  36. get_filter,
  37. get_filters_names,
  38. save_filter,
  39. )
  40. from wbb.utils.filter_groups import chat_filters_group
  41. from wbb.utils.functions import (
  42. check_format,
  43. extract_text_and_keyb,
  44. get_data_and_name,
  45. )
  46. __MODULE__ = "自动回复"
  47. __HELP__ = """/filters - 查看本群全部自动回复规则。
  48. /filter [关键词] - 回复一条消息,将其保存为关键词回复。
  49. 支持文字、动图、图片、文件、视频、圆形视频、音频和语音。
  50. 多词关键词可使用下划线,例如 `/filter 你好_呀` 对应“你好 呀”。
  51. /stop [关键词] - 删除指定自动回复。
  52. /stopall - 永久删除本群全部自动回复。
  53. 文字回复支持 Markdown 或 HTML,发送 /markdownhelp 查看格式说明。
  54. """
  55. @app.on_message(filters.command("filter") & ~filters.private)
  56. @adminsOnly("can_change_info")
  57. async def save_filters(_, message):
  58. try:
  59. if len(message.command) < 2:
  60. return await message.reply_text(
  61. "**用法:**\n回复一条消息并发送 "
  62. "/filter [关键词] [回复内容],即可保存自动回复。"
  63. )
  64. replied_message = message.reply_to_message
  65. if not replied_message:
  66. replied_message = message
  67. data, name = await get_data_and_name(replied_message, message)
  68. if data == "error":
  69. return await message.reply_text(
  70. "**用法:**\n__/filter [关键词] [回复内容]__\n"
  71. "`-----------或者-----------`\n"
  72. "回复一条消息并发送:\n/filter [关键词]"
  73. )
  74. if replied_message.text:
  75. _type = "text"
  76. file_id = None
  77. if replied_message.sticker:
  78. _type = "sticker"
  79. file_id = replied_message.sticker.file_id
  80. if replied_message.animation:
  81. _type = "animation"
  82. file_id = replied_message.animation.file_id
  83. if replied_message.photo:
  84. _type = "photo"
  85. file_id = replied_message.photo.file_id
  86. if replied_message.document:
  87. _type = "document"
  88. file_id = replied_message.document.file_id
  89. if replied_message.video:
  90. _type = "video"
  91. file_id = replied_message.video.file_id
  92. if replied_message.video_note:
  93. _type = "video_note"
  94. file_id = replied_message.video_note.file_id
  95. if replied_message.audio:
  96. _type = "audio"
  97. file_id = replied_message.audio.file_id
  98. if replied_message.voice:
  99. _type = "voice"
  100. file_id = replied_message.voice.file_id
  101. if replied_message.reply_markup and not re.findall(r"\[.+\,.+\]", data):
  102. urls = extract_urls(replied_message.reply_markup)
  103. if urls:
  104. response = "\n".join(
  105. [f"{name}=[{text}, {url}]" for name, text, url in urls]
  106. )
  107. data = data + response
  108. if data:
  109. data = await check_format(ikb, data)
  110. if not data:
  111. return await message.reply_text(
  112. "**消息格式不正确,请发送 /markdownhelp 查看说明。**"
  113. )
  114. name = name.replace("_", " ")
  115. _filter = {
  116. "type": _type,
  117. "data": data,
  118. "file_id": file_id,
  119. }
  120. chat_id = message.chat.id
  121. await save_filter(chat_id, name, _filter)
  122. return await message.reply_text(f"__**自动回复“{name}”已保存。**__")
  123. except UnboundLocalError:
  124. return await message.reply_text(
  125. "**无法读取被回复消息,请先转发该消息再重试。**"
  126. )
  127. @app.on_message(filters.command("filters") & ~filters.private)
  128. @capture_err
  129. async def get_filterss(_, message):
  130. _filters = await get_filters_names(message.chat.id)
  131. if not _filters:
  132. return await message.reply_text("**本群没有自动回复规则。**")
  133. _filters.sort()
  134. msg = f"{message.chat.title} 的自动回复规则:\n"
  135. for _filter in _filters:
  136. msg += f"**-** `{_filter}`\n"
  137. await message.reply_text(msg)
  138. @app.on_message(filters.command("stop") & ~filters.private)
  139. @adminsOnly("can_change_info")
  140. async def del_filter(_, message):
  141. if len(message.command) < 2:
  142. return await message.reply_text("**用法:**\n__/stop [关键词]__")
  143. name = message.text.split(None, 1)[1].strip()
  144. if not name:
  145. return await message.reply_text("**用法:**\n__/stop [关键词]__")
  146. chat_id = message.chat.id
  147. deleted = await delete_filter(chat_id, name)
  148. if deleted:
  149. await message.reply_text(f"**自动回复“{name}”已删除。**")
  150. else:
  151. await message.reply_text("**未找到该自动回复。**")
  152. @app.on_message(
  153. filters.text & ~filters.private & ~filters.channel & ~filters.via_bot & ~filters.forwarded,
  154. group=chat_filters_group,
  155. )
  156. @capture_err
  157. async def filters_re(_, message):
  158. from_user = message.from_user if message.from_user else message.sender_chat
  159. user_id = from_user.id
  160. chat_id = message.chat.id
  161. text = message.text.lower().strip()
  162. if not text:
  163. return
  164. chat_id = message.chat.id
  165. list_of_filters = await get_filters_names(chat_id)
  166. for word in list_of_filters:
  167. pattern = r"( |^|[^\w])" + re.escape(word) + r"( |$|[^\w])"
  168. if re.search(pattern, text, flags=re.IGNORECASE):
  169. _filter = await get_filter(chat_id, word)
  170. data_type = _filter["type"]
  171. data = _filter["data"]
  172. file_id = _filter.get("file_id")
  173. keyb = None
  174. if data:
  175. if "{chat}" in data:
  176. data = data.replace(
  177. "{chat}", message.chat.title
  178. )
  179. if "{name}" in data:
  180. data = data.replace(
  181. "{name}", (from_user.mention if message.from_user else from_user.title)
  182. )
  183. if re.findall(r"\[.+\,.+\]", data):
  184. keyboard = extract_text_and_keyb(ikb, data)
  185. if keyboard:
  186. data, keyb = keyboard
  187. replied_message = message.reply_to_message
  188. if replied_message:
  189. replied_user = replied_message.from_user if replied_message.from_user else replied_message.sender_chat
  190. if text.startswith("~"):
  191. await message.delete()
  192. if replied_user.id != from_user.id:
  193. message = replied_message
  194. if data_type == "text":
  195. await message.reply_text(
  196. text=data,
  197. reply_markup=keyb,
  198. disable_web_page_preview=True,
  199. )
  200. else:
  201. if not file_id:
  202. continue
  203. if data_type == "sticker":
  204. await message.reply_sticker(
  205. sticker=file_id,
  206. )
  207. if data_type == "animation":
  208. await message.reply_animation(
  209. animation=file_id,
  210. caption=data,
  211. reply_markup=keyb,
  212. )
  213. if data_type == "photo":
  214. await message.reply_photo(
  215. photo=file_id,
  216. caption=data,
  217. reply_markup=keyb,
  218. )
  219. if data_type == "document":
  220. await message.reply_document(
  221. document=file_id,
  222. caption=data,
  223. reply_markup=keyb,
  224. )
  225. if data_type == "video":
  226. await message.reply_video(
  227. video=file_id,
  228. caption=data,
  229. reply_markup=keyb,
  230. )
  231. if data_type == "video_note":
  232. await message.reply_video_note(
  233. video_note=file_id,
  234. )
  235. if data_type == "audio":
  236. await message.reply_audio(
  237. audio=file_id,
  238. caption=data,
  239. reply_markup=keyb,
  240. )
  241. if data_type == "voice":
  242. await message.reply_voice(
  243. voice=file_id,
  244. caption=data,
  245. reply_markup=keyb,
  246. )
  247. return # NOTE: Avoid filter spam
  248. @app.on_message(filters.command("stopall") & ~filters.private)
  249. @adminsOnly("can_change_info")
  250. async def stop_all(_, message):
  251. _filters = await get_filters_names(message.chat.id)
  252. if not _filters:
  253. await message.reply_text("**本群没有自动回复规则。**")
  254. else:
  255. keyboard = InlineKeyboardMarkup(
  256. [
  257. [
  258. InlineKeyboardButton(
  259. "确认删除", callback_data="stop_yes"
  260. ),
  261. InlineKeyboardButton("取消", callback_data="stop_no"),
  262. ]
  263. ]
  264. )
  265. await message.reply_text(
  266. "**确认永久删除本群全部自动回复吗?**",
  267. reply_markup=keyboard,
  268. )
  269. @app.on_callback_query(filters.regex("stop_(.*)"))
  270. async def stop_all_cb(_, cb):
  271. chat_id = cb.message.chat.id
  272. from_user = cb.from_user
  273. permissions = await member_permissions(chat_id, from_user.id)
  274. permission = "can_change_info"
  275. if permission not in permissions:
  276. return await cb.answer(
  277. "你没有修改群信息的权限。",
  278. show_alert=True,
  279. )
  280. input = cb.data.split("_", 1)[1]
  281. if input == "yes":
  282. stoped_all = await deleteall_filters(chat_id)
  283. if stoped_all:
  284. return await cb.message.edit(
  285. "**本群全部自动回复已删除。**"
  286. )
  287. if input == "no":
  288. await cb.message.reply_to_message.delete()
  289. await cb.message.delete()