How to create a back button in telebot with python

225 views Asked by At

My source is telebot lib and I am writing in Python 3.11.5

I want to have a Go Back button when I click on the support button and be able to go back the previous buttons

contact_sup_markup = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True)
contact_sup_button = telebot.types.KeyboardButton('contact support')
contact_sup_markup.add(contact_sup_button)

@bot.message_handler(func= lambda m: m.text == 'contact support')      
def contact_support(message):  
    bot.send_message(chat_id= message.chat.id, text= f'<b>You can now texted support admin bot</b>', parse_mode="HTML")
    bot.set_state(user_id= message.from_user.id, state= Support.text, chat_id= message.chat.id)
    

I want to have a button called back after clicking the Contact Support button, I need how to write the code

1

There are 1 answers

2
Pavel Nekrasov On BEST ANSWER

To add a "Go Back" button after clicking the "Contact Support" button, you can modify your code as follows:

import telebot
from telebot.types import ReplyKeyboardMarkup, KeyboardButton

# Create a custom keyboard markup
contact_sup_markup = ReplyKeyboardMarkup(resize_keyboard=True)
contact_sup_button = KeyboardButton('Contact Support')
contact_sup_markup.row(contact_sup_button)

# Create a custom keyboard markup for the "Go Back" button
go_back_markup = ReplyKeyboardMarkup(resize_keyboard=True)
go_back_button = KeyboardButton('Go Back')
go_back_markup.row(go_back_button)

@bot.message_handler(func=lambda m: m.text == 'Contact Support')
def contact_support(message):
    # Send a message to the user
    bot.send_message(chat_id=message.chat.id, text='<b>You can now text the support admin bot.</b>', parse_mode="HTML", reply_markup=go_back_markup)
    # Store the current state in the user's session
    bot.set_state(user_id=message.from_user.id, state='Support', chat_id=message.chat.id)

@bot.message_handler(func=lambda m: m.text == 'Go Back')
def go_back(message):
    # Send a message to the user
    bot.send_message(chat_id=message.chat.id, text='You have gone back to the previous menu.', reply_markup=contact_sup_markup)
    # Clear the current state from the user's session
    bot.clear_state(user_id=message.from_user.id, chat_id=message.chat.id)

In this code, we first create a custom keyboard markup go_back_markup with a "Go Back" button row. We assign this markup to the reply_markup parameter when sending the message to the user after clicking the "Contact Support" button.

We also add a new message handler for the "Go Back" button. When the user clicks the "Go Back" button, it will trigger this message handler. In the handler, we send a message to the user and provide the contact_sup_markup as the reply_markup parameter to display the original menu with the "Contact Support" button. We also clear the current state from the user's session to go back to the previous state.

With this code, when the user clicks the "Contact Support" button, they will be presented with the option to go back by clicking the "Go Back" button.