LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
10.08k stars 1.41k forks source link

Cannot forward message if I don't interact with group first #4411

Closed Faycop closed 4 months ago

Faycop commented 4 months ago

Code that causes the issue

message = await client.get_messages(spam_lists[list_name]['chat_id'], ids=message_id)
                for i in range(0, len(groups), bulk_size):
                    bulk = groups[i:i + bulk_size]
                    for group in bulk:
                        retries = 0
                        while retries < 3:
                            try:
                                await fetch_chat_info(client, group)  # Manual polling
                                await client.get_entity(PeerChannel(group))

                                await client.forward_messages(group, message)
                                spam_lists[list_name]['last_sent'][group] = datetime.now(italy_tz).isoformat()
                                break
                            except ValueError:
                                logging.warning(f"Group {group} not accessible. Skipping.")
                                break
                            except errors.RPCError as e:
                                logging.error(f"Error sending message to group {group}: {e}")
                                retries += 1
                                await asyncio.sleep(2)
                        if retries == 3:
                            logging.error(f"Failed to send message to group {group} after 3 retries.")
                    await asyncio.sleep(delay / 1000)

Expected behavior

I am expecting to see the message forwarded.

Actual behavior

In the logs I get the "Invalid object ID for a chat" error because it didn't found the identity for the gruop via chat_id. If I'm forwarding a message in the group manually then the code is executed perfectly.

Traceback

2024-07-18T17:29:29.664509+00:00 app[worker.1]: ERROR:root:Error sending message to group -1229581945: Invalid object ID for a chat. Make sure to pass the right types, for instance making sure that the request is designed for chats (not channels/megagroups) or otherwise look for a different one more suited\nAn example working with a megagroup and AddChatUserRequest, it will fail because megagroups are channels. Use InviteToChannelRequest instead (caused by ForwardMessagesRequest)
2024-07-18T17:29:31.746386+00:00 app[worker.1]: ERROR:root:Error sending message to group -1229581945: Invalid object ID for a chat. Make sure to pass the right types, for instance making sure that the request is designed for chats (not channels/megagroups) or otherwise look for a different one more suited\nAn example working with a megagroup and AddChatUserRequest, it will fail because megagroups are channels. Use InviteToChannelRequest instead (caused by ForwardMessagesRequest)
2024-07-18T17:29:33.833484+00:00 app[worker.1]: ERROR:root:Error sending message to group -1229581945: Invalid object ID for a chat. Make sure to pass the right types, for instance making sure that the request is designed for chats (not channels/megagroups) or otherwise look for a different one more suited\nAn example working with a megagroup and AddChatUserRequest, it will fail because megagroups are channels. Use InviteToChannelRequest instead (caused by ForwardMessagesRequest)
2024-07-18T17:29:35.836516+00:00 app[worker.1]: ERROR:root:Failed to send message to group -1229581945 after 3 retries.
2024-07-18T17:29:36.275161+00:00 app[worker.1]: ERROR:root:Error sending message to group -1177688883: Invalid object ID for a chat. Make sure to pass the right types, for instance making sure that the request is designed for chats (not channels/megagroups) or otherwise look for a different one more suited\nAn example working with a megagroup and AddChatUserRequest, it will fail because megagroups are channels. Use InviteToChannelRequest instead (caused by ForwardMessagesRequest)
2024-07-18T17:29:38.356095+00:00 app[worker.1]: ERROR:root:Error sending message to group -1177688883: Invalid object ID for a chat. Make sure to pass the right types, for instance making sure that the request is designed for chats (not channels/megagroups) or otherwise look for a different one more suited\nAn example working with a megagroup and AddChatUserRequest, it will fail because megagroups are channels. Use InviteToChannelRequest instead (caused by ForwardMessagesRequest)
2024-07-18T17:29:40.611554+00:00 app[worker.1]: ERROR:root:Error sending message to group -1177688883: Invalid object ID for a chat. Make sure to pass the right types, for instance making sure that the request is designed for chats (not channels/megagroups) or otherwise look for a different one more suited\nAn example working with a megagroup and AddChatUserRequest, it will fail because megagroups are channels. Use InviteToChannelRequest instead (caused by ForwardMessagesRequest)
2024-07-18T17:29:42.612171+00:00 app[worker.1]: ERROR:root:Failed to send message to group -1177688883 after 3 retries.

Telethon version

1.36

Python version

3.12.4

Operating system (including distribution name and version)

Linux (Hosted on heroku)

Other details

I am developing a userbot that among the other things spams its advertise in over 100 groups every 4-6 hour so I cannot have the possibility to interact manually with each group...

I also saw this post (#4345) but I don't know how I can implement it in my code, I'm kinda new on Python and Telethon, sorry in advance.

Checklist

Lonami commented 4 months ago

Spamming is against the ToS, and the library cannot interact in any other way with just IDs.