| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- """
- MIT License
- Copyright (c) 2021 TheHamkerCat
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- """
- from wbb import app, app2, SUDOERS, USERBOT_ID, BOT_ID
- from wbb.core.decorators.errors import capture_err
- from wbb.utils.filter_groups import pipes_group
- from wbb.utils.dbfunctions import is_pipe_active, activate_pipe, deactivate_pipe, show_pipes
- from pyrogram import filters
- from pyrogram.types import Message
- import asyncio
- __MODULE__ = "Pipes"
- __HELP__ = """
- **THIS MODULE IS ONLY FOR DEVS**
- Use this module to create a pipe that will forward messages of one chat/channel to another.
- /activate_pipe [FROM_CHAT_ID] [TO_CHAT_ID] [BOT|USERBOT]
- Active a pipe.
- choose 'BOT' or 'USERBOT' according to your needs, this will decide
- which client will fetch the message from 'FROM_CHAT'.
- /deactivate_pipe [FROM_CHAT_ID] [TO_CHAT_ID]
- Deactivete a pipe.
- /show_pipes
- Show all the active pipes.
- """
- pipes_list_bot = []
- pipes_list_userbot = []
- async def load_pipes():
- global pipes_list_bot, pipes_list_userbot
- pipes_list_bot = []
- pipes_list_userbot = []
- pipes = await show_pipes()
- for pipe in pipes:
- if pipe['fetcher'] == "bot":
- pipes_list_bot.append(pipe)
- continue
- pipes_list_userbot.append(pipe)
- loop = asyncio.get_running_loop()
- loop.create_task(load_pipes())
- @app.on_message(~filters.me, group=pipes_group)
- @capture_err
- async def pipes_worker_bot(_, message: Message):
- for pipe in pipes_list_bot:
- if pipe['from_chat_id'] == message.chat.id:
- await message.forward(pipe['to_chat_id'])
- @app2.on_message(~filters.me, group=pipes_group)
- @capture_err
- async def pipes_worker_userbot(_, message: Message):
- for pipe in pipes_list_userbot:
- if pipe['from_chat_id'] == message.chat.id:
- if not message.text:
- m, temp = await asyncio.gather(
- app.listen(USERBOT_ID),
- message.copy(BOT_ID)
- )
- caption = f"Forwarded from `{pipe['from_chat_id']}`"
- caption = f"{temp.caption}\n\n{caption}" if temp.caption else caption
- await app.copy_message(
- pipe['to_chat_id'],
- USERBOT_ID,
- m.message_id,
- caption=caption
- )
- await asyncio.sleep(10)
- await temp.delete()
- return
- caption = f"Forwarded from `{pipe['from_chat_id']}`"
- await app.send_message(
- pipe['to_chat_id'],
- text=message.text + "\n\n" + caption
- )
- @app.on_message(filters.command("activate_pipe") & filters.user(SUDOERS))
- @capture_err
- async def activate_pipe_func(_, message: Message):
- if len(message.command) != 4:
- await message.reply_text("**Usage:**\n/activate_pipe [FROM_CHAT_ID] [TO_CHAT_ID] [BOT|USERBOT]")
- return
- text = message.text.strip().split()
- from_chat = int(text[1])
- to_chat = int(text[2])
- fetcher = text[3].lower()
- if fetcher != "bot" and fetcher != "userbot":
- await message.reply_text("Wrong fetcher, see help menu.")
- return
- if await is_pipe_active(from_chat, to_chat):
- await message.reply_text("This pipe is already active.")
- return
- await activate_pipe(from_chat, to_chat, fetcher)
- await load_pipes()
- await message.reply_text("Activated pipe.")
- @app.on_message(filters.command("deactivate_pipe") & filters.user(SUDOERS))
- @capture_err
- async def deactivate_pipe_func(_, message: Message):
- if len(message.command) != 3:
- await message.reply_text("**Usage:**\n/deactivate_pipe [FROM_CHAT_ID] [TO_CHAT_ID]")
- return
- text = message.text.strip().split()
- from_chat = int(text[1])
- to_chat = int(text[2])
- if not await is_pipe_active(from_chat, to_chat):
- await message.reply_text("This pipe is already inactive.")
- return
- await deactivate_pipe(from_chat, to_chat)
- await load_pipes()
- await message.reply_text("Deactivated pipe.")
- @app.on_message(filters.command("pipes") & filters.user(SUDOERS))
- @capture_err
- async def show_pipes_func(_, message: Message):
- pipes = pipes_list_bot + pipes_list_userbot
- if not pipes:
- await message.reply_text("No pipe is active.")
- return
- text = ""
- for count, pipe in enumerate(pipes, 1):
- text += f"**Pipe:** `{count}`\n**From:** `{pipe['from_chat_id']}`\n" \
- + f"**To:** `{pipe['to_chat_id']}`\n**Fetcher:** `{pipe['fetcher']}`\n\n"
- await message.reply_text(text)
|