pipes.py 5.4 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 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. global pipes_list_bot, pipes_list_userbot
  44. pipes_list_bot = []
  45. pipes_list_userbot = []
  46. pipes = await show_pipes()
  47. for pipe in pipes:
  48. if pipe['fetcher'] == "bot":
  49. pipes_list_bot.append(pipe)
  50. continue
  51. pipes_list_userbot.append(pipe)
  52. loop = asyncio.get_running_loop()
  53. loop.create_task(load_pipes())
  54. @app.on_message(~filters.me, group=pipes_group)
  55. @capture_err
  56. async def pipes_worker_bot(_, message: Message):
  57. for pipe in pipes_list_bot:
  58. if pipe['from_chat_id'] == message.chat.id:
  59. await message.forward(pipe['to_chat_id'])
  60. @app2.on_message(~filters.me, group=pipes_group)
  61. @capture_err
  62. async def pipes_worker_userbot(_, message: Message):
  63. for pipe in pipes_list_userbot:
  64. if pipe['from_chat_id'] == message.chat.id:
  65. if not message.text:
  66. m, temp = await asyncio.gather(
  67. app.listen(USERBOT_ID),
  68. message.copy(BOT_ID)
  69. )
  70. caption = f"Forwarded from `{pipe['from_chat_id']}`"
  71. caption = f"{temp.caption}\n\n{caption}" if temp.caption else caption
  72. await app.copy_message(
  73. pipe['to_chat_id'],
  74. USERBOT_ID,
  75. m.message_id,
  76. caption=caption
  77. )
  78. await asyncio.sleep(10)
  79. await temp.delete()
  80. return
  81. caption = f"Forwarded from `{pipe['from_chat_id']}`"
  82. await app.send_message(
  83. pipe['to_chat_id'],
  84. text=message.text + "\n\n" + caption
  85. )
  86. @app.on_message(filters.command("activate_pipe") & filters.user(SUDOERS))
  87. @capture_err
  88. async def activate_pipe_func(_, message: Message):
  89. if len(message.command) != 4:
  90. await message.reply_text("**Usage:**\n/activate_pipe [FROM_CHAT_ID] [TO_CHAT_ID] [BOT|USERBOT]")
  91. return
  92. text = message.text.strip().split()
  93. from_chat = int(text[1])
  94. to_chat = int(text[2])
  95. fetcher = text[3].lower()
  96. if fetcher != "bot" and fetcher != "userbot":
  97. await message.reply_text("Wrong fetcher, see help menu.")
  98. return
  99. if await is_pipe_active(from_chat, to_chat):
  100. await message.reply_text("This pipe is already active.")
  101. return
  102. await activate_pipe(from_chat, to_chat, fetcher)
  103. await load_pipes()
  104. await message.reply_text("Activated pipe.")
  105. @app.on_message(filters.command("deactivate_pipe") & filters.user(SUDOERS))
  106. @capture_err
  107. async def deactivate_pipe_func(_, message: Message):
  108. if len(message.command) != 3:
  109. await message.reply_text("**Usage:**\n/deactivate_pipe [FROM_CHAT_ID] [TO_CHAT_ID]")
  110. return
  111. text = message.text.strip().split()
  112. from_chat = int(text[1])
  113. to_chat = int(text[2])
  114. if not await is_pipe_active(from_chat, to_chat):
  115. await message.reply_text("This pipe is already inactive.")
  116. return
  117. await deactivate_pipe(from_chat, to_chat)
  118. await load_pipes()
  119. await message.reply_text("Deactivated pipe.")
  120. @app.on_message(filters.command("pipes") & filters.user(SUDOERS))
  121. @capture_err
  122. async def show_pipes_func(_, message: Message):
  123. pipes = pipes_list_bot + pipes_list_userbot
  124. if not pipes:
  125. await message.reply_text("No pipe is active.")
  126. return
  127. text = ""
  128. for count, pipe in enumerate(pipes, 1):
  129. text += f"**Pipe:** `{count}`\n**From:** `{pipe['from_chat_id']}`\n" \
  130. + f"**To:** `{pipe['to_chat_id']}`\n**Fetcher:** `{pipe['fetcher']}`\n\n"
  131. await message.reply_text(text)