K
K
kodbiprog2021-04-17 23:05:06
Python
kodbiprog, 2021-04-17 23:05:06

How to fix a bug in Telebot Python?

import telebot
import random
import sqlite3
from telebot import types

TOKEN = 'token'
bot = telebot.TeleBot(TOKEN)
owner = 1116620584

db = sqlite3.connect('bot.db')
sql = db.cursor()

sql.execute("""CREATE TABLE IF NOT EXISTS users (
  login TEXT,
  password TEXT,
  cash INT
)""")
db.commit()


@bot.message_handler(commands=['start'])
def start_message(message):
        sti = open('img/welcome.webp', 'rb')
        bot.send_sticker(message.chat.id, sti)
        #keyboard
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item1 = types.KeyboardButton(" Описание бота")
        item2 = types.KeyboardButton(" Профиль")
        item3 = types.KeyboardButton(" Обратная связь")

        markup.add(item1,item2,item3)

        msg = bot.send_message( message. chat . id ,  "Добро пожаловать, {0.first_name}! Я - <b> {1.first_name} </b>, отличный Бот для заработка! Желаете ли вы зарегестрироваться?" . format ( message. from_user , bot. get_me ( ) ) , parse_mode='html',reply_markup=markup)
        bot.register_next_step_handler(msg, answer_reg)
def answer_reg(message):
    if message.text == 'Да':
      user_login = bot.send_message(message.chat.id, "Введите логин: ")
      bot.register_next_step_handler(user_login, text_yes_in_reg)
def text_yes_in_reg(message):
    user_password = bot.send_message(message.chat.id, "Введите пароль: ")
    bot.register_next_step_handler(user_login, user_password, reg_processing)
def reg_processing(message):
    sql.execute(f"SELECT login FROM users WHERE login = '{user_login}'")
    if sql.fetchone() is None:
      sql.execute(f"INSERT INTO users VALUES (?,?,?)", (user_login, user_password, 0))
      db.commit()
      print('Ваш аккаунт зарегестрирован!')
    else:
      print('Аккаунт с таким логином уже есть!')

      for value in sql.execute("SELECT * FROM users"):
        print(value)

Gives an error message:
File "D:\python\New Folder\bot20.py", line 42, in text_yes_in_reg
bot.register_next_step_handler(user_login, user_password, reg_processing)
NameError: name 'user_login' is not defined

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yupiter7575, 2021-04-17
@yupiter7575

if message.text == 'Yes':
user_login = bot.send_message(message.chat.id, "Enter your login: ")

Your variable is created under the condition that message.text = 'yes'. if the condition is not met => the variable is not created => an error occurs because this variable is not marked but used

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question