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

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

Open
3 tasks done
gameuser1982 opened this issue Apr 14, 2024 · 2 comments
Open
3 tasks done

Comments

@gameuser1982
Copy link

Checklist

  • I am sure the error is coming from Pyrogram's code and not elsewhere
  • I have searched in the issue tracker for similar bug reports, including closed ones
  • I ran pip3 install -U https://github.com/pyrogram/pyrogram/archive/master.zip and reproduced the issue using the latest development version

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
Copy link

Dante4 commented Apr 16, 2024

Be sure that both side added to contact each other

@efinskiy
Copy link

efinskiy commented May 1, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants