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.35k stars 1.4k forks source link

username invalid error, even on a valid username #1231

Closed SpEcHiDe closed 1 year ago

SpEcHiDe commented 1 year ago

Checklist

Description

join_chat is not working as expected in the new type of supergroups.

Steps to reproduce

  1. Create a new supergroup

  2. Enable Join Requests in the group

  3. Try to use client.join_chat method with the t.me/pyrogram (link of the chat) ==> this shows the below logs

  4. Try to use the client.join_chat method with the @pyrogram (username of the chat) ==> workd as expected with the appropriate exception raise getattr( pyrogram.errors.exceptions.bad_request_400.InviteRequestSent: Telegram says: [400 INVITE_REQUEST_SENT] - The request to join this chat or channel has been successfully sent (caused by "channels.JoinChannel")

Code example

from pyrogram import Client
app = Client("bot")
app.start()
# app.join_chat("https://t.me/pyrogram")
app.join_chat("@pyrogram")
app.stop()

Logs

Traceback (most recent call last):
  File "/venv/lib/python3.9/site-packages/pyrogram/methods/advanced/resolve_peer.py", line 62, in resolve_peer
    return await self.storage.get_peer_by_id(peer_id)
  File "/venv/lib/python3.9/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: https://t.me/pyrogram'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/venv/lib/python3.9/site-packages/pyrogram/methods/advanced/resolve_peer.py", line 71, in resolve_peer
    int(peer_id)
ValueError: invalid literal for int() with base 10: 'https://t.me/pyrogram'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/venv/lib/python3.9/site-packages/pyrogram/methods/advanced/resolve_peer.py", line 74, in resolve_peer
    return await self.storage.get_peer_by_username(peer_id)
  File "/venv/lib/python3.9/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: https://t.me/pyrogram'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/bot/bp/eval_exec_ecp.py", line 275, in _run_coro
    ret = loop.run_until_complete(task)
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "", line 4, in __aexec
  File "/venv/lib/python3.9/site-packages/pyrogram/sync.py", line 74, in coro_wrapper
    return await asyncio.wrap_future(asyncio.run_coroutine_threadsafe(coroutine, main_loop))
  File "/venv/lib/python3.9/site-packages/pyrogram/methods/chats/join_chat.py", line 70, in join_chat
    channel=await self.resolve_peer(chat_id)
  File "/venv/lib/python3.9/site-packages/pyrogram/methods/advanced/resolve_peer.py", line 76, in resolve_peer
    await self.invoke(
  File "/venv/lib/python3.9/site-packages/pyrogram/methods/advanced/invoke.py", line 79, in invoke
    r = await self.session.invoke(
  File "/venv/lib/python3.9/site-packages/pyrogram/session/session.py", line 389, in invoke
    return await self.send(query, timeout=timeout)
  File "/venv/lib/python3.9/site-packages/pyrogram/session/session.py", line 357, in send
    RPCError.raise_it(result, type(data))
  File "/venv/lib/python3.9/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")
delivrance commented 1 year ago

Thanks for reporting the issue, this should now be fixed.

snimshchikov commented 1 year ago

Issue is resolved only for main usernames, ton usernames still give this error. Example username: t.me/wechat