Search code examples
pythonpyrogram

My telegram userbot can forward a message in private but not in a supergroup


I am writing a small project related to userbots in telegram. I had a problem when trying to forward a message from a supergroup to another user, but when I try to forward messages from a regular chat, everything is successful. I found a similar question, but unfortunately bots are used there, not userbots.

Lib: pyrogram
Version: 1.4.8



my code:

@app.on_message(filters.group & filters.text)
async def formessage(client, message):
    text = message.text.lower()

    # other code

    await message.forward(botdata["id"])

I also tried using the function: app.forward_messages(botdata["id"], message.chat.id, message.message_id) but alas, I get the same error




The error that I get when a new message in the supergroup:

Traceback (most recent call last):
  File "C:\Users\nyansterowo\AppData\Local\Programs\Python\Python310\lib\site-packages\pyrogram\methods\advanced\resolve_peer.py", line 121, in resolve_peer
    return await self.storage.get_peer_by_id(peer_id)
  File "C:\Users\nyansterowo\AppData\Local\Programs\Python\Python310\lib\site-packages\pyrogram\storage\sqlite_storage.py", line 147, in get_peer_by_id
    raise KeyError(f"ID not found: {peer_id}")
KeyError: 'ID not found: 5279709634'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\nyansterowo\AppData\Local\Programs\Python\Python310\lib\site-packages\pyrogram\dispatcher.py", line 222, in handler_worker
    await handler.callback(self.client, *args)
  File "C:\Users\nyansterowo\Desktop\tg\src\UserBot.py", line 43, in formessage
    await message.forward(botdata["id"])
  File "C:\Users\nyansterowo\AppData\Local\Programs\Python\Python310\lib\site-packages\pyrogram\types\messages_and_media\message.py", line 2924, in forward
    return await self._client.forward_messages(
  File "C:\Users\nyansterowo\AppData\Local\Programs\Python\Python310\lib\site-packages\pyrogram\methods\messages\forward_messages.py", line 83, in forward_messages
    to_peer=await self.resolve_peer(chat_id),
  File "C:\Users\nyansterowo\AppData\Local\Programs\Python\Python310\lib\site-packages\pyrogram\methods\advanced\resolve_peer.py", line 123, in resolve_peer
    raise PeerIdInvalid
pyrogram.errors.exceptions.bad_request_400.PeerIdInvalid: Telegram says: [400 PEER_ID_INVALID] - The peer id being used is invalid or not known yet. Make sure you meet the peer before interacting with it



Let me remind you once again that I am writing a selfie and not a bot. I can easily forward a message from the group to someone through the telegram client itself, but I can’t do it through the code.


Solution

  • Firstly, the error you get is clear. The Session of your userbot has not yet met the group and cannot identify it based on its ID. You will have to receive any kind of update from that entity first.
    See this FAQ entry: https://docs.pyrogram.org/faq/peer-id-invalid-error

    Secondly, the group you are trying to forward from may have forwards restricted in its group settings. Since you can forward via an official client, this is likely not the case.