chatbot.py 4.9 KB

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