简体   繁体   中英

AttributeError: 'ChatForbidden' object has no attribute 'access_hash'

I have made a telegram member scraper and inviter with Python. It was successful on some tests, but there are times my accounts get banned with upon seeing this error message:

AttributeError: 'ChatForbidden' object has no attribute 'access_hash'

I'm not sure why would it show ChatForbidden if I am already an admin of a group. It's hard to test these as I had to buy new phone numbers every time.

Here's a sample and explanation of my code to invite members to a group:

# Log in into the telegram account
client = TelegramClient('Tg_scraper', api_id, api_hash)

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

# Get all the groups/channel of the account
result = client(GetDialogsRequest(
    offset_date=last_date,
    offset_id=0,
    offset_peer=InputPeerEmpty(),
    limit=chunk_size,
    hash=0
))
chats.extend(result.chats)

# Puts all the group/channel into a list
i = 0
print('Enter a NUMBER to choose a group where the members will be invited into:')
for chat in chats:
    try:
        groups.append(chat)
        hash_list.append(chat.access_hash)
        print(f"({i})" + ' - ' + chat.title)
        i += 1
    except:
        continue

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

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

Upon the last line, target_group_entity = InputPeerChannel(target_group.id, target_group.access_hash) is where I encounter the error I have stated above. Upon receiving that error, I get banned.

Does this have something to do with permissions? Do new accounts get banned for botting? It works on my first few tests, but then now I can't invite. Thank you so much for anyone who could help in advance.

I am already an admin of a group

This error is unrelated to your permission level.

Upon the last line is where I encounter the error

Wrong. you encounter this error because you're not coding it right with types in mind, expecting all your.chats are groups. Telegram doesn't tell you what fields exactly have, as you see in this error. You must use type checking to limit your chats objects to only what you expect, your try block is appending then erroring, so, rather than a plain:

except:
        continue

you need to actually confirm it won't error when accessing fields.

print('Enter a NUMBER to choose a group where the members will be invited into:')

i = 0

for chat in chats:
    if isinstance(chat, telethon.types.Channel):
        if chat.broadcast: continue # ignore non-group
        groups.append(chat)
        hash_list.append(chat.access_hash)
        print(f"({i})" + ' - ' + chat.title)
        i += 1

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM