Answer the question
In order to leave comments, you need to log in
Why doesn't handler catch callback in telebot?
I provide the code, the buttons are called inside the function with the TEXT handler. I understand that somewhere there is a jamb but I can not catch up.
def handle_text(message):
userid = str(message.chat.id)
user_first_name = str(message.chat.first_name)
user_last_name = str(message.chat.last_name)
user_username = str(message.chat.username)
if userid in userslist:
with open(logfilename, 'a') as file_object:
timenow = datetime.now()
file_object.write(f'"{timenow}","{userid}","{user_username}","{user_first_name} {user_last_name}","request","{message.text}","A"\n')
if len(message.text) >= 5:
result = []
markup=types.ReplyKeyboardMarkup(resize_keyboard=True)
for s in pdfs:
if s.casefold().__contains__(message.text.casefold()):
# if s.__contains__(message.text):
result.append(s)
if len(result) == 1:
for schema in result:
file_to_send = open(schema, 'rb')
bot.send_document(message.chat.id, file_to_send)
file_to_send.close()
a = types.ReplyKeyboardRemove()
bot.send_message(message.chat.id, 'Чертеж найден, поищем еще?', reply_markup=a )
timenow = datetime.now()
with open(logfilename, 'a') as file_object:
file_object.write(f'"{timenow}","{userid}","{user_username}","{user_first_name} {user_last_name}","response","{schema}","A"\n')
elif len(result) == 0:
a = types.ReplyKeyboardRemove()
bot.send_message(message.chat.id, 'Чертеж НЕ НАЙДЕН', reply_markup=a )
elif len(result) > 15:
for schema in result[:15]:
dirname, filename = os.path.split(schema)
markup.add(types.InlineKeyboardButton(text=filename, callback_data=schema))
bot.send_message(message.chat.id, 'Показаны первые 15 результатов. Если искомого чертежа нет - уточните поиск:', reply_markup=markup )
else:
for schema in result:
dirname, filename = os.path.split(schema)
markup.add(types.InlineKeyboardButton(text=filename, callback_data=schema))
# markup.add(types.KeyboardButtonUrl(text=filename, url=schema))
bot.send_message(message.chat.id, f'Найдено {len(result)} Чертежей. Выберите чертеж:', reply_markup=markup )
else:
bot.send_message(message.chat.id, 'Нужно вводить 5+ символов' )
else:
bot.send_message(message.chat.id, 'Ты не имеешь доступа!' )
timenow = datetime.now()
with open(logfilename, 'a') as file_object:
file_object.write(f'"{timenow}","{userid}","{user_username}","{user_first_name} {user_last_name}","request","{message.text}","NA"\n')
#Obrabotka callbackov
@bot.callback_query_handler(func=lambda call: True)
def callback_worker(call):
"""Функция обработки кнопок"""
if call.data:
filetosend = call.data
file_to_send = open(filetosend, 'rb')
bot.send_document(call.chat.id, file_to_send)
file_to_send.close()
# Запускаем бота
bot.polling(none_stop=True, interval=0)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question