簡體   English   中英

如何檢查機器人用戶是特定聊天或電報頻道的成員? python電報機器人

[英]how to check bot-user is member of specific chat or telegram channel? python-telegram-bot

如何檢查機器人用戶是特定頻道的成員還是聊天成員?
我是 python-telegram-bot 的新手。 所以,請給我完整的答案。 附言。 我正在使用 python-telegram-bot 我使用了方法: get_chat_member()

def is_member(update, context):
    userid = update.effective_user.id
    chatid = "@chatusername"
    try:
       res = context.bot.get_chat_member(chat_id=chatid, user_id=userid)
       if res.status in ['member', 'administrator']:
           return True
    except Exception as e:
       return False

此 function 僅適用於某些聊天和電報頻道,但有時我會遇到錯誤(telegram.error.BadRequest:找不到用戶)。 但我確定用戶是電報頻道的成員如果您有更好的解決方案,請與我們分享

如果你想在一個組或其他地方找到一個成員,首先你需要知道目標組 ID。 然后你必須看到目標用戶 ID。

讓我們在用戶向機器人發送消息時檢索此信息。

def check_user_in_the_group(update,context):

    group_chat_id = update.message.chat.id #replace this with group id number, if you already know it
    user_id = update.message.from_user.id #replace this with user id number, if you already know it


    check = context.bot.getChatMember(chat_id,user_id) #check if the user exist in the target group

    if check: #If check variable isn't null, user is in the group
        print('user is in the chat')
    else:
        print('Not found')

暫無
暫無

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

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