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

pyrogram.Client.get_chat() method results in 400th HTTP-code response if chat_id argument is set as a link, and not directly as a username #1370

Open
2 of 3 tasks
progdad opened this issue Dec 3, 2023 · 0 comments

Comments

@progdad
Copy link

progdad commented Dec 3, 2023

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

I've tried several times with different groups and users to obtain information about a chat using pyrogram.Client.get_chat() method, and simply passing a username to the method's chat_id argument works, whereas passing the username as a link instead raises the error.

I've researched the issue, and it seems that another person had run into the same issue, which remains unresolved > the issue.

The doc string of the get_chat() says that a link is a valid input format:

...
        Parameters:
            chat_id (``int`` | ``str``):
                Unique identifier (int) or username (str) of the target chat.
                Unique identifier for the target chat in form of a *t.me/joinchat/* link, identifier (int) or username
                of the target channel/supergroup (in the format @username)
...

Steps to reproduce

Use a valid telegram username:

  1. Pass "<username>" to the chat_id argument, and try to run the coroutine, check the output.
  2. Repeat the first step, but instead of "<username>" try passing "https://t.me/<username>" or "t.me/<username>"

Code example

################
peer = await client.get_chat('pyrogramchat')
print("Chat ID:", peer.id)

"""
The Output:
Chat ID: -1001387666944
"""
################

# Try this instead:
################
peer = await client.get_chat('t.me/pyrogramchat')  # The output is in the 'Logs' section.
################

Logs

Telegram says: [400 USERNAME_INVALID] - The username is invalid (caused by "contacts.ResolveUsername")
Traceback (most recent call last):
  File "/home/eliot/Documents/development/userBots/vovaTroll/venv/lib/python3.8/site-packages/pyrogram/methods/advanced/resolve_peer.py", line 62, in resolve_peer
    return await self.storage.get_peer_by_id(peer_id)
  File "/home/eliot/Documents/development/userBots/vovaTroll/venv/lib/python3.8/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: t.me/pyrogramchat'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/eliot/Documents/development/userBots/vovaTroll/venv/lib/python3.8/site-packages/pyrogram/methods/advanced/resolve_peer.py", line 71, in resolve_peer
    int(peer_id)
ValueError: invalid literal for int() with base 10: 't.me/pyrogramchat'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/eliot/Documents/development/userBots/vovaTroll/venv/lib/python3.8/site-packages/pyrogram/methods/advanced/resolve_peer.py", line 74, in resolve_peer
    return await self.storage.get_peer_by_username(peer_id)
  File "/home/eliot/Documents/development/userBots/vovaTroll/venv/lib/python3.8/site-packages/pyrogram/storage/sqlite_storage.py", line 154, in get_peer_by_username
    raise KeyError(f"Username not found: {username}")
KeyError: 'Username not found: t.me/pyrogramchat'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/eliot/Documents/development/userBots/vovaTroll/venv/lib/python3.8/site-packages/pyrogram/dispatcher.py", line 240, in handler_worker
    await handler.callback(self.client, *args)
  File "vova.py", line 17, in hello
    peer = await client.get_chat('t.me/pyrogramchat')
  File "/home/eliot/Documents/development/userBots/vovaTroll/venv/lib/python3.8/site-packages/pyrogram/methods/chats/get_chat.py", line 78, in get_chat
    peer = await self.resolve_peer(chat_id)
  File "/home/eliot/Documents/development/userBots/vovaTroll/venv/lib/python3.8/site-packages/pyrogram/methods/advanced/resolve_peer.py", line 76, in resolve_peer
    await self.invoke(
  File "/home/eliot/Documents/development/userBots/vovaTroll/venv/lib/python3.8/site-packages/pyrogram/methods/advanced/invoke.py", line 79, in invoke
    r = await self.session.invoke(
  File "/home/eliot/Documents/development/userBots/vovaTroll/venv/lib/python3.8/site-packages/pyrogram/session/session.py", line 389, in invoke
    return await self.send(query, timeout=timeout)
  File "/home/eliot/Documents/development/userBots/vovaTroll/venv/lib/python3.8/site-packages/pyrogram/session/session.py", line 357, in send
    RPCError.raise_it(result, type(data))
  File "/home/eliot/Documents/development/userBots/vovaTroll/venv/lib/python3.8/site-packages/pyrogram/errors/rpc_error.py", line 91, in raise_it
    raise getattr(
pyrogram.errors.exceptions.bad_request_400.UsernameInvalid: Telegram says: [400 USERNAME_INVALID] - The username is invalid (caused by "contacts.ResolveUsername")
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

1 participant