collect user info telegram bot old

PHOTO EMBED

Thu Nov 16 2023 23:25:54 GMT+0000 (Coordinated Universal Time)

Saved by @joeclaap #telegram #bot #user #info #collect #store #json

import json

user_info_file_path = 'userinfo.json'

def save_user_info(chat_id, user_id, first_name, last_name, username):
    # Load existing user information from the file
    user_info = load_user_info()

    # Add or update user information
    user_info[user_id] = {
	"user_id": user_id,
	"username": username,
    "chat_id": chat_id,
    "first_name": first_name,
    "last_name": last_name
    }

    # Save the updated user information back to the file
    with open(user_info_file_path, 'w') as file:
        json.dump(user_info, file)

def load_user_info():
    try:
        # Load user information from the file
        with open(user_info_file_path, 'r') as file:
            return json.load(file)
    except FileNotFoundError:
        # If the file doesn't exist yet, return an empty dictionary
        return {}
content_copyCOPY