pipes.py 4.8 KB

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