chatbot.py 5.2 KB

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