When a user starts my bot, it gets its user id and stores it in the .txt file.
def verify_id(update, context):
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
f = open(os.path.join(__location__, 'users_list_file.txt'), 'w+')
f_content = f.read().strip().split()
registered = False
user_id = str(update.message.from_user.id)
if user_id in f.read():
message_ok = "User already existing in DB"
update.message.reply_text(message_ok)
registered = True
else:
f.write(user_id)
message_added = "You have been added to my list :)"
update.message.reply_text(message_added)
f.close()
Objectives:
- If user hasn't ever started the bot, it should add its user id to the .txt file.
- If user has already been "registered", the bot should print some text letting the user know that they had already been registred.
Problems:
- After it adds a user id to the file, if you retry the same command with the same user it returns the text "You have been added to my list :)", meaning that the file could not be read, to verify if the user id already exists?
- I tested with another user, and it seems like the bot erases the value of the last user to replace it with the user id of the latest one. I want just to add, not to replace.
Any suggestion would really help me a lot...
Python version: 3.6.5