Telethon错误:无法使用AddChatUserRequest()将用户添加到群组,聊天的对象ID无效

时间:2019-09-03 16:10:55

标签: python-3.x telegram-bot telethon

当我使用AddChatUserRequest将用户添加到网上论坛时,我尝试使用Telethon开发电报机器人。

client(AddChatUserRequest(1168070860, user.id, fwd_limit=10))

我遇到了错误:

  

telethon.errors.rpcerrorlist.ChatIdInvalidError:的无效对象ID   聊天。确保传递正确的类型,例如确保   该请求是为聊天而设计的(不是频道/大型群组),或者   否则,寻找另一种更适合的\ n一个示例工作   与一个megagroup和AddChatUserRequest,它将失败,因为   大群是渠道。请改用InviteToChannelRequest(由   AddChatUserRequest)

我使用以下代码获取论坛的信息:

chats = client(GetDialogsRequest(
             offset_date=None,
             offset_id=0,
             offset_peer=InputPeerEmpty(),
             limit=200,
             hash = 0
         )).chats

groups = []
for chat in chats:
    try:
        if chat.megagroup == True:
            groups.append(chat)
            print(chat.title,chat.id)
    except:
        continue

这是我尝试添加用户的网上论坛信息:

Channel(left=False, id=1168070860, banned_rights=None, title='ruby and his friends', version=0, verified=False, signatures=False, broadcast=False, participants_count=None, creator=True, has_geo=False, photo=ChatPhoto(dc_id=1, photo_small=FileLocationToBeDeprecated(local_id=270793, volume_id=806122192), photo_big=FileLocationToBeDeprecated(local_id=270795, volume_id=806122192)), username='lanlugroup', megagroup=True, restricted=False, default_banned_rights=ChatBannedRights(change_info=True, until_date=datetime.datetime(2038, 1, 19, 3, 14, 7, tzinfo=datetime.timezone.utc), send_inline=False, invite_users=False, pin_messages=True, send_media=False, send_gifs=False, send_games=False, embed_links=False, send_stickers=False, send_messages=False, view_messages=False, send_polls=False), restriction_reason=None, min=False, has_link=False, scam=False, date=datetime.datetime(2019, 4, 1, 12, 44, 47, tzinfo=datetime.timezone.utc), admin_rights=None, access_hash=-5901560198799322202)

我使用ID 1168070860作为AddChatUserRequest中的chat_id

(顺便说一句,我以普通用户身份而不是机器人身份使用电话号码登录)

1 个答案:

答案 0 :(得分:0)

改用 InviteToChannelRequest。我的解决方案:

chat_id = "mychatid"
users = ['UserName']

response = client.invoke(ResolveUsernameRequest(chat_id))

target_group_entity = InputPeerChannel(response.chats[0].id, response.chats[0].access_hash)

try:
    res = client(InviteToChannelRequest(channel=target_group_entity, users=users ))
except Exception as e:
    print("spam protection: " + e.message + ": " + str(e))