Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restricted filter not working with groups or topics #123

Closed
bratcorpse opened this issue Aug 22, 2024 · 6 comments
Closed

Restricted filter not working with groups or topics #123

bratcorpse opened this issue Aug 22, 2024 · 6 comments

Comments

@bratcorpse
Copy link

The restricted filter is working fine with channels but it isn't working with groups or topics

@khoben
Copy link
Owner

khoben commented Aug 23, 2024

Hi, @bratcorpse
Can you provide logs and restricted filter code?

@bratcorpse
Copy link
Author

bratcorpse commented Aug 27, 2024

sorry for the late response, it's forwarding text but not attachments

INFO 2024-08-27 06:08:36,843 [mirroring.py:71]:telemirror: [New message]: https://t.me/c/2182848314/7
ERROR 2024-08-27 06:08:37,117 [mirroring.py:165]:telemirror: Error while sending message to chat#-1001596234020. ChatForwardsRestrictedError: You can't forward messages from a protected chat (caused by SendMediaRequest)
INFO 2024-08-27 06:08:48,786 [mirroring.py:71]:telemirror: [New message]: https://t.me/c/2182848314/8
INFO 2024-08-27 06:10:40,369 [mirroring.py:71]:telemirror: [New message]: https://t.me/c/2182848314/9
ERROR 2024-08-27 06:10:40,474 [mirroring.py:165]:telemirror: Error while sending message to chat#-1001596234020. ChatForwardsRestrictedError: You can't forward messages from a protected chat (caused by SendMediaRequest)

`from typing import Tuple, Type
from telethon import types

from ..hints import EventMessage, EventLike
from .base import MessageFilter

class RestrictSavingContentBypassFilter(MessageFilter):
def init(self, send_restricted_content=True):
self.send_restricted_content = send_restricted_content

async def _process_message(
    self, message: EventMessage, event_type: Type[EventLike]
) -> Tuple[bool, EventMessage]:
    if message.chat.noforwards and message.media:
        # Handle images
        if isinstance(message.media, types.MessageMediaPhoto):
            client: TelegramClient = message.client
            photo: bytes = await client.download_media(message=message, file=bytes)
            cloned_photo: types.TypeInputFile = await client.upload_file(photo)
            message.media = cloned_photo
        # Others media types set to None (remove from original message)...
        else:
            message.media = None

    return True, message`

this is the restrictedfilter.py which i'm using

@khoben
Copy link
Owner

khoben commented Aug 28, 2024

sorry for the late response, it's forwarding text but not attachments

INFO 2024-08-27 06:08:36,843 [mirroring.py:71]:telemirror: [New message]: https://t.me/c/2182848314/7
ERROR 2024-08-27 06:08:37,117 [mirroring.py:165]:telemirror: Error while sending message to chat#-1001596234020. ChatForwardsRestrictedError: You can't forward messages from a protected chat (caused by SendMediaRequest)
INFO 2024-08-27 06:08:48,786 [mirroring.py:71]:telemirror: [New message]: https://t.me/c/2182848314/8
INFO 2024-08-27 06:10:40,369 [mirroring.py:71]:telemirror: [New message]: https://t.me/c/2182848314/9
ERROR 2024-08-27 06:10:40,474 [mirroring.py:165]:telemirror: Error while sending message to chat#-1001596234020. ChatForwardsRestrictedError: You can't forward messages from a protected chat (caused by SendMediaRequest)

`from typing import Tuple, Type
from telethon import types

from ..hints import EventMessage, EventLike
from .base import MessageFilter

class RestrictSavingContentBypassFilter(MessageFilter):
def init(self, send_restricted_content=True):
self.send_restricted_content = send_restricted_content

async def _process_message(
    self, message: EventMessage, event_type: Type[EventLike]
) -> Tuple[bool, EventMessage]:
    if message.chat.noforwards and message.media:
        # Handle images
        if isinstance(message.media, types.MessageMediaPhoto):
            client: TelegramClient = message.client
            photo: bytes = await client.download_media(message=message, file=bytes)
            cloned_photo: types.TypeInputFile = await client.upload_file(photo)
            message.media = cloned_photo
        # Others media types set to None (remove from original message)...
        else:
            message.media = None

    return True, message`

this is the restrictedfilter.py which i'm using

Check that filter was added to mirror.config.yml. Here is sample working implementation for such filter - #68 (comment)

@bratcorpse
Copy link
Author

Thank you, photos are working not but it's not sending videos

@khoben
Copy link
Owner

khoben commented Aug 29, 2024

Thank you, photos are working not but it's not sending videos

Yep, you can handle other types:

...
elif isinstance(message.media, types.MessageMediaDocument):
    client: TelegramClient = message.client
  
    downloaded_media: bytes = await client.download_media(
        message=message, file=bytes
    )
    cloned_media_file: types.TypeInputFile = await client.upload_file(
        downloaded_media
    )
  
    cloned_media = types.InputMediaUploadedDocument(
        file=cloned_media_file,
        mime_type=message.media.document.mime_type,
        attributes=message.media.document.attributes,
        spoiler=message.media.spoiler,
        ttl_seconds=message.media.ttl_seconds,
    )
  
    message.media = cloned_media
else:
...

I think it's also a good idea to check the maximum size for the file, RAM might not be enough for big files:

if message.file and message.file.size > self._max_file_size_bytes:
    # File too big
    return False, message

Or even re-upload from the filesystem.

@bratcorpse
Copy link
Author

Thank you for the help, really appreciate it <3!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants