Почему зависает на проверке “содержит ли call.data ‘1.2’»?
import config
import telebot
import ast
import time
from telebot import types
bot = telebot.TeleBot(config.BOT_TOKEN)
stringList = {"Name": "John", "Language": "Python", "API": "pyTelegramBotAPI"}
crossIcon = u"\u274C"
menuList1={ "1.1":"меню 1.1","1.2":"меню 1.2","1.3":"меню 1.3"}
menuList2={ "2.1":"2.1","2.2":"2.2","2.3":"2.3"}
def makeKeyboard():
markup = types.InlineKeyboardMarkup()
for key, value in stringList.items():
markup.add(types.InlineKeyboardButton(text=value,
callback_data="['value', '" + value + "', '" + key + "']"),
types.InlineKeyboardButton(text=crossIcon,
callback_data="['key', '" + key + "']"))
return markup
def makeKeyboard1():
markup = types.InlineKeyboardMarkup()
for key, value in menuList1.items():
markup.add(types.InlineKeyboardButton(text=value,
callback_data="['value', '" + value + "', '" + key + "']"),
types.InlineKeyboardButton(text=crossIcon,
callback_data="['key', '" + key + "']"))
return markup
@bot.message_handler(commands=['menu'])
def handle_command_adminwindow(message):
bot.send_message(chat_id=message.chat.id,
text="Here are the values of stringList",
reply_markup=makeKeyboard(),
parse_mode='HTML')
@bot.callback_query_handler(func=lambda call: True)
def handle_query(call):
print(f"call.data : {call.data} , type : {type(call.data)}")
print(f"ast.literal_eval(call.data) : {ast.literal_eval(call.data)} , type : {type(ast.literal_eval(call.data))}")
valueFromCallBack = ast.literal_eval(call.data)[1]
keyFromCallBack = ast.literal_eval(call.data)[2]
print(valueFromCallBack)
print(keyFromCallBack)
if (keyFromCallBack=="1.2"):
print(f"call.data : {call.data} , type : {type(call.data)}")
print(f"ast.literal_eval(call.data) : {ast.literal_eval(call.data)} , type : {type(ast.literal_eval(call.data))}")
valueFromCallBack = ast.literal_eval(call.data)[1]
keyFromCallBack = ast.literal_eval(call.data)[2]
bot.answer_callback_query(callback_query_id=call.id,
show_alert=True,
text="You WIN! you Clicked " + valueFromCallBack + " and key is " + keyFromCallBack)
if (call.data.startswith("['value'")):
print(f"call.data : {call.data} , type : {type(call.data)}")
print(f"ast.literal_eval(call.data) : {ast.literal_eval(call.data)} , type : {type(ast.literal_eval(call.data))}")
valueFromCallBack = ast.literal_eval(call.data)[1]
keyFromCallBack = ast.literal_eval(call.data)[2]
bot.answer_callback_query(callback_query_id=call.id,
show_alert=True,
text="You Clicked " + valueFromCallBack + " and key is " + keyFromCallBack)
if (call.data.startswith("['key'")):
print(call.data)
#keyFromCallBack = ast.literal_eval(call.data)[1]
#del stringList[keyFromCallBack]
bot.edit_message_text(chat_id=call.message.chat.id,
text="Here are the menu 1",
message_id=call.message.message_id,
reply_markup=makeKeyboard1(),
parse_mode='HTML')
while True:
try:
bot.polling(none_stop=True, interval=1, timeout=0)
except:
time.sleep(10)