chatbot.py 5.4 KB

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