Answer the question
In order to leave comments, you need to log in
Access to the bot by chat.id, How?
Please tell me, I want to put all the chat id that the bot is available to in a separate file where the IDs will be indicated line by line, I do this:
file = open("users.txt")
users = file.readlines()
file.close()
@bot.message_handler(func=lambda message: message.chat.id not in users)
def access_msg(message):
bot.send_message(message.chat.id, "No Access!")
users = [тут чат ид через запятую]
@bot.message_handler(func=lambda message: message.chat.id not in users)
def access_msg(message):
bot.send_message(message.chat.id, "No Access!")
Answer the question
In order to leave comments, you need to log in
Perhaps message.chat.id
it is a number, so you need to cast all lines from users.txt to a numeric type.
Try like this:
with open('users.txt', 'r') as fp:
user_ids = [int(l) for l in fp.readlines()]
@bot.message_handler(func=lambda message: message.chat.id not in user_ids)
def access_msg(message):
bot.send_message(message.chat.id, 'No Access!')
You might find it easier to use the pickle module https://docs.python.org/3.6/library/pickle.html
import pickle
users = [тут чат ид через запятую]
#Записываем Вашу переменную в файл:
with open('chat.id', 'wb') as f:
pickle.dump(users,f)
#Когда нам нужна переменная читаем из файла
with open('chat.id', 'rb') as f:
users = pickle.load(f)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question