Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import logging
- import time
- from telegram import InlineKeyboardButton, InlineKeyboardMarkup
- from telegram.error import BadRequest
- from telegram.ext import Updater, CommandHandler, CallbackQueryHandler
- BOT_TOKEN = "<BOT_TOKEN>"
- logger = logging.getLogger(__name__)
- logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
- updater = Updater(token=BOT_TOKEN)
- dp = updater.dispatcher
- counter = 10
- def send_inline_keyboard(bot, update):
- chat_id = update.effective_message.chat.id
- text = "Test"
- b1 = InlineKeyboardButton("b1", callback_data="b1")
- keyboard = InlineKeyboardMarkup([[b1]])
- bot.sendMessage(chat_id, text=text, reply_markup=keyboard)
- def callback_handler(bot, update):
- global counter
- orig_chat_id = update.callback_query.message.chat.id
- orig_message_id = update.callback_query.message.message_id
- callback_query_id = update.callback_query.id
- time.sleep(counter)
- text = "Test successful for {} second/s".format(counter)
- try:
- bot.answerCallbackQuery(callback_query_id=callback_query_id, text=text)
- bot.editMessageText(chat_id=orig_chat_id, message_id=orig_message_id, text=text)
- counter += 1
- except BadRequest as e:
- logger.error(e)
- if str(e) == "Query_id_invalid":
- logger.info("Test *not* successful for {} seconds".format(counter))
- dp.add_handler(CommandHandler("test", send_inline_keyboard))
- dp.add_handler(CallbackQueryHandler(callback_handler))
- updater.start_polling()
- logger.info("Bot started as @{}".format(updater.bot.username))
- updater.idle()
Add Comment
Please, Sign In to add comment