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

Not forwarding messages from deafult topic #98

Closed
corpsepapi opened this issue Feb 26, 2024 · 26 comments
Closed

Not forwarding messages from deafult topic #98

corpsepapi opened this issue Feb 26, 2024 · 26 comments

Comments

@corpsepapi
Copy link

corpsepapi commented Feb 26, 2024

It's working fine with all the topics except the default one and when I'm only adding only the group id it's forwarding all topics in a single group topic

@khoben khoben closed this as completed in bc3cb80 Feb 27, 2024
@khoben
Copy link
Owner

khoben commented Feb 27, 2024

Please try now -1001#1 for the default topic

khoben added a commit that referenced this issue Feb 27, 2024
@corpsepapi
Copy link
Author

Hey, thank you for resolving this issue I wanted to ask u one more thing while forwarding messages from groups is it possible to forward the original message sender's username too?

@khoben
Copy link
Owner

khoben commented Feb 29, 2024

Hey, thank you for resolving this issue I wanted to ask u one more thing while forwarding messages from groups is it possible to forward the original message sender's username too?

There are two options here: don't copy, but forward (mode option), or use ForwardFormatFilter (set filter in yaml configuration).

@corpsepapi
Copy link
Author

i tried forward mode but the replies don't work in it, is it possible to get usernames in a specific channel using copy mode?

@khoben
Copy link
Owner

khoben commented Mar 2, 2024

i tried forward mode but the replies don't work in it, is it possible to get usernames in a specific channel using copy mode?

If you need to forward from a chat, you will need to create a filter like https://github.com/khoben/telemirror/blob/master/telemirror/messagefilters/messagefilters.py#L213, but instead of channel_name (https://github.com/khoben/telemirror/blob/master/telemirror/messagefilters/messagefilters.py#L253) get the user name from the message object.

Getting username from message:

message_sender = await message.get_sender()
message_sender_name = utils.get_display_name(message_sender) # or message_sender.username

@khoben
Copy link
Owner

khoben commented Mar 4, 2024

Check updates to ForwardFormatFilter

Sample configuration:

directions:
  - from: [-1001#3]
    to: [-1002#1]
    filters:
      - ForwardFormatFilter:
          format: "{sender_title} {sender_username} says:\n\n{message_text}"

@corpsepapi
Copy link
Author

working, thank you so much <3<3

@corpsepapi
Copy link
Author

corpsepapi commented Mar 7, 2024

Hey khoen, sorry to diusturb you again. I'm using this restricted channel filter

`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`
    
    to forward messages from restricted channels or groups but when someone in restricted group topics sends an image its sends this error
    
   `ERROR 2024-03-07 00:43:03,782 [mirroring.py:175]:telemirror: Error while sending message to chat#-1002068277802. ChatForwardsRestrictedError: You can't forward messages from a protected chat (caused by SendMediaRequest)`

texts are being forwarded but no the attachments in group topics

@khoben
Copy link
Owner

khoben commented Mar 7, 2024

Try logging the line where the media should be downloaded and replaced. More likely "if" gives False in your case.

@corpsepapi
Copy link
Author

still couldn't figure it out, could u help me with what I'm doing wrong?

@khoben
Copy link
Owner

khoben commented Mar 20, 2024

Can you show your config and the changes you made?

@corpsepapi
Copy link
Author

I added the restricted filter in messagefilter.py and

class MessageFilter(Protocol):
@Property
def restricted_content_allowed(self) -> bool:
"""Indicates that restricted content is allowed or not to process"""
return True
and changes this to true

@khoben
Copy link
Owner

khoben commented Mar 22, 2024

Just checked, everything works. To begin with, I want to say that forwarding from channels where this is prohibited violates the Telegram Terms of Service.
But for testing purposes you should change the _process_message method to sample implementation from docstring and then use the RestrictSavingContentBypassFilter filter in the config.
To make the filter importable, add to telemirror/messagefilters/__init__.py:

from .restrictsavingfilter import RestrictSavingContentBypassFilter  # noqa: F401

@corpsepapi
Copy link
Author

Thank you for the help it worked but, its sending images as file
image

@khoben
Copy link
Owner

khoben commented Mar 31, 2024

Thank you for the help it worked but, its sending images as file
image

You can try to wrap downloaded bytes with:

message.media = types.InputMediaUploadedPhoto(file=cloned_photo)

@corpsepapi
Copy link
Author

Now I'm getting this error: PhotoExtInvalidError: The extension of the photo is invalid (caused by SendMediaRequest)

@khoben
Copy link
Owner

khoben commented Mar 31, 2024

Haven't tested it, but maybe taking the file name from the source message could help:

cloned_photo: types.TypeInputFile = await client.upload_file(photo, file_name=message.file.name)
message.media = types.InputMediaUploadedPhoto(file=cloned_photo)

@corpsepapi
Copy link
Author

Still getting the same error

@khoben
Copy link
Owner

khoben commented Apr 1, 2024

In what environment is the program running? OS/docker, python version and through which telegram client are messages with images sent?

@khoben
Copy link
Owner

khoben commented Apr 1, 2024

It would also be great if you sent the images themselves as files here

@corpsepapi
Copy link
Author

In what environment is the program running? OS/docker, python version and through which telegram client are messages with images sent?
Windows OS
Python Version: 3.12.2

@khoben
Copy link
Owner

khoben commented Apr 2, 2024

Really idk why images are sent like random files or with error. Couldn't get the same behavior. Try running via docker or check that all dependencies are updated: pip install -r requirements.txt

@Amiinnetdz
Copy link

Hi did you fix that ?

@khoben
Copy link
Owner

khoben commented May 26, 2024

@corpsepapi You may try to specify the name for cloned_photo if it is still relevant for you #68 (comment)

@Amiinnetdz
Copy link

I get this error after few time
Error handling request
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/aiohttp/web_protocol.py", line 3
50, in data_received
messages, upgraded, tail = self._request_parser.feed_data(data)
File "aiohttp/_http_parser.pyx", line 557, in aiohttp._http_parser.HttpParser.
feed_data
aiohttp.http_exceptions.BadStatusLine: 400, message:
Invalid method encountered:
b'\x16\x03\x01'
^

@khoben
Copy link
Owner

khoben commented Sep 12, 2024

I get this error after few time
Error handling request
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/aiohttp/web_protocol.py", line 3
50, in data_received
messages, upgraded, tail = self._request_parser.feed_data(data)
File "aiohttp/_http_parser.pyx", line 557, in aiohttp._http_parser.HttpParser.
feed_data
aiohttp.http_exceptions.BadStatusLine: 400, message:
Invalid method encountered:
b'\x16\x03\x01'
^

aiohttp is used here for the heartbeat endpoint, someone from outside is sending invalid packets.
This should not affect the forwarding process in any way.

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