Суть кода в том, что если пишет user_id из переменной operatorSelect - то его сообщение пересылается пользователю. Так же есть две кнопки - "завершить разговор" и "переключить оператора". Так вот, после переключения оператора, старый в БД отображается как not_in_dialog, новому соответственно присваивается in_dialog и казалось бы всё должно работать. Но нет. Хандлер всё ещё видит переменную operatorSelect с старым айдишником, хотя ему уже присвоили новый. Почему так происходит? Как передать новое значение оператора в хандлер? Пробовал принудительно сбрасывать хандлер при помощи bot.clear_step_handler_by_chat_id(call.message.chat.id) - не помогло.
@bot.callback_query_handler(func=lambda call: call.data.startswith('newOperator.'))
def newOperator(call, operatorSelect=operatorSelect):
print('operatorOLD: ', operatorSelect)
mydb = mysql.connector.connect(host = hostBot, user = userBot, password = passwordBot, database = databaseBot)
cursor = mydb.cursor()
cursor.execute("UPDATE operators SET operatorState = %s WHERE user_id = %s", ("not_in_dialog", operatorSelect))
mydb.commit()
cursor.close()
mydb.close()
newOperatorName = call.data.replace('newOperator.', '')
print('newOperatorName: ', newOperatorName)
mydb = mysql.connector.connect(host = hostBot, user = userBot, password = passwordBot, database = databaseBot)
cursor = mydb.cursor()
cursor.execute("SELECT user_id FROM operators WHERE operatorVariable = %s", (newOperatorName,))
result = cursor.fetchone()
print('result из базы: ', result)
operatorSelect = result[0]
print('operatorNEW: ', operatorSelect)
mydb = mysql.connector.connect(host = hostBot, user = userBot, password = passwordBot, database = databaseBot)
cursor = mydb.cursor()
cursor.execute("UPDATE operators SET operatorState = %s WHERE user_id = %s", ("in_dialog", operatorSelect))
mydb.commit()
cursor.close()
mydb.close()
@bot.message_handler(func=lambda message: message.from_user.id == operatorSelect)
def dialog_message_operator(message):
print('вызвана def dialog_message_operator')
mydb = mysql.connector.connect(host = hostBot, user = userBot, password = passwordBot, database = databaseBot)
cursor = mydb.cursor()
cursor.execute("SELECT operatorState FROM operators WHERE user_id = %s", (operatorSelect,))
result = cursor.fetchone()
if result is not None:
operatorSelectState = result[0]
print('operatorSelectState: ', operatorSelectState)
cursor.execute("SELECT userState FROM users WHERE user_id = %s", (user_help_id,))
result = cursor.fetchone()
if result is not None:
user_help_id_state = result[0]
print('user_help_id_state: ', user_help_id_state)
cursor.close()
mydb.close()
if message.text == text56:
bot.send_message(user_help_id, text52, reply_markup=startBtn)
bot.send_message(operatorSelect, text58, reply_markup=startBtn)
mydb = mysql.connector.connect(host = hostBot, user = userBot, password = passwordBot, database = databaseBot)
cursor = mydb.cursor()
cursor.execute("UPDATE operators SET operatorState = %s WHERE user_id = %s", ("not_in_dialog", operatorSelect))
mydb.commit()
cursor.execute("UPDATE users SET userState = %s WHERE user_id = %s", ("end_dialog", user_help_id))
mydb.commit()
mydb.close()
elif message.text == text62:
mydb = mysql.connector.connect(host = hostBot, user = userBot, password = passwordBot, database = databaseBot)
cursor = mydb.cursor()
cursor.execute("SELECT nameOperator FROM operators WHERE operatorVariable = 'operator1'")
result = cursor.fetchone()
if result is not None:
nameOperator1 = result[0]
cursor.execute("SELECT nameOperator FROM operators WHERE operatorVariable = 'operator2'")
result = cursor.fetchone()
if result is not None:
nameOperator2 = result[0]
cursor.execute("SELECT nameOperator FROM operators WHERE operatorVariable = 'operator3'")
result = cursor.fetchone()
if result is not None:
nameOperator3 = result[0]
cursor.execute("SELECT nameOperator FROM operators WHERE operatorVariable = 'operator4'")
result = cursor.fetchone()
if result is not None:
nameOperator4 = result[0]
cursor.execute("SELECT nameOperator FROM operators WHERE operatorVariable = 'operator5'")
result = cursor.fetchone()
if result is not None:
nameOperator5 = result[0]
mydb.close()
showOperatorsBtn = types.InlineKeyboardMarkup()
if nameOperator1 is not None:
oper1Btn = types.InlineKeyboardButton(nameOperator1, callback_data='newOperator.operator1')
showOperatorsBtn.add(oper1Btn)
if nameOperator2 is not None:
oper2Btn = types.InlineKeyboardButton(nameOperator2, callback_data='newOperator.operator2')
showOperatorsBtn.add(oper2Btn)
if nameOperator3 is not None:
oper3Btn = types.InlineKeyboardButton(nameOperator3, callback_data='newOperator.operator3')
showOperatorsBtn.add(oper3Btn)
if nameOperator4 is not None:
oper4Btn = types.InlineKeyboardButton(nameOperator4, callback_data='newOperator.operator4')
showOperatorsBtn.add(oper4Btn)
if nameOperator5 is not None:
oper5Btn = types.InlineKeyboardButton(nameOperator5, callback_data='newOperator.operator5')
showOperatorsBtn.add(oper5Btn)
bot.send_message(operatorSelect, text63, reply_markup=showOperatorsBtn)
elif user_help_id_state and operatorSelectState == 'in_dialog':
operator_text = message.text
bot.send_message(user_help_id, operator_text)
else:
print('else ниче нет')