Ошибка telethon: telethon.errors.rpcerrorlist.UserBotError: Bots can only be admins in channels. (caused by InviteToChannelRequest)

Я хотел добавить бота в свой канал через telethon:

bot_entity = await client.get_entity(bot_username)
channel_entity = await client.get_entity(channel_username)
result = await client(telethon.functions.channels.InviteToChannelRequest(
       channel=channel_entity,
       users=[bot_entity]
))

На что получил следующую ошибку:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\threading.py", line 980, in _bootstrap_inner
    self.run()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\threading.py", line 917, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Shtirmann\PycharmProjects\TelegramAuto\app.py", line 120, in go
    asyncio.run(process(current_topic, thread_number, console))
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\asyncio\base_events.py", line 647, in run_until_complete
    return future.result()
  File "C:\Users\Shtirmann\PycharmProjects\TelegramAuto\threadwork.py", line 32, in process
    await go(console, current_topic, f"{thread_number}:{xz + 1}", lock, xz)
  File "C:\Users\Shtirmann\PycharmProjects\TelegramAuto\threadwork.py", line 37, in go
    await work(console, current_topic, thread_number, lock, xz)
  File "C:\Users\Shtirmann\PycharmProjects\TelegramAuto\threadwork.py", line 170, in work
    result = await client(telethon.functions.channels.InviteToChannelRequest(
  File "C:\Users\Shtirmann\PycharmProjects\lib\site-packages\telethon\client\users.py", line 30, in __call__
    return await self._call(self._sender, request, ordered=ordered)
  File "C:\Users\Shtirmann\PycharmProjects\lib\site-packages\telethon\client\users.py", line 83, in _call
    result = await future
telethon.errors.rpcerrorlist.UserBotError: Bots can only be admins in channels. (caused by InviteToChannelRequest)

Я собирался дать боту права админа после его добавления в канал, но Telethon требует чтобы они у него уже были в момент добавления. Как исправить данную ошибку\дать боту права при добавлении?


Ответы (1 шт):

Автор решения: Nevertheless

Нашёл ответ, надо попросту вместо приглашения бота сразу дать ему права:

bot_entity = await client.get_entity(bot_username)
channel_entity = await client.get_entity(desiredPublicUsername)
result = await client(telethon.functions.channels.EditAdminRequest(
            channel=channel_entity,
            user_id=bot_entity,
            admin_rights=telethon.types.ChatAdminRights(
                change_info=True,
                post_messages=True,
                edit_messages=True,
                delete_messages=True,
                ban_users=True,
                invite_users=True,
                pin_messages=True,
                add_admins=True,
                anonymous=True,
                manage_call=True,
                other=True,
                manage_topics=True
            ),
            rank="Poster"
        ))
→ Ссылка