P
P
Privet1232022-04-02 16:29:13
Python
Privet123, 2022-04-02 16:29:13

How to make two answers to a bot question using the pyTelegramBotAPI library?

I want to do like this:

-/start
-Придумайте логин
-user1
-Придумайте пароль
-qwerty123
-Вы успешно зарегистрировались!


I can’t make it so that after I write the login, the bot asks for a password and I can’t write it into variables. How to do it?

Here is the code:
import telebot
import config
import sqlite3

bot = telebot.TeleBot(config.TOKEN)

db = sqlite3.connect('bot_db')
cursor = db.cursor()

cursor.execute("""CREATE TABLE IF NOT EXISTS users(
    login text,
    password text
  )""")

db.commit()

@bot.message_handler(commands=['start'])
# тут должен быть код, который я не понял как делать

def reg(message):
  cursor.execute(f"SELECT login FROM users WHEN login = '{login(message)}'")
  if cursor.fetchone() is None:
    cursor.execute("INSERT INTO users VALUES (?, ?)", (login(message), password(message)))
    db.commit()

    bot.send_message(message.chat.id, 'Вы успешно зарегистрированы!')
  else:
    bot.send_message(message.chat.id, 'Такой пользователь уже существует!')

bot.polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Nesterov, 2022-04-02
@AlexNest

register_next_step_handler

R
Romeo558, 2022-04-02
@Romeo558

import telebot
import config
import sqlite3

bot = telebot.TeleBot(config.TOKEN)

db = sqlite3.connect('bot_db')
cursor = db.cursor()

cursor.execute("""CREATE TABLE IF NOT EXISTS users(
    login text,
    password text
  )""")

db.commit()

@bot.message_handler(commands=['start'])
    msg = bot.send_message(message.chat.id, "Ок,  придумайте логин") #Что то типа заготовки.
    bot.register_next_step_handler(msg, reg)

def reg(message):
   login = message.text
   msg = bot.send_message(message.chat.id, "Придумайте пароль")
   bot.register_next_step_handler(msg, pass_reg)
#  cursor.execute(f"SELECT login FROM users WHEN login = '{login(message)}'")
#  if cursor.fetchone() is None:
#    cursor.execute("INSERT INTO users VALUES (?, ?)", (login(message), password(message)))
#    db.commit()
#
#    bot.send_message(message.chat.id, 'Вы успешно зарегистрированы!')
#  else:
 #   bot.send_message(message.chat.id, 'Такой пользователь уже существует!')
def pass_reg(message):
   password = message.text
   bot.send_message(message.chat.id, "Вы успешно авторизованы!")
bot.polling(none_stop=True)

That is, it was necessary to add register_next_step_handler and add an initial message

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question