首页 > 解决方案 > 限制访问电报机器人的问题

问题描述

我对python真的很陌生,我希望我的机器人回答只通过用户名限制用户。我正在尝试编写此功能,但我的机器人仅发送一条消息“未授权”。

怎么了?

number_list.append(choice)
if update.effective_user.username not in  ["username"]:
    query.edit_message_text(text="Not authorized")
    return

标签: python-3.xtelegram-bot

解决方案


您可以使用 Telebot api

import telebot 

bot = telebot.TeleBot("TOKEN")

username = ["USERNAME"]

@bot.message_handler(content_types=["text"])
def default_test(message):
    if message.from_user.username not in username:
        bot.send_message(message.chat.id, "Not authorized")
        
    else:
        bot.send_message(message.chat.id, "Authorized")
        
bot.polling(none_stop=True)


推荐阅读