【问题标题】:EditBannedRequest doesn't work for bot in private channelEditBannedRequest 不适用于私人频道中的机器人
【发布时间】:2020-09-01 21:05:00
【问题描述】:
  1. 我已尝试将 EditBannedRequest 方法用于私人频道, 与客户端一起使用时效果很好,但是,当我使用机器人时 拥有频道管理员权限,我收到此错误 Main.telethon.errors.rpcerrorlist.BotMethodInvalidError:API 机器人用户的访问受到限制。您尝试调用的方法 无法作为机器人执行(由 CheckChatInviteRequest 引起)
  2. 我处理了 EditBannedRequest 方法直接接收有效 没有执行 get_input_channel 和 get_input_entity 的实体 方法...然后,我打印了为机器人生成的值,然后 与为客户端生成的值进行比较,结果相等。

例如:

在 Telegram 方法中,我修改了 resolve 函数:

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))

客户端生成的值: InputChannel(channel_id=XXXXXX, access_hash=XXXXXX)

机器人发送的值: INVITE_ACCESS = [InputChannel(channel_id=XXXXXXX, access_hash=XXXXXXX)] USER = [InputUser(user_id=XXXXXXX, access_hash=-XXXXXXX)]

功能是这样的:

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())

最后的错误是这样的:

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)

有什么方法可以让机器人在私人频道中毫无问题地运行 EditBannedRequest 方法?

【问题讨论】:

    标签: telegram telegram-bot telethon


    【解决方案1】:

    EditBannedRequest 可以被机器人正常使用,但机器人(如错误所示)不能使用CheckChatInviteRequest

    access_hash 对于每个帐户都是唯一的(帐户 A 将看到个人 C 的哈希值为 123,帐户 B 将看到个人 C 的哈希值为 456)。

    您应该使用频道 peer(或标记的 ID)让 Telethon 知道您指的是频道。此外,您应该使用client.edit_permissions,它比原始 API 更好用:

    chat = types.PeerChannel(123)
    # chat = -100123  # equivalent, bot-API style channel ID
    
    # Banning `user` from `chat` forever
    await client.edit_permissions(chat, user, view_messages=False)
    

    【讨论】:

      猜你喜欢
      • 2021-06-17
      • 2018-12-05
      • 1970-01-01
      • 2017-09-02
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多