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

EditBannedRequest doesn't work for bot in private channel #1544

Closed ldelgadop44 closed 4 years ago

ldelgadop44 commented 4 years ago

Checklist

  1. I have tried use the EditBannedRequest method for a private channel, when used it with a client works fine, however, when i use a bot with administrator permission over the channel, I got this error Main.telethon.errors.rpcerrorlist.BotMethodInvalidError: The API access for bot users is restricted. The method you tried to invoke cannot be executed as a bot (caused by CheckChatInviteRequest)

  2. I handled EditBannedRequest method for receive directly valid entities without execute _get_inputchannel and _get_inputentity methods... Then, I printed the values generated for the bot and compared with values generated for the client, and were equals.

For example:

In the method, I modified resolve function so:

async def resolve(self, client, utils):
        if isinstance(self.channel, InputChannel) and isinstance(self.user_id, InputUser):
            self.channel = self.channel
            self.user_id = self.user_id
        else:
            self.channel = utils.get_input_channel(await client.get_input_entity(self.channel))
            self.user_id = utils.get_input_user(await client.get_input_entity(self.user_id))

And the function is this:

with TelegramClient(phone, api_id, api_hash).start(bot_token=bot_token) as bot:
    result = bot(functions.channels.EditBannedRequest(
        channel=INVITE_ACCESS[0],
        user_id=USER[0],
        banned_rights=types.ChatBannedRights(
            until_date=None,
            view_messages=True,
            send_messages=True
        )
    ))
    print(result.stringify())

The final error is this:

Traceback (most recent call last):
  File "banneduser.py", line 41, in <module>
    client(EditBannedRequest(
  File "/usr/local/lib/python3.8/dist-packages/telethon/sync.py", line 39, in syncified
    return loop.run_until_complete(coro)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/usr/local/lib/python3.8/dist-packages/telethon/client/users.py", line 30, in _call_
    return await self._call(self._sender, request, ordered=ordered)
  File "/usr/local/lib/python3.8/dist-packages/telethon/client/users.py", line 77, in _call
    result = await future
telethon.errors.rpcerrorlist.ChannelInvalidError: Invalid channel object. Make sure to pass the right types, for instance making sure that the request is designed for channels or otherwise look for a different one more suited (caused by EditBannedRequest)
Lonami commented 4 years ago

You're using "raw API", which means you're directly talking to Telegram, and the error also comes from Telegram. This is not a bug.

Bots can't check invite links to obtain the ID / access hash. Instead you should rely on the channel ID or username. Access hashes are per account and the channel will be invalid if a bad hash is used. If you have more questions, you should ask those in StackOverflow or the @TelethonChat group.