Created
March 15, 2018 17:36
-
-
Save d-Rickyy-b/f789c75228bf00f572eec4450ed0d7c9 to your computer and use it in GitHub Desktop.
Code for checking out the timeout of answerCallbackQuery of the Telegram bot API. After ~15 seconds you get the Telegram error QUERY_ID_INVALID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment