首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检查bot-用户是特定聊天或电报频道的成员?python-电报-bot

如何检查bot-用户是特定聊天或电报频道的成员?python-电报-bot
EN

Stack Overflow用户
提问于 2022-04-05 15:43:49
回答 1查看 1.2K关注 0票数 0

如何检查bot-用户是特定频道的成员还是聊天成员?

我是蟒蛇-电报-机器人的新手。所以,请给我完整的回答。Ps。我用的是python,我用的方法是:成员()

代码语言:javascript
复制
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

此功能只适用于某些聊天和电报频道,但有时我会遇到错误(telegram.error.BadRequest: User )。但我确信用户是电报频道的一员,如果您有更好的解决方案,请与我们分享。

EN

回答 1

Stack Overflow用户

发布于 2022-05-19 18:10:10

如果您想在一个组或其他地方找到一个成员,首先您需要知道目标组的id。然后你要看到目标用户的ID。

当用户向bot发送消息时,让我们检索此信息。

代码语言:javascript
复制
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')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71754736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档