E
E
enabl32018-03-30 14:01:29
Python
enabl3, 2018-03-30 14:01:29

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!")

Responds to everyone that there is no access.
Working version:
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!")

How to fix?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Fadi Haj, 2018-03-31
@enabl3

Perhaps message.chat.idit 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!')

S
spikejke, 2018-03-31
@spikejke

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 question

Ask a Question

731 491 924 answers to any question