pipes.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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, SUDOERS, USERBOT_ID, BOT_ID
  21. from wbb.core.decorators.errors import capture_err
  22. from wbb.utils.filter_groups import pipes_group
  23. from wbb.utils.dbfunctions import is_pipe_active, activate_pipe, deactivate_pipe, show_pipes
  24. from pyrogram import filters
  25. from pyrogram.types import Message
  26. import asyncio
  27. __MODULE__ = "Pipes"
  28. __HELP__ = """
  29. **THIS MODULE IS ONLY FOR DEVS**
  30. Use this module to create a pipe that will forward messages of one chat/channel to another.
  31. /activate_pipe [FROM_CHAT_ID] [TO_CHAT_ID] [BOT|USERBOT]
  32. Active a pipe.
  33. choose 'BOT' or 'USERBOT' according to your needs, this will decide
  34. which client will fetch the message from 'FROM_CHAT'.
  35. /deactivate_pipe [FROM_CHAT_ID] [TO_CHAT_ID]
  36. Deactivete a pipe.
  37. /show_pipes
  38. Show all the active pipes.
  39. """
  40. pipes_list_bot = []
  41. pipes_list_userbot = []
  42. async def load_pipes():
  43. print("[INFO]: LOADING PIPES")
  44. global pipes_list_bot, pipes_list_userbot
  45. pipes_list_bot = []
  46. pipes_list_userbot = []
  47. pipes = await show_pipes()
  48. for pipe in pipes:
  49. if pipe['fetcher'] == "bot":
  50. pipes_list_bot.append(pipe)
  51. continue
  52. pipes_list_userbot.append(pipe)
  53. loop = asyncio.get_running_loop()
  54. loop.create_task(load_pipes())
  55. @app.on_message(~filters.me, group=pipes_group)
  56. @capture_err
  57. async def pipes_worker_bot(_, message: Message):
  58. for pipe in pipes_list_bot:
  59. if pipe['from_chat_id'] == message.chat.id:
  60. await message.forward(pipe['to_chat_id'])
  61. @app2.on_message(~filters.me, group=pipes_group)
  62. @capture_err
  63. async def pipes_worker_userbot(_, message: Message):
  64. for pipe in pipes_list_userbot:
  65. if pipe['from_chat_id'] == message.chat.id:
  66. if not message.text:
  67. m, temp = await asyncio.gather(
  68. app.listen(USERBOT_ID),
  69. message.copy(BOT_ID)
  70. )
  71. caption = f"Forwarded from `{pipe['from_chat_id']}`"
  72. caption = f"{temp.caption}\n\n{caption}" if temp.caption else caption
  73. await app.copy_message(
  74. pipe['to_chat_id'],
  75. USERBOT_ID,
  76. m.message_id,
  77. caption=caption
  78. )
  79. await asyncio.sleep(10)
  80. await temp.delete()
  81. return
  82. caption = f"Forwarded from `{pipe['from_chat_id']}`"
  83. await app.send_message(
  84. pipe['to_chat_id'],
  85. text=message.text + "\n\n" + caption
  86. )
  87. @app.on_message(filters.command("activate_pipe") & filters.user(SUDOERS))
  88. @capture_err
  89. async def activate_pipe_func(_, message: Message):
  90. if len(message.command) != 4:
  91. await message.reply_text("**Usage:**\n/activate_pipe [FROM_CHAT_ID] [TO_CHAT_ID] [BOT|USERBOT]")
  92. return
  93. text = message.text.strip().split()
  94. from_chat = int(text[1])
  95. to_chat = int(text[2])
  96. fetcher = text[3].lower()
  97. if fetcher != "bot" and fetcher != "userbot":
  98. await message.reply_text("Wrong fetcher, see help menu.")
  99. return
  100. if await is_pipe_active(from_chat, to_chat):
  101. await message.reply_text("This pipe is already active.")
  102. return
  103. await activate_pipe(from_chat, to_chat, fetcher)
  104. await load_pipes()
  105. await message.reply_text("Activated pipe.")
  106. @app.on_message(filters.command("deactivate_pipe") & filters.user(SUDOERS))
  107. @capture_err
  108. async def deactivate_pipe_func(_, message: Message):
  109. if len(message.command) != 3:
  110. await message.reply_text("**Usage:**\n/deactivate_pipe [FROM_CHAT_ID] [TO_CHAT_ID]")
  111. return
  112. text = message.text.strip().split()
  113. from_chat = int(text[1])
  114. to_chat = int(text[2])
  115. if not await is_pipe_active(from_chat, to_chat):
  116. await message.reply_text("This pipe is already inactive.")
  117. return
  118. await deactivate_pipe(from_chat, to_chat)
  119. await load_pipes()
  120. await message.reply_text("Deactivated pipe.")
  121. @app.on_message(filters.command("pipes") & filters.user(SUDOERS))
  122. @capture_err
  123. async def show_pipes_func(_, message: Message):
  124. pipes = pipes_list_bot + pipes_list_userbot
  125. if not pipes:
  126. await message.reply_text("No pipe is active.")
  127. return
  128. text = ""
  129. for count, pipe in enumerate(pipes, 1):
  130. text += f"**Pipe:** `{count}`\n**From:** `{pipe['from_chat_id']}`\n" \
  131. + f"**To:** `{pipe['to_chat_id']}`\n**Fetcher:** `{pipe['fetcher']}`\n\n"
  132. await message.reply_text(text)