【问题标题】:Get User List in Telegram在 Telegram 中获取用户列表
【发布时间】:2020-06-21 21:29:41
【问题描述】:

我想在 Python-Telegram-Bot 中获取具有此功能的频道成员列表:

context.bot.getChatMember("@*****",update.message.chat_id)

我这样做:

a=context.bot.getChatMember("@*****",update.message.chat_id)

但是当我想打印时,如果该用户不在该频道中,它不会返回任何内容,但如果该用户在该频道中,它会返回用户信息

如果该用户不在该频道上,我想获取 (a) 的值。

我使用了 pdb,我得到了 'telegram.error.BadRequest: User not found' 这个错误,但是当我想打印它时什么也不打印,当我在 pdb 中打印 (a) 时,它显示 *** NameError:名称“a”未定义

我该怎么办?

以及如何获取频道成员列表

【问题讨论】:

  • 你能告诉我们你的错误吗?
  • 我使用了 pdb,我得到了 'telegram.error.BadRequest: User not found' 这个错误,但是当我想打印它时什么也不打印,当我在 pdb 中打印(a)时它说*** NameError: name 'a' is not defined
  • 啊,那你能把它加到你的问题里吗?
  • 抱歉,我正在添加。

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


【解决方案1】:

首先,getChatMember 方法用于检查用户是否是特定的聊天(组/频道)。所以使用这种方法,您无法检索频道成员列表。

至于您收到错误的原因是理想情况下 ptb 如果在该聊天中找不到用户,则会引发 BadRequest: User not found这不是很明显吗! )。你必须使用异常块来处理这些。

假设您要检查用户是否在聊天

from telegram.error import BadRequest
def is_user_there(channel, chat_id) -> bool:
    try:
       chat_member = context.bot.getChatMember(channel,update.message.chat_id) # this will return a ChatMember object
       if chat_member.status in [‘administrator’, ‘creator’, ‘member’]:
         return True
       else:
         return False
    except BadRequest:
       return False

在此处阅读有关ChatMember 对象的更多信息

【讨论】:

    猜你喜欢
    • 2022-07-28
    • 2016-02-23
    • 2022-08-22
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    相关资源
    最近更新 更多