I am using InlineKeyboard while making a telegram bot using python-telegram-bot. The problem is that the query handler function, button(), is not being called on clicking any button in the inline keyboard.
Here is the code
from telegram import *
from telegram.ext import *
print('bot executed')
token = <BOT_TOKEN>
async def send_message_in_channel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
global new_data
await context.bot.send_message(chat_id=<CHANNEL_USERNAME>, text="new_data")
async def message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await context.bot.delete_message(update.effective_chat.id, message_id=update.message.id)
async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
query = update.callback_query
await query.answer()
print('button reached')
if query.data == 'True':
print("choose yes")
await send_message_in_channel(update, context)
elif query.data == 'False':
print("choose no")
async def option(new_data) -> None:
bot = Bot(token=token)
options = [
[
InlineKeyboardButton('Yes, Post It', callback_data="True"),
InlineKeyboardButton('No, Don\'t Post', callback_data="False")
],
]
reply_markup = InlineKeyboardMarkup(options)
await bot.send_message(chat_id=<MY_OWN_CHAT_ID>, text=new_data, reply_markup=reply_markup, disable_web_page_preview=True)
if __name__ == '__main__':
new_data = ''
application = ApplicationBuilder().token(token).build()
application.add_handler(CallbackQueryHandler(button))
application.add_handler(MessageHandler(
filters.TEXT & (~filters.COMMAND), message))
application.run_polling()
The option() function is being called from a external function with text as parameter 'new_data'.
I didn't face this issue while calling option from bot itself like using CommandHandler('start', option) and replacing new_data with 'update, context'. But that is not what I want. Since on calling option() from outside, I don't have any context or update object.
I also tried changing context of button() from ContextTypes.DEFAULT_TYPE to CallbackContext with no luck.