chatbot.py 5.9 KB

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