chatbot.py 5.4 KB

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