pyrogram / pyrogram

Elegant, modern and asynchronous Telegram MTProto API framework in Python for users and bots
https://pyrogram.org
GNU Lesser General Public License v3.0
4.39k stars 1.42k forks source link

[400 PEER_ID_INVALID] but I met this peer in a group chat! On real client I can DM them. #1417

Open gameuser1982 opened 6 months ago

gameuser1982 commented 6 months ago

Checklist

Description

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

Steps to reproduce

  1. I setup two telegram accounts. Neither are on each other's contact list
  2. I added both telegram accounts into a group chat. They both typed in the group chat and both received the message. They should have satisfied the requirement of "meeting each other"
  3. Despite this I keep getting a "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". when telegram account "Peter" tries to send a DM to "Sean" via API. Neither account has a username set but both are on a verified unique phone number.
  4. Although they cannot message each other in DM using the API, I know that on the official client it's completely possible to DM someone you met in a group chat. Why can't I do it via the Telegram API? I should have met this peer in the group chat.
  5. I am DMing them using the API through their chat ID.

Code example

from pyrogram import Client
from dotenv import load_dotenv
import os

load_dotenv()

CONFIG = {
    "telegram_api_id": int(os.getenv("TG_API_ID")),
    "telegram_hash": os.getenv("TG_API_HASH"),
}

app = Client("my_account",CONFIG["telegram_api_id"],CONFIG["telegram_hash"])

with app:
    app.send_message(7141057061, "Hello there. I am using Pyrogram.")

Logs

Traceback (most recent call last):
  File "C:\pytelegrambot\pytelegrambot\Lib\site-packages\pyrogram\methods\advanced\resolve_peer.py", line 62, in resolve_peer
    return await self.storage.get_peer_by_id(peer_id)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\pytelegrambot\pytelegrambot\Lib\site-packages\pyrogram\storage\sqlite_storage.py", line 142, in get_peer_by_id
    raise KeyError(f"ID not found: {peer_id}")
KeyError: 'ID not found: 7141057061'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\pytelegrambot\pytelegrambot\Lib\site-packages\pyrogram\methods\advanced\resolve_peer.py", line 123, in resolve_peer
    return await self.storage.get_peer_by_id(peer_id)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\pytelegrambot\pytelegrambot\Lib\site-packages\pyrogram\storage\sqlite_storage.py", line 142, in get_peer_by_id
    raise KeyError(f"ID not found: {peer_id}")
KeyError: 'ID not found: 7141057061'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\pytelegrambot\dm.py", line 16, in <module>
    app.send_message(7141057061, "Hello there. I am using Pyrogram.")
  File "C:\pytelegrambot\pytelegrambot\Lib\site-packages\pyrogram\sync.py", line 66, in async_to_sync_wrap
    return loop.run_until_complete(coroutine)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python312\Lib\asyncio\base_events.py", line 684, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\pytelegrambot\pytelegrambot\Lib\site-packages\pyrogram\methods\messages\send_message.py", line 128, in send_message
    peer=await self.resolve_peer(chat_id),
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\pytelegrambot\pytelegrambot\Lib\site-packages\pyrogram\methods\advanced\resolve_peer.py", line 125, 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
Dante4 commented 6 months ago

Be sure that both side added to contact each other

efinskiy commented 5 months ago

@gameuser1982 Monkeypatch pyrogram.utils.get_peer_type

import pyrogram.utils as utils

def get_peer_type(peer_id: int) -> str:
    print('get_peer_type call')
    peer_id_str = str(peer_id)
    if not peer_id_str.startswith("-"):
        return "user"
    elif peer_id_str.startswith("-100"):
        return "channel"
    else:
        return "chat"

utils.get_peer_type = get_peer_type

thx @AndrewFucher for solution.