'Python telegram bot doesn't execute actions operations
I made this code to display buttons on /start.
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, ConversationHandler
MENU, HELP = range(2)
def start(bot, update):
keyboard = [
[InlineKeyboardButton('Help', callback_data='help')]
]
# Create initial message:
message = 'Welcome.'
update.message.reply_text(message, reply_markup=InlineKeyboardMarkup(keyboard))
def help(bot, update):
keyboard = [
[InlineKeyboardButton('Leave', callback_data='cancel')]
]
bot.edit_message_text(
text='Help ... help..',
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id,
reply_markup=InlineKeyboardMarkup(keyboard)
)
bot.answer_callback_query(update.callback_query.id, text='')
def cancel(bot, update):
bot.edit_message_text(
text='Bye',
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id,
)
bot.answer_callback_query(update.callback_query.id, text='')
return ConversationHandler.END
# Create the EventHandler and pass it your bot's token.
updater = Updater(token="tokencode", use_context=True)
# Get the dispatcher to register handlers:
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CallbackQueryHandler(help, pattern='help'))
dispatcher.add_handler(CallbackQueryHandler(cancel, pattern='cancel'))
updater.start_polling()
updater.idle()
The code works because if we write some debugging print("hi") in the methods and we execute the bot, we will see that we enter in those methods. Neverthless buttons or messages doesn't show.
The result of the code should be this, but doesn't work like this https://i.stack.imgur.com/c0wyM.gif
I'm on this since many hours, so any help is appreciated.
Solution 1:[1]
You have to switch the parameters in the definition of the functions. In addition to this I'd rather call context what you've call bot.
Here the code that works
import os
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, ConversationHandler
MENU, HELP = range(2)
def start(update, bot):
keyboard = [
[InlineKeyboardButton('Help', callback_data='help')]
]
# Create initial message:
message = 'Welcome.'
update.message.reply_text(message, reply_markup=InlineKeyboardMarkup(keyboard))
def help(update, context):
keyboard = [
[InlineKeyboardButton('Leave', callback_data='cancel')]
]
context.bot.edit_message_text(
text='Help ... help..',
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id,
reply_markup=InlineKeyboardMarkup(keyboard)
)
context.bot.answer_callback_query(update.callback_query.id, text='')
def cancel(update, context):
context.bot.edit_message_text(
text='Bye',
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id,
)
context.bot.answer_callback_query(update.callback_query.id, text='')
return ConversationHandler.END
# Create the EventHandler and pass it your bot's token.
updater = Updater(token="tokencode", use_context=True)
# Get the dispatcher to register handlers:
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CallbackQueryHandler(help, pattern='help'))
dispatcher.add_handler(CallbackQueryHandler(cancel, pattern='cancel'))
updater.start_polling()
updater.idle()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dharman |
