Answer the question
In order to leave comments, you need to log in
How to make it so that if the conditions are not met, the bot stops further work?
There is the following code, which checks if the user has an id in the list. If it is not present, then a message is displayed.
import telebot
bot = telebot.TeleBot('мой токен')
users = [id1,id2,id3]
@bot.message_handler(func=lambda message: message.chat.id not in users)
def CheckUser(message):
bot.send_message(message.chat.id, "Извини, но ты не можешь работать с ботом")
bot.polling(none_stop=True, interval=0)
Answer the question
In order to leave comments, you need to log in
as an option to raise an error.
class DontNeedToDo(Exception):
pass
@bot.message_handler(func=lambda message: message.chat.id not in users)
def CheckUser(message):
bot.send_message(message.chat.id, "Извини, но ты не можешь работать с ботом")
raise DontNeedToDo()
# Ограничение доступа к боту по ID
@bot.message_handler(func=lambda message: message.chat.id not in users)
def some(message):
bot.send_message(message.chat.id, 'Не дозволено общаться с незнакомцами')
# Ограничение выполнение команды start
@bot.message_handler(func=lambda message: message.chat.id not in users, commands=['start'])
def some(message):
bot.send_message(message.chat.id, 'Не дозволено')
# Ограничение выполнение команд
@bot.message_handler(func=lambda message: message.chat.id not in users_command,
commands=['command1', 'command2', 'command3', 'command4'])
def some(message):
bot.send_message(message.chat.id, 'Не дозволено')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question