I'm sorry because I'm still a noob and still learning.
This is my code :
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import CallbackQueryHandler
import telegram.ext
import mysql.connector
mydb = mysql.connector.connect(...)
sql = mydb.cursor()
with open('token file', 'r') as f:
TOKEN = f.read()
def start(update, context):
try:
update.message.reply_text("Welcome to Pet Regist",reply_markup=menu_key())
except Exception:
update.callback_query.message.edit_text("Welcome to Pet Regist",reply_markup=menu_key())
def search_cat (update,context):
update.callback_query.message.edit_text("Please enter your cat's name :")
def handler_message1(update,context):
texts = update.message.text.split(' ')
cat_name = texts[0]
sql.execute("select cat_type from cat_table where cat_name='{}'".format(cat_name))
findings = sql.fetchall()
try:
findings1 = findings[0]
cat_type = findings1[0]
update.message.reply_text(f"""
Your cat registered. with type {cat_type}""",reply_markup=cat_key())
except IndexError: #First IndexError
update.message.reply_text(f"""
Your cat unregistered.""",reply_markup=cat_key())
disp.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.text, handler_message1))
def search_dog (update,context):
update.callback_query.message.edit_text("Please enter your dog's name :")
def handler_message1(update,context):
texts = update.message.text.split(' ')
dog_name = texts[0]
sql.execute("select dog_type from dog_table where dog_name='{}'".format(dog_name))
findings = sql.fetchall()
try:
findings1 = findings[0]
dog_type = findings1[0]
update.message.reply_text(f"""
Your dog registered. with type {dog_type}""",reply_markup=dog_key())
except IndexError: #second IndexError
update.message.reply_text(f"""
Your dog unregistered.""",reply_markup=dog_key())
disp.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.text, handler_message1))
def menu_key():
keyboard = [[InlineKeyboardButton('Cat', callback_data='cat_key')],
[InlineKeyboardButton('Dog', callback_data='dog_key')]]
return InlineKeyboardMarkup(keyboard)
def cat_key():
keyboard = [[InlineKeyboardButton('Menu',callback_data='menu_key')]]
return InlineKeyboardMarkup(keyboard)
def dog_key():
keyboard = [[InlineKeyboardButton('Menu',callback_data='menu_key')]]
return InlineKeyboardMarkup(keyboard)
updater = telegram.ext.Updater(TOKEN, use_context=True)
disp = updater.dispatcher
disp.add_handler(telegram.ext.CommandHandler("start", start))
updater.dispatcher.add_handler(CallbackQueryHandler(start, pattern='menu_key'))
updater.dispatcher.add_handler(CallbackQueryHandler(search_cat, pattern='cat_key'))
updater.dispatcher.add_handler(CallbackQueryHandler(search_dog, pattern='dog_key'))
updater.start_polling()
updater.idle()
My problem is when I try to run it on Telegram, at the first try, I chose the Cat option, input some data, and the bot replied it, then at the second try, I went back to the main menu and chose the Dog option and input some data, but the bot is still with the Cat function reply. Is there any mistake in my code and how to solve it so I can go to the second function reply without restarting the bot?