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

[HELP] Skip restricted content #68

Closed
lucag74 opened this issue Feb 4, 2023 · 14 comments
Closed

[HELP] Skip restricted content #68

lucag74 opened this issue Feb 4, 2023 · 14 comments

Comments

@lucag74
Copy link

lucag74 commented Feb 4, 2023

HI,
thank's for your work,
I enable the forward of restrict channel.
i uncommented this

Skip 'restricted saving content' enabled
on event on_new_message

there is a way for this channel mirror only the text , maybe the image (saving to array) ?
(i see one old post)

I can skip all the other element , audio, album etc.. ?
In few word i'm try to avoid the ban, and i'm interested on only text part and maybe images of every message.

Thank's

@khoben
Copy link
Owner

khoben commented Feb 4, 2023

@lucag74 in general, you need to download, upload and change the media object.
Pseudo-code:

# If here is media and noforwards enabled
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

@lucag74
Copy link
Author

lucag74 commented Feb 4, 2023

Fantastic i will try.

Your project it's fantastic and i'm using it and the same time i will level up my python and telegram skill :)

@khoben khoben closed this as completed Feb 16, 2023
@khoben khoben pinned this issue Feb 23, 2023
@Amiinnetdz
Copy link

Hi, anyone fixed that ? i am stil getting "are not supported"

@khoben
Copy link
Owner

khoben commented May 25, 2024

Hi, anyone fixed that ? i am stil getting "are not supported"

Copying from protected channels isn't implemented because this violates the terms of the user agreement.
But you can implement it by yourself, check #98 (comment)

@khoben
Copy link
Owner

khoben commented May 25, 2024

Hi, anyone fixed that ? i am stil getting "are not supported"

Saw your message with error, indentation is most likely the problem there. Here is a sample filter implementation:

from typing import Tuple, Type

from telethon import TelegramClient, types

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


class RestrictSavingContentBypassFilter(MessageFilter):
    """Filter that bypasses `saving content restriction`

    Sample implementation:

    Download the media, upload it to the Telegram servers,
    and then change to the new uploaded media

    ```
    # If here is media and noforwards enabled
    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
    ```
    """

    @property
    def restricted_content_allowed(self) -> bool:
        return True

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

        # Process message if not empty
        return bool(message.media or message.message), message

@Amiinnetdz
Copy link

Amiinnetdz commented May 25, 2024

Hi, anyone fixed that ? i am stil getting "are not supported"

Saw your message with error, indentation is most likely the problem there. Here is a sample filter implementation:

from typing import Tuple, Type

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


class RestrictSavingContentBypassFilter(MessageFilter):
    """Filter that bypasses `saving content restriction`

    Sample implementation:

    Download the media, upload it to the Telegram servers,
    and then change to the new uploaded media

    ```
    # If here is media and noforwards enabled
    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
    ```
    """

    @property
    def restricted_content_allowed(self) -> bool:
        return True

    async def _process_message(
        self, message: EventMessage, event_type: Type[EventLike]
    ) -> Tuple[bool, EventMessage]:
        
        if message.media is None or (
            message.chat is None or not message.chat.noforwards
        ):
            # Forwarding allowed
            return True, message
        
        client: TelegramClient = message.client
        
        # Handle images
        if isinstance(message.media, types.MessageMediaPhoto):
            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

        # Process message if not empty
        return bool(message.media or message.message), message

Hi added this but stil getting this message ?
WARNING 2024-05-25 14:43:50,223 [mirroring.py:117]:telemirror: Forwards from channel#-1001633397411 with restricted saving content enabled to channel#-1002063015567 are not supported.

my source containt Text and image

@khoben
Copy link
Owner

khoben commented May 25, 2024

Hi, anyone fixed that ? i am stil getting "are not supported"

Saw your message with error, indentation is most likely the problem there. Here is a sample filter implementation:

from typing import Tuple, Type

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


class RestrictSavingContentBypassFilter(MessageFilter):
    """Filter that bypasses `saving content restriction`

    Sample implementation:

    Download the media, upload it to the Telegram servers,
    and then change to the new uploaded media

    ```
    # If here is media and noforwards enabled
    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
    ```
    """

    @property
    def restricted_content_allowed(self) -> bool:
        return True

    async def _process_message(
        self, message: EventMessage, event_type: Type[EventLike]
    ) -> Tuple[bool, EventMessage]:
        
        if message.media is None or (
            message.chat is None or not message.chat.noforwards
        ):
            # Forwarding allowed
            return True, message
        
        client: TelegramClient = message.client
        
        # Handle images
        if isinstance(message.media, types.MessageMediaPhoto):
            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

        # Process message if not empty
        return bool(message.media or message.message), message

Hi added this but stil getting this message ? WARNING 2024-05-25 14:43:50,223 [mirroring.py:117]:telemirror: Forwards from channel#-1001633397411 with restricted saving content enabled to channel#-1002063015567 are not supported.

my source containt Text and image

You should also add RestrictSavingContentBypassFilter to yaml filter config:

...
directions:
  - from: [-1001]
    to: [-1002]
    filters:
      - RestrictSavingContentBypassFilter

To make the filter importable, add to telemirror/messagefilters/__init__.py:

from .restrictsavingfilter import RestrictSavingContentBypassFilter  # noqa: F401

@Amiinnetdz
Copy link

message forwarded but image stil get this error ?!
ERROR 2024-05-25 15:16:12,865 [mirroring.py:54]:telemirror: name 'types' is not defined
Traceback (most recent call last):
File "/root/frd/telemirror/mirroring.py", line 52, in wrapper
return await fn(self, *args, **kw)
File "/root/frd/telemirror/mirroring.py", line 125, in new_message
proceed, filtered_message = await config.filters.process(
File "/root/frd/telemirror/messagefilters/base.py", line 29, in process
return await self._process_message(entity, event_type)
File "/root/frd/telemirror/messagefilters/restrictsavingfilter.py", line 49, in _process_message
if isinstance(message.media, types.MessageMediaPhoto):
NameError: name 'types' is not defined

@khoben
Copy link
Owner

khoben commented May 25, 2024

message forwarded but image stil get this error ?! ERROR 2024-05-25 15:16:12,865 [mirroring.py:54]:telemirror: name 'types' is not defined Traceback (most recent call last): File "/root/frd/telemirror/mirroring.py", line 52, in wrapper return await fn(self, *args, **kw) File "/root/frd/telemirror/mirroring.py", line 125, in new_message proceed, filtered_message = await config.filters.process( File "/root/frd/telemirror/messagefilters/base.py", line 29, in process return await self._process_message(entity, event_type) File "/root/frd/telemirror/messagefilters/restrictsavingfilter.py", line 49, in _process_message if isinstance(message.media, types.MessageMediaPhoto): NameError: name 'types' is not defined

Here is missing imports. See updated filter sample code #68 (comment)

@Amiinnetdz
Copy link

all working good but one last problem image send as file
Capture d’écran 2024-05-25 172530
thank you for your help

@khoben
Copy link
Owner

khoben commented May 26, 2024

all working good but one last problem image send as file Capture d’écran 2024-05-25 172530 thank you for your help

I suppose setting a filename might help:

cloned_photo.name = (
    message.file.name if message.file.name else "photo.jpg"
)

Also I've updated the example #68 (comment)

@Amiinnetdz
Copy link

it work like a charme <3 thank you bro

@Amiinnetdz
Copy link

i will try to make it easy with web interface with streamlit can you suggest anything ?

@khoben
Copy link
Owner

khoben commented May 27, 2024

i will try to make it easy with web interface with streamlit can you suggest anything ?

I think it's a good idea to make a UI, someone might find it useful. You need to somehow save and modify the chat_mapping from https://github.com/khoben/telemirror/blob/master/telemirror/mirroring.py and the filter parameters from https://github.com/khoben/telemirror/tree/master/telemirror/messagefilters . Feel free to make a PR.

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

3 participants