簡體   English   中英

Python 電報機器人,如何為每個用戶存儲和顯示用戶數據?

[英]Python telegram bot, how to store and show users data for each users?

您好,我是 python 和電報 api 的新人,所以我有一些問題。 我正在使用用戶配置文件創建電報機器人(python 電報 api)。 我已經創建了數據庫(mysql.connector)並在注冊后將所有用戶信息存儲在那里。 我還創建了用戶 class。當用戶鍵入 /start 我檢查它是否存在,如果存在,我將填寫此 class。然后我使用此 class 顯示一些個人資料信息(照片、姓名、年齡等)如果用戶點擊按鈕(我的個人資料)。 所以問題是當我同時有 2 個用戶時。 首先輸入“/start”並登錄,想看自己的個人資料,一切正常。 但是當第二個用戶做同樣的事情時,我得到了第一個用戶點擊(我的個人資料)時,他或她得到了最后一個為兩個用戶加載的輸入“/ start”的個人資料。 如何解決這個問題? 始終檢查和加載數據的解決方案聽起來不太好,我想對“類用戶”做一些事情,但我不知道要為每個用戶 session 設置唯一性。有什么解決方案嗎? 如果需要,我可以提供更多代碼,只問。

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)

你能告訴我你正在使用什么電報 api package 嗎?

我認為,您問題的核心是使用全局變量user來存儲用戶數據。 每次調用check_auth時實例化並返回一個新Users是最佳實踐。

話雖如此,

  1. 在 Python 中,如果要更新全局變量user ,則必須先使用語句global user
  2. 考慮使用 ORM (例如SQLAlchemy)來省去一些麻煩和代碼。

讓我知道這是否解決了您的問題。

C

我從用戶那里得到 email 並將其保存在一個元組中:) 我只想為他們自己顯示用戶數據️我的機器人實際上向每個人顯示。 任何人都可以

我已經通過向數據庫發出請求,獲取數據並將其推送到 class,然后關閉與數據庫的連接並向用戶顯示來解決此問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM