telegram-bot to build like a menu builder bot to manage some documents for collage, and i wanted to add back button to give me the pervious menu.. so i made a stack class and imported it to pop last menu but it seems calling the functions does not work in the library (the only one i used so far)
from telegram import *
from telegram.ext import *
from stack import Stack
from env import token
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("httpx").setLevel(logging.DEBUG)
logger = logging.getLogger(__name__)
stack = Stack()
def keyboard(*buttons: list):
key = ReplyKeyboardMarkup([x for x in buttons], one_time_keyboard=True)
return key
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
starter_message = await context.bot.sendMessage(
chat_id=update.effective_chat.id,
text=f"Hello user, how may i help you",
reply_markup=keyboard(["Button1"], ["Button2"], ["Button3"])
)
stack.push(starter_message)
async def replay_keys(update: Update, context: ContextTypes.DEFAULT_TYPE):
if update.message.text == "Button1":
y1 = await update.message.reply_text(text=update.message.text,
reply_markup=keyboard(["innerButton1"], ["innerButton2"], ["Back"]))
stack.push(y1)
elif update.message.text == "Button2":
y2 = await update.message.reply_text(text=update.message.text,
reply_markup=keyboard(
["innerButton1"],
["innerButton2"],
["innerButton3"],
["Back"]))
stack.push(y2)
elif update.message.text == "Button3":
y3 = await update.message.reply_text(text=update.message.text,
reply_markup=keyboard(["innerButton"],["Back"]))
stack.push(y3)
elif update.message.text == "Back":
stack.pop()
if __name__ == "__main__":
application = ApplicationBuilder().token(f"{token}").build()
start_Handler = CommandHandler("start", start)
application.add_handler(start_Handler)
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, replay_keys))
application.run_polling(allowed_updates=Update.ALL_TYPES)