chatbot.py 4.9 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 asyncio import gather, sleep
  21. from pyrogram import filters
  22. from pyrogram.types import Message
  23. from wbb import (
  24. BOT_ID,
  25. SUDOERS,
  26. USERBOT_ID,
  27. USERBOT_PREFIX,
  28. USERBOT_USERNAME,
  29. app,
  30. app2,
  31. arq,
  32. eor,
  33. )
  34. from wbb.core.decorators.errors import capture_err
  35. from wbb.utils.filter_groups import chatbot_group
  36. __MODULE__ = "ChatBot"
  37. __HELP__ = """
  38. /chatbot [ENABLE|DISABLE] To Enable Or Disable ChatBot In Your Chat.
  39. There's one module of this available for userbot also
  40. check userbot module help."""
  41. active_chats_bot = []
  42. active_chats_ubot = []
  43. async def chat_bot_toggle(db, message: Message):
  44. status = message.text.split(None, 1)[1].lower()
  45. chat_id = message.chat.id
  46. if status == "enable":
  47. if chat_id not in db:
  48. db.append(chat_id)
  49. text = "Chatbot Enabled!"
  50. return await eor(message, text=text)
  51. await eor(message, text="ChatBot Is Already Enabled.")
  52. elif status == "disable":
  53. if chat_id in db:
  54. db.remove(chat_id)
  55. return await eor(message, text="Chatbot Disabled!")
  56. await eor(message, text="ChatBot Is Already Disabled.")
  57. else:
  58. await eor(message, text="**Usage:**\n/chatbot [ENABLE|DISABLE]")
  59. # Enabled | Disable Chatbot
  60. @app.on_message(filters.command("chatbot") & ~filters.edited)
  61. @capture_err
  62. async def chatbot_status(_, message: Message):
  63. if len(message.command) != 2:
  64. return await eor(message, text="**Usage:**\n/chatbot [ENABLE|DISABLE]")
  65. await chat_bot_toggle(active_chats_bot, message)
  66. async def lunaQuery(query: str, user_id: int):
  67. luna = await arq.luna(query, user_id)
  68. return luna.result
  69. async def type_and_send(message: Message):
  70. chat_id = message.chat.id
  71. user_id = message.from_user.id if message.from_user else 0
  72. query = message.text.strip()
  73. await message._client.send_chat_action(chat_id, "typing")
  74. response, _ = await gather(lunaQuery(query, user_id), sleep(3))
  75. await message.reply_text(response)
  76. await message._client.send_chat_action(chat_id, "cancel")
  77. @app.on_message(
  78. filters.text
  79. & filters.reply
  80. & ~filters.bot
  81. & ~filters.via_bot
  82. & ~filters.forwarded
  83. & ~filters.edited,
  84. group=chatbot_group,
  85. )
  86. @capture_err
  87. async def chatbot_talk(_, message: Message):
  88. if message.chat.id not in active_chats_bot:
  89. return
  90. if not message.reply_to_message:
  91. return
  92. if not message.reply_to_message.from_user:
  93. return
  94. if message.reply_to_message.from_user.id != BOT_ID:
  95. return
  96. await type_and_send(message)
  97. # FOR USERBOT
  98. @app2.on_message(
  99. filters.command("chatbot", prefixes=USERBOT_PREFIX)
  100. & ~filters.edited
  101. & SUDOERS
  102. )
  103. @capture_err
  104. async def chatbot_status_ubot(_, message: Message):
  105. if len(message.text.split()) != 2:
  106. return await eor(message, text="**Usage:**\n.chatbot [ENABLE|DISABLE]")
  107. await chat_bot_toggle(active_chats_ubot, message)
  108. @app2.on_message(
  109. ~filters.me & ~filters.private & filters.text & ~filters.edited,
  110. group=chatbot_group,
  111. )
  112. @capture_err
  113. async def chatbot_talk_ubot(_, message: Message):
  114. if message.chat.id not in active_chats_ubot:
  115. return
  116. username = "@" + str(USERBOT_USERNAME)
  117. if message.reply_to_message:
  118. if not message.reply_to_message.from_user:
  119. return
  120. if (
  121. message.reply_to_message.from_user.id != USERBOT_ID
  122. and username not in message.text
  123. ):
  124. return
  125. else:
  126. if username not in message.text:
  127. return
  128. await type_and_send(message)
  129. @app2.on_message(
  130. filters.text & filters.private & ~filters.me & ~filters.edited,
  131. group=(chatbot_group + 1),
  132. )
  133. @capture_err
  134. async def chatbot_talk_ubot_pm(_, message: Message):
  135. if message.chat.id not in active_chats_ubot:
  136. return
  137. await type_and_send(message)