chatbot.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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: str, user_id: int):
  60. luna = await arq.luna(query, user_id)
  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. await app2.send_chat_action(message.chat.id, "typing")
  76. response = await lunaQuery(query, message.from_user.id if message.from_user else 0)
  77. await app2.send_chat_action(message.chat.id, "cancel")
  78. await message.reply_text(response)
  79. """ FOR USERBOT """
  80. @app2.on_message(
  81. filters.command("chatbot", prefixes=USERBOT_PREFIX)
  82. & ~filters.edited
  83. & filters.user(SUDOERS)
  84. )
  85. @capture_err
  86. async def chatbot_status_ubot(_, message):
  87. global active_chats_ubot
  88. if len(message.text.split()) != 2:
  89. await edit_or_reply(message, text="**Usage**\n.chatbot [ON|OFF]")
  90. return
  91. status = message.text.split(None, 1)[1]
  92. chat_id = message.chat.id
  93. if status == "ON" or status == "on" or status == "On":
  94. if chat_id not in active_chats_ubot:
  95. active_chats_ubot.append(chat_id)
  96. text = "Chatbot Enabled Reply To Any Message " + "Of Mine To Get A Reply"
  97. await edit_or_reply(message, text=text)
  98. return
  99. await edit_or_reply(message, text="ChatBot Is Already Enabled.")
  100. return
  101. elif status == "OFF" or status == "off" or status == "Off":
  102. if chat_id in active_chats_ubot:
  103. active_chats_ubot.remove(chat_id)
  104. await edit_or_reply(message, text="Chatbot Disabled!")
  105. return
  106. await edit_or_reply(message, text="ChatBot Is Already Disabled.")
  107. return
  108. else:
  109. await edit_or_reply(message, text="**Usage**\n/chatbot [ON|OFF]")
  110. @app2.on_message(
  111. ~filters.me & ~filters.private & filters.text & ~filters.edited, group=chatbot_group
  112. )
  113. @capture_err
  114. async def chatbot_talk_ubot(_, message):
  115. if message.chat.id not in active_chats_ubot:
  116. return
  117. username = "@" + str(USERBOT_USERNAME)
  118. query = message.text
  119. if message.reply_to_message:
  120. if (
  121. message.reply_to_message.from_user.id != USERBOT_ID
  122. and username not in query
  123. ):
  124. return
  125. else:
  126. if username not in query:
  127. return
  128. await app2.send_chat_action(message.chat.id, "typing")
  129. response = await lunaQuery(query, message.from_user.id if message.from_user else 0)
  130. await app2.send_chat_action(message.chat.id, "cancel")
  131. await message.reply_text(response)
  132. @app2.on_message(
  133. filters.text & filters.private & ~filters.me & ~filters.edited,
  134. group=(chatbot_group + 1),
  135. )
  136. @capture_err
  137. async def chatbot_talk_ubot_pm(_, message):
  138. if message.chat.id not in active_chats_ubot:
  139. return
  140. query = message.text
  141. await app2.send_chat_action(message.chat.id, "typing")
  142. response = await lunaQuery(query, message.from_user.id if message.from_user else 0)
  143. await message.reply_text(response)
  144. await app2.send_chat_action(message.chat.id, "cancel")