chatbot.py 5.3 KB

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