0

您好,我是 python 和电报 api 的新手,所以我有一些问题。我正在使用用户个人资料创建电报机器人(python 电报 api)。我已经创建了数据库(mysql.connector)并在注册后存储了所有用户信息。我也创建了用户类。当用户键入 /start 时,我正在检查它是否存在,如果存在,我正在填写此类。然后,如果用户单击按钮(我的个人资料),我将使用此类显示一些个人资料信息(照片、姓名、年龄等)。所以问题是当我同时有 2 个用户时。首先输入“/start”并登录,想看自己的个人资料,一切都很好。但是当第二个用户做同样的事情时,当我点击(我的个人资料)时,我得到了第一个用户,他或她得到了最后一个为两个用户加载“/start”的个人资料。如何解决这个问题?一直检查和加载数据的解决方案听起来不太好,我想用“class Users”做些事情,但我不知道让它对每个用户会话都唯一。有什么解决办法吗?如果需要,我可以提供更多代码,请问。

class Users:
def __init__(self, id=0, name='', age=0, gender='', balance=0, telegram_id=0, photo='', sallarytext=0, sallaryvideo=0, videocall=0):
    self.id = id
    self.name = name
    self.age = age
    self.gender = gender
    self.balance = balance
    self.telegram_id = telegram_id
    self.photo = photo
    self.sallarytext = sallarytext
    self.sallaryvideo = sallaryvideo
    self.videocall = videocall


user = Users()


def check_auth(connection, telegram_id):
cursor = connection.cursor()
result = None
try:
    cursor.execute("SELECT * FROM users WHERE telegram_id = '%s'" % telegram_id)
    result = cursor.fetchall()
    data = []
    if result:
        for row in result:
            user.id = row[0]
            user.name = row[1]
            user.age = row[2]
            user.gender = row[3]
            user.telegram_id = row[4]
            user.balance = row[5]
            data = [user.name]
        if user.gender == 'Female':
            cursor.execute("SELECT * FROM photos WHERE users_id = '%s'" % user.id)
            result2 = cursor.fetchall()
            for row in result2:
                user.photo = row[1]
                user.sallarytext = row[2]
                user.sallaryvideo = row[3]
                user.videocall = row[4]
        return data
except Error as e:
    print(f"The error '{e}' occurred")


@bot.message_handler(commands=['start'])
def check_reg(message):
    if message.chat.type == 'private':
        telegram_id = message.from_user.id
        # create_db_users(connection)
        # create_db_photos(connection)
        # create_db_chats(connection)
        data_user = check_auth(connection, telegram_id)
        if not data_user:
            new_user(message) # user registration
        else:
            if user.gender == 'Male':
                default_user_keybord(message) # show user keybord
            elif user.gender == 'Female':
                default_model_keybord(message)


def show_profile(message): # funtion show profile when user click on "My profile" button
        profile_text = "Profile\n\nYour name: " + user.name + "\nYour age: " + str(
            user.age)
        menu_keybord = types.ReplyKeyboardMarkup(row_width=2, resize_keyboard=True)
        button_name_age = types.KeyboardButton(text=" Change name/age")
        button_back = types.KeyboardButton(text="◀️ Return")
        menu_keybord.add(button_name_age, button_back)
        bot.send_message(message.chat.id, profile_text, reply_markup=menu_keybord)
4

1 回答 1

0

你能告诉我你正在使用什么电报api包吗?

我认为,您问题的核心是使用全局变量user来存储用户数据。最好的做法是在Users每次调用时实例化并返回一个新的check_auth

话虽如此,

  1. 在 Python 中,如果你想更新一个全局变量,比如说user,你必须global user在这样做之前使用语句;
  2. 考虑使用诸如SQLAlchemy之类的 ORM 来省去一些麻烦和代码。

让我知道这是否解决了您的问题。

C

于 2022-03-02T02:36:20.990 回答