Answer the question
In order to leave comments, you need to log in
Why telegram bot freezes?
Why does it hang on checking “does call.data contain '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)
Answer the question
In order to leave comments, you need to log in
To find out why the bot hangs, remove your try ... except. If you don't swallow the errors, they will be in the console. Specifically, you have a list with a length of two elements, you are trying to get the third element from it
here:
keyFromCallBack = ast.literal_eval(call.data)[2]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question