I
I
Intowi2020-05-25 07:09:29
Python
Intowi, 2020-05-25 07:09:29

How to proceed to password verification?

I decided to make a bot for the cart, ala taskmanager and for checking the password while I entered the test 'Pass' in order to check the functioning, but when choosing to log in and entering any data (correct or not), it goes to 'an unexpected error occurred'. Tell me how to fix. Thanks in advance

import telebot
import config
from telebot import types

bot = telebot.TeleBot(config.TOKEN)


@bot.message_handler(commands=['start'])
def welcome(message):
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    item1 = types.KeyboardButton('Вход в систему')
    item2 = types.KeyboardButton('Выход')

    markup.add(item1, item2)

    bot.send_message(message.chat.id, 'Добро пожаловать', reply_markup=markup)


@bot.message_handler(content_types=['text'])
def autorization(message):
    if message.text == 'Вход в систему':
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        exit = types.KeyboardButton('Выход')
        markup.add(exit)
        bot.send_message(message.chat.id, 'Введите ключ для входа в систему', reply_markup=markup)

        @bot.message_handler(content_types=['text'])
        def login(message):
            if message.text == 'Pass':
                bot.send_message(message.chat.id, 'Вход успешен')
            else:
                bot.send_message(message.chat.id, 'Пароль неверен')

    elif message.text == 'Выход':
        markup = types.ReplyKeyboardRemove()
        bot.send_message(message.chat.id, 'Всего хорошего', reply_markup=markup)
    else:
        bot.send_message(message.chat.id, 'Произошла непредвиденная ошибка')




if __name__ == "__main__":
    bot.polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Lebedinsky, 2020-05-25
@StellandYT

Shalom
You get "An unexpected error has occurred" because the bot does not wait until you enter the password, but it immediately goes through the "login" function.
Instead of "bot.send_message(message.chat.id, 'Enter your login key', reply_markup=markup)" you need to write:

sent = bot.send_message(message.chat.id, 'Введите ключ для входа в систему')
bot.register_next_step_handler(sent, login)

As a result, you will get:
def autorization(message):
    if message.text == 'Вход в систему':
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        exit = types.KeyboardButton('Выход')
        markup.add(exit)
        sent = bot.send_message(message.chat.id, 'Введите ключ для входа в систему',  reply_markup=markup)
        bot.register_next_step_handler(sent, login)

def login(message):
        if message.text == 'Pass':
            bot.send_message(message.chat.id, 'Вход успешен')
        else:
            bot.send_message(message.chat.id, 'Пароль неверен')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question