chatbot.py 5.9 KB

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