E
E
emperorman2021-10-06 16:31:05
Python
emperorman, 2021-10-06 16:31:05

Menu back button not working in inlinekeyboard on phytone for telegram bot?

Tell me how to register a back button in the minidlna menu, what would return the Package Management menu?

@bot.message_handler(commands=['start'])
def start(message):
  markup = types.ReplyKeyboardMarkup()
  buttonA = types.KeyboardButton('Управление сервером')
  buttonB = types.KeyboardButton('Управление пакетами')
  buttonC = types.KeyboardButton('Загрузка на сервер')

  markup.row(buttonA)
  markup.row(buttonB)
  markup.row(buttonC)

  bot.send_message(message.chat.id, 'Добро пожаловать, Дмитрий!', reply_markup=markup)



@bot.message_handler(content_types=["text"])
def main(message):
    print(message.text)
    if message.text == "Управление сервером":
      if (user_id == message.chat.id):
            markup = types.InlineKeyboardMarkup()
            buttonA = types.InlineKeyboardButton('Перазагрузить сервер', callback_data='a1')
            buttonB = types.InlineKeyboardButton('Выключить сервер', callback_data='b1')
            buttonC = types.InlineKeyboardButton('Вывести время', callback_data='c1')
            markup.row(buttonA)
            markup.row(buttonB)
            markup.row(buttonC)
            bot.send_message(message.chat.id, "Управление сервером", reply_markup=markup)

    if message.text == "Управление пакетами":
       if (user_id == message.chat.id):
            markup = types.InlineKeyboardMarkup()
            buttonA = types.InlineKeyboardButton('VPN', callback_data='vpn')
            buttonB = types.InlineKeyboardButton('Minidlna', callback_data='Minidlna')
            buttonC = types.InlineKeyboardButton('Samba', callback_data='Samba')
            markup.row(buttonA)
            markup.row(buttonB)
            markup.row(buttonC)
            bot.send_message(message.chat.id, "Управление пакетами", reply_markup=markup)


@bot.callback_query_handler(func =lambda call: True)
def handle(call):
    if call.data == 'a1':
       subprocess.call("reboot", shell=True)
    if call.data == 'b1':
       subprocess.call("shutdown", shell=True)
    if call.data == 'c1':
       cmd = subprocess.Popen("uptime", stdout=subprocess.PIPE)
       cmd_out, cmd_err = cmd.communicate()
       bot.send_message(call.message.chat.id, cmd_out.decode("utf-8"))
    if call.data == 'dlna0':
       cmd = subprocess.Popen("service minidlna start", stdout=subprocess.PIPE)
       cmd_out, cmd_err = cmd.communicate()
       bot.send_message(call.message.chat.id, cmd_out.decode("utf-8"))    
    if call.data == 'dlna1':
       cmd = subprocess.Popen("service minidlna stop", stdout=subprocess.PIPE)
       cmd_out, cmd_err = cmd.communicate()
       bot.send_message(call.message.chat.id, cmd_out.decode("utf-8"))
    if call.data == 'dlna2':
       subprocess.call("service minidlna restart", shell=True)
    if call.data == 'dlna3':
       cmd = subprocess.Popen("service minidlna status", stdout=subprocess.PIPE, shell=True)
       cmd_out, cmd_err = cmd.communicate()
       bot.send_message(call.message.chat.id, cmd_out.decode("utf-8"))

    elif call.data == "Minidlna":
        next_menu = types.InlineKeyboardMarkup()
        button0 = types.InlineKeyboardButton(text='Запустить Minidlna', callback_data='dlna0')
        buttonA = types.InlineKeyboardButton(text='Остановить Minidlna', callback_data='dlna1')
        buttonB = types.InlineKeyboardButton(text='Перезапустить Minidlna', callback_data='dlna2')
        buttonC = types.InlineKeyboardButton(text='Статус Minidlna', callback_data='dlna3')
        back = types.InlineKeyboardButton(text='Назад', message_text='Управление пакетами')
        next_menu.add(button0)
        next_menu.add(buttonA)
        next_menu.add(buttonB)
        next_menu.add(buttonC)
        next_menu.add(back)
        bot.edit_message_text('Управление Minidlna', call.message.chat.id, call.message.message_id, reply_markup=next_menu)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anastasia_K, 2021-10-06
@emperorman

try something like this)

@bot.message_handler(commands=['start'])
def start(message):
  markup = types.ReplyKeyboardMarkup()
  buttonA = types.KeyboardButton('Управление сервером')
  buttonB = types.KeyboardButton('Управление пакетами')
  buttonC = types.KeyboardButton('Загрузка на сервер')

  markup.row(buttonA)
  markup.row(buttonB)
  markup.row(buttonC)

  bot.send_message(message.chat.id, 'Добро пожаловать, Дмитрий!', reply_markup=markup)



@bot.message_handler(content_types=["text"])
def main(message):
    print(message.text)
    if message.text == "Управление сервером":
      if (user_id == message.chat.id):
            markup = types.InlineKeyboardMarkup()
            buttonA = types.InlineKeyboardButton('Перазагрузить сервер', callback_data='a1')
            buttonB = types.InlineKeyboardButton('Выключить сервер', callback_data='b1')
            buttonC = types.InlineKeyboardButton('Вывести время', callback_data='c1')
            markup.row(buttonA)
            markup.row(buttonB)
            markup.row(buttonC)
            bot.send_message(message.chat.id, "Управление сервером", reply_markup=markup)

    if message.text == "Управление пакетами":
       if (user_id == message.chat.id):
            markup = types.InlineKeyboardMarkup()
            buttonA = types.InlineKeyboardButton('VPN', callback_data='vpn')
            buttonB = types.InlineKeyboardButton('Minidlna', callback_data='Minidlna')
            buttonC = types.InlineKeyboardButton('Samba', callback_data='Samba')
            markup.row(buttonA)
            markup.row(buttonB)
            markup.row(buttonC)
            bot.send_message(message.chat.id, "Управление пакетами", reply_markup=markup)


@bot.callback_query_handler(func =lambda call: True)
def handle(call):
    if call.data == 'a1':
        subprocess.call("reboot", shell=True)
    elif call.data == 'b1':
        subprocess.call("shutdown", shell=True)
    elif call.data == 'c1':
        cmd = subprocess.Popen("uptime", stdout=subprocess.PIPE)
        cmd_out, cmd_err = cmd.communicate()
        bot.send_message(call.message.chat.id, cmd_out.decode("utf-8"))
    elif call.data == 'dlna0':
        cmd = subprocess.Popen("service minidlna start", stdout=subprocess.PIPE)
        cmd_out, cmd_err = cmd.communicate()
        bot.send_message(call.message.chat.id, cmd_out.decode("utf-8"))    
    elif call.data == 'dlna1':
        cmd = subprocess.Popen("service minidlna stop", stdout=subprocess.PIPE)
        cmd_out, cmd_err = cmd.communicate()
        bot.send_message(call.message.chat.id, cmd_out.decode("utf-8"))
    elif call.data == 'dlna2':
        subprocess.call("service minidlna restart", shell=True)
    elif call.data == 'dlna3':
        cmd = subprocess.Popen("service minidlna status", stdout=subprocess.PIPE, shell=True)
        cmd_out, cmd_err = cmd.communicate()
        bot.send_message(call.message.chat.id, cmd_out.decode("utf-8"))
    elif call.data == 'packets':
        markup = types.InlineKeyboardMarkup()
        buttonA = types.InlineKeyboardButton('VPN', callback_data='vpn')
        buttonB = types.InlineKeyboardButton('Minidlna', callback_data='Minidlna')
        buttonC = types.InlineKeyboardButton('Samba', callback_data='Samba')
        markup.row(buttonA)
        markup.row(buttonB)
        markup.row(buttonC)
        bot.send_message(message.chat.id, "Управление пакетами", reply_markup=markup)
    elif call.data == "Minidlna":
        next_menu = types.InlineKeyboardMarkup()
        button0 = types.InlineKeyboardButton(text='Запустить Minidlna', callback_data='dlna0')
        buttonA = types.InlineKeyboardButton(text='Остановить Minidlna', callback_data='dlna1')
        buttonB = types.InlineKeyboardButton(text='Перезапустить Minidlna', callback_data='dlna2')
        buttonC = types.InlineKeyboardButton(text='Статус Minidlna', callback_data='dlna3')
        back = types.InlineKeyboardButton(text='Назад', callback_data='packets')
        next_menu.add(button0)
        next_menu.add(buttonA)
        next_menu.add(buttonB)
        next_menu.add(buttonC)
        next_menu.add(back)
        bot.edit_message_text('Управление Minidlna', call.message.chat.id, call.message.message_id, reply_markup=next_menu)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question