簡體   English   中英

在 Telegram 中獲取用戶列表

[英]Get User List in Telegram

我想在 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: name 'a ' 沒有定義

我該做什么?

以及如何獲取頻道成員列表

首先, 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 object 的更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM