D
D
Danila2019-12-16 23:49:05
Python
Danila, 2019-12-16 23:49:05

How to make two CallBack Handlers work in Telegram Bot in Python?

Good afternoon. I have two inlne keyboards in my telegram bot.
Both work separately in different files, or if one of them is replaced.
I tried to move one keyboard to another file, but the result is the same.
I understand that
@bot.callback_query_handler(func=lambda c: True)
this handler, which is in the code above, is trying to process the keyboard, which is in the code below.
Please help me figure it out. The keyboard code is pretty standard, for example the first one:

#Курсы валют
@bot.message_handler(regexp="Курсы валют")
def value_message(message):
    keyboardV = types.InlineKeyboardMarkup()
    kbv1 = types.InlineKeyboardButton(text="Доллар", callback_data="USD")
    kbv2 = types.InlineKeyboardButton(text="Евро", callback_data="EUR")
    kbv3 = types.InlineKeyboardButton(text="Фунт", callback_data="GBP")
    keyboardV.add(kbv1, kbv2, kbv3)
    bot.send_message(message.chat.id, "Выберите валюту: ", reply_markup=keyboardV)

Second:
@bot.message_handler(regexp="Новости")
def selectCounrty(message):
    # Клавиатура выбора стран
    keyboard = types.InlineKeyboardMarkup()
    kb1 = types.InlineKeyboardButton(text="Россия", callback_data="country1")
    kb2 = types.InlineKeyboardButton(text="Германия", callback_data="country2")
    keyboard.add(kb1, kb2)
    bot.send_message(message.chat.id, "Список стран: ", reply_markup=keyboard)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
domoarigato, 2021-08-24
@domoarigato

@bot.callback_query_handler(func=lambda call: True)

Instead of this form, you can do:
@bot.callback_query_handler(func=lambda call: call.data == "Data")
def callHandler1(call: CallbackQuery):
    #Code

@bot.callback_query_handler(func=lambda call: call.data == "Data2")
def callHandler2(call: CallbackQuery):
    #Code 2

You can use another option - do inside one handler if call.data == "Value": func1(call), and so for each value, or in the if itself and write code.
Hope it helped.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question