Python Telegram 机器人 + 第 3 方监听器

Python Telegram Bot + 3rd Party Listener

我正在使用 Python Telegram Bot https://python-telegram-bot.readthedocs.io/

我正在尝试将机器人功能实现到第 3 方侦听器中,因此当 handle_event() 被触发时,机器人将电报用户名作为参数,禁止该成员加入群组。我们举个例子:

def handle_event(event):

        result = offerCancelled_event.processReceipt(receipt) 

        #Double check if we got a result
        if(result):

            #Set telegram user
            telegram_user = "Ulvur"

            users.remove(telegram_user)
            file = open('users.txt', 'w+')
            for user in users:
                file.write('%s\n' % user)
                file.close()

            if(telegram_user not in users):
                Update.effective_chat.ban_member(telegram_user)

以下代码在触发函数时返回 AttributeError: 'property' object has no attribute 'ban_member'

Update 是一个 class,而 属性 Update.effective_chat 只能针对 class 的实例进行正确评估。如果你想对 Bot API 进行简单调用,你应该实例化 telegram.Bot class 的实例并调用它的方法 - 在你的例子中 Bot.ban_chat_member. Please see the Introduction to the API 更详细关于如何使用 API 方法的说明。