I am creating my first ever Telegram Bot on Python. For now, I managed to get an inline menu, but I cannot understand how to get the information from the last button (aka the text on the button that was pushed) to use it in the query for google doc. For example, if they check Button 3, I want to somehow access text 'Button 3' and use it in the query (data[data['type']=='Button 3']). Here is the code to help with understanding:
########################## Open connection with Google Sheets #############################
client = gspread.authorize(creds)
sheet = client.open('YodissiDay').sheet1
############################### Bot ############################################
def start(bot, update):
bot.message.reply_text(main_menu_message(), reply_markup=main_menu_keyboard())
def main_menu(bot, update):
bot.callback_query.message.edit_text(main_menu_message(), reply_markup=main_menu_keyboard())
def first_menu(bot, update):
bot.callback_query.message.edit_text(first_menu_message(),
reply_markup=first_menu_keyboard())
def second_menu(bot, update):
bot.callback_query.message.edit_text(second_menu_message(),
reply_markup=second_menu_keyboard())
def third_menu(bot, update):
bot.callback_query.message.edit_text(second_menu_message(),
reply_markup=second_menu_keyboard())
def first_submenu(bot, update):
bot.callback_query.message.edit_text(first_submenu_message(),
reply_markup=first_submenu_keyboard(bot, update))
def second_submenu(bot, update):
pass
def error(update, context):
print(f'Update {update} caused error {context.error}')
############################ Keyboards #########################################
def main_menu_keyboard():
keyboard = [[InlineKeyboardButton('First Choice', callback_data='m1')],
[InlineKeyboardButton('Second Choice', callback_data='m2')],
[InlineKeyboardButton('Third Choice', callback_data='m3')]]
return InlineKeyboardMarkup(keyboard)
def first_menu_keyboard():
keyboard = [[InlineKeyboardButton('Beauty', callback_data='Beauty')],
[InlineKeyboardButton('One', callback_data='One')],
[InlineKeyboardButton('Two', callback_data='Two')],
[InlineKeyboardButton('Three', callback_data='Three')],
[InlineKeyboardButton('Main menu', callback_data='main')]]
return InlineKeyboardMarkup(keyboard)
def second_menu_keyboard():
pass
def first_submenu_keyboard(bot, update):
data = gspread_dataframe.get_as_dataframe(sheet)
data = data.astype({'Taken' : 'float64'})
data = data[data['Taken']==0]
gift_type = update.callback_query.data
gift_ideas = data[data['Type']==gift_type]
keyboard = []
for row in range(0,gift_ideas.shape[0]):
keyboard[row]=InlineKeyboardButton(text=gift_ideas['Name'][row], url=gift_ideas['Link'][row])
keyboard[gift_ideas.shape[0]+1]=InlineKeyboardButton('Main menu', callback_data='main')
return InlineKeyboardMarkup(keyboard)
############################# Messages #########################################
def main_menu_message():
return 'Hello 1:'
def first_menu_message():
return 'Hello 1_1:'
def second_menu_message():
return 'Hello 1_2:'
def third_menu_message():
return 'Hello 1_3:'
def first_submenu_message():
return 'Hello 1_1_1:'
############################# Handlers #########################################
#
Create bot object:
bot = telegram.Bot(token=BOT_KEY)
updater = Updater(BOT_KEY, use_context=True)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(main_menu, pattern='main'))
updater.dispatcher.add_handler(CallbackQueryHandler(first_menu, pattern='m1'))
updater.dispatcher.add_handler(CallbackQueryHandler(second_menu, pattern='m2'))
updater.dispatcher.add_handler(CallbackQueryHandler(third_menu, pattern='m3'))
updater.dispatcher.add_handler(CallbackQueryHandler(first_submenu, pattern='Beauty'))
updater.dispatcher.add_error_handler(error)
# Start polling:
updater.start_polling()
updater.idle()```
All other recommendations/advices are also welcome! Keep in mind, that I try it step by step and for now I want to see how this 'Beauty' button would work! Other buttons lead to nowhere for now