A
A
Alpharius_Prog2022-04-03 17:57:43
Python
Alpharius_Prog, 2022-04-03 17:57:43

How to switch functions in python?

Hello. It is necessary to achieve switching of functions after the if-elif-else chain. Code example:

@bot.message_handler(content_types=['text'])
def EN(message):
    if (message.text == "EN"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item11 = types.KeyboardButton("Choose category")
        item12 = types.KeyboardButton("FAQ")
        item13 = types.KeyboardButton("About")
        item14 = types.KeyboardButton("Payment instructions")
        item15 = types.KeyboardButton("Back")
        markup.add(item11,item12,item13,item14,item15)
        bot.send_message(message.chat.id,text = "EN", reply_markup=markup)
    elif (message.text == "Choose category"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item111 = types.KeyboardButton("History")
        item112 = types.KeyboardButton("Science")
        item113 = types.KeyboardButton("Arts")
        item114 = types.KeyboardButton("Back")
        markup.add(item111,item112,item113,item114)
        bot.send_message(message.chat.id,text = "Choose category", reply_markup=markup)
    elif (message.text == "FAQ"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item122 = types.KeyboardButton("Back")
        markup.add(item122)
        text = """Text about TOP-7 questions"""
        bot.send_message(message.chat.id,text,parse_mode = 'MarkdownV2',reply_markup=markup)
    elif (message.text == "Payment instructions"):
        markup = types.InlineKeyboardMarkup()
        item141 = types.InlineKeyboardButton("Binance", url = 'https://www.binance.com/en/support/faq/c-2?navId=2')
        item142 = types.InlineKeyboardButton("BlockChain",url = 'https://support.blockchain.com/hc/en-us/sections/4517566823060-Deposits-and-Withdrawals')
        item143 = types.InlineKeyboardButton("TrustWallet",url = 'https://community.trustwallet.com/t/how-to-create-a-payment-request/23332')
        markup.add(item141,item142,item143)
        bot.send_message(message.chat.id,text = "Choose your Wallet", reply_markup=markup)
    elif (message.text == "Back"):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item11 = types.KeyboardButton("Choose category")
        item12 = types.KeyboardButton("FAQ")
        item13 = types.KeyboardButton("About")
        item14 = types.KeyboardButton("Payment instructions")
        item15 = types.KeyboardButton("Back")
        markup.add(item11,item12,item13,item14,item15)
        bot.send_message(message.chat.id,text = "You returned to the menu", reply_markup=markup)
    else:
        return RU

(If anything - after else is the return of the argument of the next function).
How to achieve function switching?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wispik, 2022-04-03
@Wispik

if I understand the problem correctly, then
create a dictionary

lang_dict = {
  "EN" : {
    "button1": "button1",
    "button2": "button2"
  }, 
  "RU" : {
    "button1": "кнопка1",
    "button2": "кнопка2"
  }
}

and take from it the names of the buttons depending on the language
lang_dict["RU"]["button1"]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question