Guest User

Untitled

a guest
Mar 24th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. import logging
  2. import time
  3.  
  4. from telegram import InlineKeyboardButton, InlineKeyboardMarkup
  5. from telegram.error import BadRequest
  6. from telegram.ext import Updater, CommandHandler, CallbackQueryHandler
  7.  
  8. BOT_TOKEN = "<BOT_TOKEN>"
  9.  
  10. logger = logging.getLogger(__name__)
  11. logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
  12.  
  13. updater = Updater(token=BOT_TOKEN)
  14. dp = updater.dispatcher
  15. counter = 10
  16.  
  17.  
  18. def send_inline_keyboard(bot, update):
  19. chat_id = update.effective_message.chat.id
  20. text = "Test"
  21. b1 = InlineKeyboardButton("b1", callback_data="b1")
  22. keyboard = InlineKeyboardMarkup([[b1]])
  23.  
  24. bot.sendMessage(chat_id, text=text, reply_markup=keyboard)
  25.  
  26.  
  27. def callback_handler(bot, update):
  28. global counter
  29. orig_chat_id = update.callback_query.message.chat.id
  30. orig_message_id = update.callback_query.message.message_id
  31. callback_query_id = update.callback_query.id
  32.  
  33. time.sleep(counter)
  34.  
  35. text = "Test successful for {} second/s".format(counter)
  36.  
  37. try:
  38. bot.answerCallbackQuery(callback_query_id=callback_query_id, text=text)
  39. bot.editMessageText(chat_id=orig_chat_id, message_id=orig_message_id, text=text)
  40. counter += 1
  41. except BadRequest as e:
  42. logger.error(e)
  43. if str(e) == "Query_id_invalid":
  44. logger.info("Test *not* successful for {} seconds".format(counter))
  45.  
  46.  
  47. dp.add_handler(CommandHandler("test", send_inline_keyboard))
  48. dp.add_handler(CallbackQueryHandler(callback_handler))
  49.  
  50. updater.start_polling()
  51. logger.info("Bot started as @{}".format(updater.bot.username))
  52. updater.idle()
Add Comment
Please, Sign In to add comment