Answer the question
In order to leave comments, you need to log in
Telegram bot questionnaire saves data in the database out of order, how to fix the error?
Hello, I am writing a telegrambot prog, a profile on python, connected with MySQL, but the entered data is not saved, not in the correct order, please help here is the code
import telebot
from telebot import types # кнопки
import mysql.connector
bot = telebot.TeleBot("")
db = mysql.connector.connect(
host="localhost",
user="",
passwd="",
port="3306",
database="diplomka"
)
cursor = db.cursor()
user_data = {}
class User:
def __init__(self, city):
self.city = city
self.fio = ''
self.telephone = ''
self.iin = ''
self.adress = ''
self.napravlenie = ''
@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True)
itembtn1 = types.KeyboardButton('Павлодар')
itembtn2 = types.KeyboardButton('Экибастуз')
itembtn3 = types.KeyboardButton('Нур-Султан')
itembtn4 = types.KeyboardButton('Алматы')
itembtn5 = types.KeyboardButton('Атырау')
itembtn6 = types.KeyboardButton('Тараз')
markup.add(itembtn1, itembtn2, itembtn3, itembtn4, itembtn5, itembtn6)
msg = bot.send_message(message.chat.id, 'Ваш город?', reply_markup=markup)
bot.register_next_step_handler(msg, process_city_step)
def process_city_step(message):
try:
user_id = message.from_user.id
user_data[user_id] = User(message.text)
msg = bot.send_message(message.chat.id, "Введите Ф.И.О")
bot.register_next_step_handler(msg, process_fio_step)
except Exception as e:
bot.reply_to(message, 'oooops')
def process_fio_step(message):
try:
user_id = message.from_user.id
user_data[user_id] = User(message.text)
msg = bot.send_message(message.chat.id, "Введите номер Вашего телефона")
bot.register_next_step_handler(msg, process_telephone_step)
except Exception as e:
bot.reply_to(message, 'oooops')
def process_telephone_step(message):
try:
user_id = message.from_user.id
user_data[user_id] = User(message.text)
msg = bot.send_message(message.chat.id, "Введите свой ИИН")
bot.register_next_step_handler(msg, process_iin_step)
except Exception as e:
bot.reply_to(message, 'oooops')
def process_iin_step(message):
try:
user_id = message.from_user.id
user_data[user_id] = User(message.text)
msg = bot.send_message(message.chat.id, "Введите свой адресс")
bot.register_next_step_handler(msg, process_adress_step)
except Exception as e:
bot.reply_to(message, 'oooops')
def process_adress_step(message):
try:
user_id = message.from_user.id
user_data[user_id] = User(message.text)
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True)
itembtn1 = types.KeyboardButton('сложное МП')
itembtn2 = types.KeyboardButton('среднее МП')
itembtn3 = types.KeyboardButton('простое МП')
itembtn4 = types.KeyboardButton('сайт-визитка')
itembtn5 = types.KeyboardButton('интернет магазин')
itembtn6 = types.KeyboardButton('сайт с базой данных')
markup.add(itembtn1, itembtn2, itembtn3, itembtn4, itembtn5, itembtn6)
msg = bot.send_message(message.chat.id, "Выберите направление разработки", reply_markup=markup)
bot.register_next_step_handler(msg, process_napravlenie_step)
except Exception as e:
bot.reply_to(message, 'oooops')
def process_napravlenie_step(message):
try:
user_id = message.from_user.id
user = user_data[user_id]
user.napravlenie = message.text
sql = "INSERT INTO users (city, fio, telephone, iin, adress, napravlenie, user_id) \
VALUES (%s, %s, %s, %s, %s, %s, %s)"
val = (user.city, user.fio, user.telephone, user.iin, user.adress, user.napravlenie, user_id)
cursor.execute(sql, val)
db.commit()
bot.send_message(message.chat.id, "Заявка подана!")
except Exception as e:
bot.reply_to(message, 'Ошибка !')
bot.enable_save_next_step_handlers(delay=2)
bot.load_next_step_handlers()
if __name__ == '__main__':
bot.polling(none_stop=True)
Answer the question
In order to leave comments, you need to log in
You overwrite the value in the dictionary, passing from handler to handler, at least you didn’t delve into the rest.
Here is the code for you to understand what you need to do:
user_id = 123
get_id_from_db = 6
user_data = {user_id: {'city':'', 'fio':'', 'phone':'', 'inn':'', 'address':'', 'napravlenie':'', 'id': get_id_from_db}}
print(user_data)
user_data[user_id]['city'] = 'Павлодар'
print(user_data)
user_data[user_id]['fio'] = 'Иванов а м'
print(user_data)
user_data[user_id]['phone'] = '77777777'
print(user_data)
user_data[user_id]['inn'] = '4679085467'
print(user_data)
user_data[user_id]['address'] = 'Петрова 65 кв 98'
print(user_data)
user_data[user_id]['napravlenie'] = 'сайт-визитка'
print(user_data)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question