T
T
Tayrus02019-12-18 19:43:35
Python
Tayrus0, 2019-12-18 19:43:35

How to split the input data of telegram bot users?

I am writing a bot in a telegram, the bot has a button, when you click on it, the user's input is processed through register_next_step_handler(), then a problem appears, for example: user1 clicks on this button and enters a value, the value is entered into a variable and stored there, but if another user enters, he clicks on this button, enters a value, then the variable will be updated to the user2 input, I need to somehow separate users (there may be an unlimited number of them), so that the input value is assigned to users, how can this be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2019-12-19
@Tayrus0

you can use sqlite to pull values ​​later by user_id

import sqlite3 as db
from sqlite3 import Error
import time


# handler по команде старт передаст нужные поля из объекта message в функцию добавления юзера в дб
@bot.message_handler(commands=['start'])
def start(message):
    tupp = (message.from_user.id, message.from_user.username, message.from_user.first_name, message.from_user.last_name, time.ctime())
    send_user_info(tupp)


# один раз запускаешь для создания БД и таблицы в ней
with db.connect('bot_db') as connection:
    cursor = connection.cursor()
    cursor.execute('''CREATE TABLE IF NOT EXISTS  USERS (user_id INTEGER NOT NULL PRIMARY KEY, first_name TEXT, last_name TEXT, username TEXT)''')


# вызываемая функция 
def send_user_info(tup):
    connection = db.connect('bot_db'))
    cursor = connection.cursor()
    try:
        cursor.execute("INSERT INTO USERS (user_id, username, first_name, last_name, reg_date) VALUES(?,?,?,?,?)", tup)
    except Error:
        pass
    connection.commit()
    connection.close()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question