首页 > 解决方案 > 获取频道/聊天实体

问题描述

我希望有人可以将我的机器人添加到他的频道。为此,我想将频道的 webhook (chat.id) 保存在数据库中。

我的代码:

async with bot.conversation(chat.id) as conv:
        await conv.send_message("Which channel you want add? (invite link of the channel):")
        resp = await conv.get_response()
        hookName = resp.message

        hook = await bot.get_entity(hookName)
        print(hook)
        print(hook.chat_id)
        print(hook.user_id)

当我使用邀请链接时,我在 get_entity 行收到此错误:

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)

当我使用频道名时出现这个错误:

ValueError: No user has "channelname" as username

我想知道为什么当我硬编码频道名称而不是作为响应时它会起作用......

如果用户可以输入聊天名称或频道名称或邀请链接,我会从中获得 id,那就太好了。

有没有办法做到这一点?

谢谢 :)

标签: pythontelegram-bottelethontelegram-webhook

解决方案


推荐阅读