I have a problem when calling the function textMessage the prompt for entering text does not work, but immediately goes to the end of the function, what is the problem.
Here is the function code
def textMessage(bot, update): k = str(update.message.text) #Вот здесь все нормально if k in final_user : cursor = cnx.cursor() query = "SELECT password FROM student where email='%s'"%(k) cursor.execute(query) result = cursor.fetchall() s1 = str(result) s2 = s1.replace("'", "") s2 = s2.replace("(", "") s2 = s2.replace(")", "") s2 = s2.replace(",", "") bot.send_message(chat_id=update.message.chat_id, text='Password') password = str(update.message.text) #А вот здесь пропускает ввод if s2 == password: bot.send_message(chat_id=update.message.chat_id, text="Welcome!") else: bot.send_message(chat_id=update.message.chat_id, text="Invalid password!") else: bot.send_message(chat_id=update.message.chat_id, text="Invalid login!")
All bot code
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters import pymysql import apiai, json updater = Updater(token='****') # Токен API к Telegram dispatcher = updater.dispatcher cnx = pymysql.connect(user='****', password='****', host='****', database='****') def startCommand(bot, update): bot.send_message(chat_id=update.message.chat_id, text='Hello, I`m bot \n' 'Sing in please\n' '/sign_in') bot.send_message(chat_id=update.message.chat_id, text='Login') cursor = cnx.cursor() query = "SELECT email FROM student" cursor.execute(query) result = cursor.fetchall() final_result = [list(i) for i in result] final_user = [] for i in range(len(final_result)-1): final_user.append(final_result[i][0]) print (final_user[1]) final_user = [] for i in range(len(final_result) - 1): final_user.append(final_result[i][0]) def textMessage(bot, update): k = str(update.message.text) #Вот здесь все нормально if k in final_user : cursor = cnx.cursor() query = "SELECT password FROM student where email='%s'"%(k) cursor.execute(query) result = cursor.fetchall() s1 = str(result) s2 = s1.replace("'", "") s2 = s2.replace("(", "") s2 = s2.replace(")", "") s2 = s2.replace(",", "") bot.send_message(chat_id=update.message.chat_id, text='Password') password = str(update.message.text) #А вот здесь пропускает ввод if s2 == password: bot.send_message(chat_id=update.message.chat_id, text="Welcome!") else: bot.send_message(chat_id=update.message.chat_id, text="Invalid password!") else: bot.send_message(chat_id=update.message.chat_id, text="Invalid login!") # Обработка команд def helpCommand(bot, update): bot.send_message(chat_id=update.message.chat_id, text='') # Хендлеры start_command_handler = CommandHandler('start', startCommand) help_command_handler = CommandHandler('help', helpCommand) text_message_handler = MessageHandler(Filters.text, textMessage) F_message_handler = MessageHandler(Filters.text, textMessage) # Добавляем хендлеры в диспетчер dispatcher.add_handler(start_command_handler) dispatcher.add_handler(help_command_handler) dispatcher.add_handler(text_message_handler) dispatcher.add_handler(F_message_handler) # Начинаем поиск обновлений updater.start_polling(clean=True) # Останавливаем бота, если были нажаты Ctrl + C updater.idle()
if k in final_user:
:? - gil9redresult
- you can simply call fetchone and request the first element from it - password:result = cursor.execute(query).fetchone()[0]
and theresult
will be your password, and thesereplace
look like a terrible crutch and mb because of them something is not working. Kst, you could add print'y to each line and track where it went wrong, as you expected - gil9redКст, вы ведь могли на каждую строку добавить print'ы и отследить где пошло не так, как вы ожидали
- gil9red