usernein / pyromod

A monkeypatcher add-on for Pyrogram
https://pyromod.pauxis.dev/
GNU Lesser General Public License v3.0
223 stars 70 forks source link

Method ask raising [400 USERNAME_INVALID] - The username is invalid (caused by "contacts.ResolveUsername") #20

Closed bagi3mk closed 10 months ago

bagi3mk commented 1 year ago

i got this error pyrogram.errors.exceptions.bad_request_400.UsernameInvalid: Telegram says: [400 USERNAME_INVALID] - The username is invalid (caused by "contacts.ResolveUsername")

when i use this code :

Phone_Number = await client.ask(chat,f"- قم بأرسال رقم الهاتف .")

why???

full error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/pyrogram/dispatcher.py", line 240, in handler_worker
    await handler.callback(self.client, *args)
  File "/usr/local/lib/python3.9/dist-packages/pyromod/listen/listen.py", line 293, in resolve_future
    await self.registered_handler(client, query, *args)
  File "/root/Tmoel/plugins/Main.py", line 554, in Call_Start
    Phone_Number = await client.ask(int(chat),f"- قم بأرسال رقم الهاتف .")
  File "/usr/local/lib/python3.9/dist-packages/pyromod/listen/listen.py", line 99, in ask
    request = await self.send_message(identifier[0], text, *args, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/pyrogram/methods/messages/send_message.py", line 128, in send_message
    peer=await self.resolve_peer(chat_id),
  File "/usr/local/lib/python3.9/dist-packages/pyrogram/methods/advanced/resolve_peer.py", line 76, in resolve_peer
    await self.invoke(
  File "/usr/local/lib/python3.9/dist-packages/pyrogram/methods/advanced/invoke.py", line 79, in invoke
    r = await self.session.invoke(
  File "/usr/local/lib/python3.9/dist-packages/pyrogram/session/session.py", line 389, in invoke
    return await self.send(query, timeout=timeout)
  File "/usr/local/lib/python3.9/dist-packages/pyrogram/session/session.py", line 357, in send
    RPCError.raise_it(result, type(data))
  File "/usr/local/lib/python3.9/dist-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")
usernein commented 1 year ago

@bagi3mk What is in the variable chat?

bagi3mk commented 1 year ago

@bagi3mk What is in the variable chat?

it's like any chat id : 683470553

CallMyNameStar commented 11 months ago

anyone found a way?

usernein commented 10 months ago

This is because client.ask is being used with the wrong arguments, probably after migrating between major releases without fixing the breaking changes.

I suppose you're using pyrogram v2 (since the property registered_handler is named user_callback on v1 and original_callback on v3).

On v1, your code is correct.

Phone_Number = await client.ask(chat,f"- قم بأرسال رقم الهاتف .")

On v2, as stated on the release notes, pyromod started receiving tuple identifiers instead of the chat_id. The correct code for v2 would be:

Phone_Number = await client.ask(f"- قم بأرسال رقم الهاتف .", (chat, None, None))

Note that the argument text comes first now.

On v3 though, it's easier:

Phone_Number = await client.ask(chat,f"- قم بأرسال رقم الهاتف .")

Just like pyromod v1, with no tuple identifiers and with the chat_id as first positional argument. Read the release notes for v3 here.

I suggest you to read the release notes before migrating between major releases. They always contains breaking changes (otherwise they would not be major).