spam.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. """
  2. MIT License
  3. Copyright (c) 2021 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 pyrogram import filters
  21. from pyrogram.types import (CallbackQuery, InlineKeyboardButton,
  22. InlineKeyboardMarkup, Message)
  23. from wbb import LOG_GROUP_ID, SUDOERS, app, arq
  24. from wbb.core.decorators.errors import capture_err
  25. from wbb.modules.admin import list_admins, member_permissions
  26. from wbb.utils.filter_groups import spam_protection_group
  27. __MODULE__ = "AntiSpam"
  28. __HELP__ = """
  29. **Antispam helps protect your group from spam.**
  30. - It's still new so it might be a bit inaccurate.
  31. - Currently, it only alerts for spam and gives you a button to delete the message,
  32. but in the future, it will delete the spam message automatically.
  33. **Commands:**
  34. - /spam
  35. To mark a message as spam, this will help devs to
  36. improve spam protection algorithm.
  37. As of now, you cannot turn this off, but we'll add an enable/disable command in the future
  38. """
  39. @app.on_message(
  40. (filters.text | filters.caption) & ~filters.private & ~filters.me,
  41. group=spam_protection_group,
  42. )
  43. async def spam_protection_func(_, message: Message):
  44. text = message.text if message.text else message.caption
  45. user = message.from_user
  46. chat_id = message.chat.id
  47. if not text or not user:
  48. return
  49. # We'll handle admins only if it's spam, ignore only sudo users for now.
  50. if user.id in SUDOERS:
  51. return
  52. data = await arq.nlp(text)
  53. data = data.result[0]
  54. if not data.is_spam:
  55. return
  56. if user.id in (await list_admins(chat_id)):
  57. return
  58. text = f"""
  59. 🚨 **SPAM DETECTED** 🚨
  60. **User:** {message.from_user.mention}
  61. **Message:** [Link]({message.link})
  62. **Spam Probability:** {data.spam_probability} %
  63. **Action:** Alerted
  64. """
  65. keyboard = InlineKeyboardMarkup(
  66. [
  67. [
  68. InlineKeyboardButton(
  69. text="Yes it's spam", callback_data="s_p_spam"
  70. )
  71. ],
  72. [
  73. InlineKeyboardButton(
  74. text="No, it's not spam",
  75. callback_data="s_p_ham",
  76. )
  77. ],
  78. ]
  79. )
  80. await message.reply_text(
  81. text=text,
  82. reply_markup=keyboard,
  83. disable_web_page_preview=True,
  84. )
  85. dev_forward = (
  86. "If you're not a developer of WBB, forward this message to devs, "
  87. + "so that they can use it to improve spam protection algorithm."
  88. )
  89. @app.on_callback_query(filters.regex("s_p_"))
  90. async def spam_p_callback(_, cq: CallbackQuery):
  91. from_user = cq.from_user
  92. chat_id = cq.message.chat.id
  93. permissions = await member_permissions(chat_id, from_user.id)
  94. permission = "can_delete_messages"
  95. if permission not in permissions:
  96. return await cq.answer(
  97. "You don't have enough permissions to perform this action.\n"
  98. + f"Permission needed: {permission}",
  99. show_alert=True,
  100. )
  101. if cq.data.split("_")[-1] == "spam":
  102. try:
  103. await cq.message.reply_to_message.delete()
  104. except Exception:
  105. await cq.message.delete()
  106. return
  107. text = cq.message.text.markdown
  108. text = text.replace(
  109. "Alerted", f"Deleted Message With {from_user.mention}'s Approval."
  110. )
  111. await cq.message.edit(text)
  112. if cq.message.reply_to_message:
  113. text = f"**ADMINS OF {chat_id} FLAGGED THIS MESSAGE AS SPAM**\n\n"
  114. text += f"`{cq.message.reply_to_message.text.markdown}`\n\n__{dev_forward}__"
  115. return await app.send_message(
  116. LOG_GROUP_ID, text, disable_web_page_preview=True
  117. )
  118. return
  119. await cq.message.delete()
  120. if cq.message.reply_to_message:
  121. text = f"**ADMINS OF {chat_id} FLAGGED THIS MESSAGE AS NOT SPAM**\n\n"
  122. text += f"`{cq.message.reply_to_message.text.markdown}`\n\n__{dev_forward}__"
  123. return await app.send_message(
  124. LOG_GROUP_ID, text, disable_web_page_preview=True
  125. )
  126. @app.on_message(filters.command("spam") & ~filters.private)
  127. async def spam_flag_func(_, message: Message):
  128. if not message.reply_to_message:
  129. return await message.reply_text(
  130. "Reply to a message to flag it as spam"
  131. )
  132. r = message.reply_to_message
  133. if not r.text and not r.caption:
  134. return await message.reply_text(
  135. "Reply to a text message to flag it as spam"
  136. )
  137. text = r.text or r.caption
  138. if not text:
  139. return await message.reply_text(
  140. "Reply to a text message to flag it as spam"
  141. )
  142. msg = f"""
  143. **ADMINS OF {message.chat.id} FLAGGED THIS MESSAGE AS SPAM**
  144. ```
  145. {text.markdown}
  146. ```
  147. __{dev_forward}__
  148. """
  149. await message.reply_text(
  150. "Message was flagged as spam, Devs will use it to improve spam protection algorithm."
  151. )
  152. await app.send_message(LOG_GROUP_ID, msg, disable_web_page_preview=True)