LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
10.01k stars 1.41k forks source link

telethon.errors.rpcerrorlist.UserIdInvalidError #1237

Closed C0deL0ver closed 5 years ago

C0deL0ver commented 5 years ago

Hi. I get this error and no matter how hard I try it won’t fade away. It appears when I select add by user id (the original code is on the bottom). How can I solve it? I managed to use it once and it worked, but now I want to use it on another account of mine, but it shows this error. I also checked with my friends' accounts and still the same error.

Traceback (most recent call last):
File “add.py”, line 79, in
client(InviteToChannelRequest(target_group_entity,[user_to_add]))
File “C:\Python37\lib\site-packages\telethon\sync.py”, line 39, in syncified
return loop.run_until_complete(coro)
File “C:\Python37\Lib\asyncio\base_events.py”, line 584, in run_until_complete

return future.result()
File “C:\Python37\lib\site-packages\telethon\client\users.py”, line 64, in __c
all__
result = await future
telethon.errors.rpcerrorlist.UserIdInvalidError: Invalid object ID for a user. M
ake sure to pass the right types, for instance making sure that the request is d
esigned for users or otherwise look for a different one more suited (caused by I
nviteToChannelRequest)
Unexpected Error

The code:

from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty, InputPeerChannel, InputPeerUser
from telethon.errors.rpcerrorlist import PeerFloodError, UserPrivacyRestrictedError
from telethon.tl.functions.channels import InviteToChannelRequest
import sys
import csv
import traceback
import time

blah blah blah

input_file = sys.argv[1]
users = []
with open(input_file, encoding='UTF-8') as f:
    rows = csv.reader(f,delimiter=",",lineterminator="\n")
    next(rows, None)
    for row in rows:
        user = {}
        user['username'] = row[0]
        user['id'] = int(row[1])
        user['access_hash'] = int(row[2])
        user['name'] = row[3]
        users.append(user)

chats = []
last_date = None
chunk_size = 200
groups=[]

result = client(GetDialogsRequest(
             offset_date=last_date,
             offset_id=0,
             offset_peer=InputPeerEmpty(),
             limit=chunk_size,
             hash = 0
         ))
chats.extend(result.chats)

for chat in chats:
    try:
        if chat.megagroup== True:
            groups.append(chat)
    except:
        continue

print('Choose a group to add members:')
i=0
for group in groups:
    print(str(i) + '- ' + group.title)
    i+=1

g_index = input("Enter a Number: ")
target_group=groups[int(g_index)]

target_group_entity = InputPeerChannel(target_group.id,target_group.access_hash)

mode = int(input("Enter 1 to add by username or 2 to add by ID: "))

for user in users:
    try:
        print ("Adding {}".format(user['id']))
        if mode == 1:
            if user['username'] == "":
                continue
            user_to_add = client.get_input_entity(user['username'])
        elif mode == 2:
            user_to_add = InputPeerUser(user['id'], user['access_hash'])
        else:
            sys.exit("Invalid Mode Selected. Please Try Again.")
        client(InviteToChannelRequest(target_group_entity,[user_to_add]))
        print("Waiting 60 Seconds...")
        time.sleep(60)
    except PeerFloodError:
        print("Getting Flood Error from telegram. Script is stopping now. Please try again after some time.")
    except UserPrivacyRestrictedError:
        print("The user's privacy settings do not allow you to do this. Skipping.")
    except:
        traceback.print_exc()
        print("Unexpected Error")
        continue
Lonami commented 5 years ago

Not an issue with the library, it's an error from Telegram.

C0deL0ver commented 5 years ago

Umm... It is clearly saying UserIdInvalidError. I've also read some other discussions here. Do you think it has anything to do with the access.hash ?

C0deL0ver commented 5 years ago

Is there a way other than InviteToChannelRequest to add members to a super group?

C0deL0ver commented 5 years ago

@Lonami could you please answer my last questions?

bilalahmed-125 commented 4 years ago

@C0deL0ver would you find out the answer? Because I'm facing the same problem while adding a user from a different account. @Lonami If you solve this error so please tell me about how to solve it.

Lonami commented 4 years ago

No help will be given by me in the issues section. Nor will most people help others try to spam (inviting to channel is nearly always spam and there are better alternatives). Hence the lack of a response.

jpatk123 commented 3 years ago
        user_to_add = InputPeerUser(user['id'], user['access_hash'])

is the culprit, seems this is not supported method, though document says supported. or its bug in telethon

umitkose commented 3 years ago
        user_to_add = InputPeerUser(user['id'], user['access_hash'])

is the culprit, seems this is not supported method, though document says supported. or its bug in telethon

@jpatk123 did you find a solution to this problem. I am having the same problem.

UsernameNotFound403 commented 3 years ago

Has anyone a solution for this problem?

Lonami commented 3 years ago

Locking since there is nothing to fix here. Access hash is unique per account and adding users is spam. That's all.