chatbot.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 wbb import (BOT_ID, SUDOERS, USERBOT_ID, USERBOT_PREFIX, USERBOT_USERNAME,
  22. app, app2, arq)
  23. from wbb.core.decorators.errors import capture_err
  24. from wbb.modules.userbot import edit_or_reply
  25. from wbb.utils.filter_groups import chatbot_group
  26. __MODULE__ = "ChatBot"
  27. __HELP__ = """
  28. /chatbot [ON|OFF] To Enable Or Disable ChatBot In Your Chat.
  29. .chatbot [ON|OFF] To Do The Same For Userbot."""
  30. active_chats_bot = []
  31. active_chats_ubot = []
  32. # Enabled | Disable Chatbot
  33. @app.on_message(filters.command("chatbot") & ~filters.edited)
  34. @capture_err
  35. async def chatbot_status(_, message):
  36. global active_chats_bot
  37. if len(message.command) != 2:
  38. await message.reply_text("**Usage**\n/chatbot [ON|OFF]")
  39. return
  40. status = message.text.split(None, 1)[1]
  41. chat_id = message.chat.id
  42. if status == "ON" or status == "on" or status == "On":
  43. if chat_id not in active_chats_bot:
  44. active_chats_bot.append(chat_id)
  45. text = "Chatbot Enabled Reply To Any Message " + "Of Mine To Get A Reply"
  46. await message.reply_text(text)
  47. return
  48. await message.reply_text("ChatBot Is Already Enabled.")
  49. return
  50. elif status == "OFF" or status == "off" or status == "Off":
  51. if chat_id in active_chats_bot:
  52. active_chats_bot.remove(chat_id)
  53. await message.reply_text("Chatbot Disabled!")
  54. return
  55. await message.reply_text("ChatBot Is Already Disabled.")
  56. return
  57. else:
  58. await message.reply_text("**Usage**\n/chatbot [ON|OFF]")
  59. async def lunaQuery(query):
  60. luna = await arq.luna(query)
  61. return luna.result
  62. @app.on_message(
  63. filters.text & filters.reply & ~filters.bot & ~filters.via_bot & ~filters.forwarded,
  64. group=chatbot_group,
  65. )
  66. @capture_err
  67. async def chatbot_talk(_, message):
  68. if message.chat.id not in active_chats_bot:
  69. return
  70. if not message.reply_to_message:
  71. return
  72. if message.reply_to_message.from_user.id != BOT_ID:
  73. return
  74. query = message.text
  75. response = await lunaQuery(query)
  76. await message.reply_text(response)
  77. """ FOR USERBOT """
  78. @app2.on_message(
  79. filters.command("chatbot", prefixes=USERBOT_PREFIX)
  80. & ~filters.edited
  81. & filters.user(SUDOERS)
  82. )
  83. @capture_err
  84. async def chatbot_status_ubot(_, message):
  85. global active_chats_ubot
  86. if len(message.text.split()) != 2:
  87. await edit_or_reply(message, text="**Usage**\n.chatbot [ON|OFF]")
  88. return
  89. status = message.text.split(None, 1)[1]
  90. chat_id = message.chat.id
  91. if status == "ON" or status == "on" or status == "On":
  92. if chat_id not in active_chats_ubot:
  93. active_chats_ubot.append(chat_id)
  94. text = "Chatbot Enabled Reply To Any Message " + "Of Mine To Get A Reply"
  95. await edit_or_reply(message, text=text)
  96. return
  97. await edit_or_reply(message, text="ChatBot Is Already Enabled.")
  98. return
  99. elif status == "OFF" or status == "off" or status == "Off":
  100. if chat_id in active_chats_ubot:
  101. active_chats_ubot.remove(chat_id)
  102. await edit_or_reply(message, text="Chatbot Disabled!")
  103. return
  104. await edit_or_reply(message, text="ChatBot Is Already Disabled.")
  105. return
  106. else:
  107. await edit_or_reply(message, text="**Usage**\n/chatbot [ON|OFF]")
  108. @app2.on_message(
  109. ~filters.me & ~filters.private & filters.text & ~filters.edited, group=chatbot_group
  110. )
  111. @capture_err
  112. async def chatbot_talk_ubot(_, message):
  113. if message.chat.id not in active_chats_ubot:
  114. return
  115. username = "@" + str(USERBOT_USERNAME)
  116. query = message.text
  117. if message.reply_to_message:
  118. if (
  119. message.reply_to_message.from_user.id != USERBOT_ID
  120. and username not in query
  121. ):
  122. return
  123. else:
  124. if username not in query:
  125. return
  126. response = await lunaQuery(query)
  127. await message.reply_text(response)
  128. @app2.on_message(
  129. filters.text & filters.private & ~filters.me & ~filters.edited,
  130. group=(chatbot_group + 1),
  131. )
  132. @capture_err
  133. async def chatbot_talk_ubot_pm(_, message):
  134. if message.chat.id not in active_chats_ubot:
  135. return
  136. query = message.text
  137. await app2.send_chat_action(message.chat.id, "typing")
  138. response = await lunaQuery(query)
  139. await message.reply_text(response)
  140. await app2.send_chat_action(message.chat.id, "cancel")