Skip to content

Instantly share code, notes, and snippets.

@Poolitzer
Created December 8, 2019 08:13
Show Gist options
  • Save Poolitzer/8a062c55e4a8f204ebaddcff91980e4e to your computer and use it in GitHub Desktop.
Save Poolitzer/8a062c55e4a8f204ebaddcff91980e4e to your computer and use it in GitHub Desktop.
no
import logging
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
def start_command(update, context):
button = [[InlineKeyboardButton("start", callback_data="numbers")]]
update.message.reply_text("Hey, lets do that", reply_markup=InlineKeyboardMarkup(button))
def first_page(update, context):
query = update.callback_query
# our end buttons list
buttons = []
# this will get appended to the list above and then reset
buttons_row = []
# we will need this to know when to append the list above. we set it to 6
x = 0
# 58 because it will start with 1, so we stop with 57
for number in range(1, 58):
buttons_row.append(InlineKeyboardButton(number, callback_data=str(number)))
x += 1
if x == 6:
# this means, after 6 iterations, we append our row and then set everything back
buttons.append(buttons_row)
buttons_row = []
x = 0
# failsafe, in case we have numbers left. Probably unnecessary, but its way too late for me to figure that out
if buttons_row:
buttons.append(buttons_row)
back_button = InlineKeyboardButton("cancel", callback_data="cancel")
next_button = InlineKeyboardButton("next", callback_data="next")
menu_buttons = [back_button, next_button]
buttons.append(menu_buttons)
query.edit_message_text("Numbers", reply_markup=InlineKeyboardMarkup(buttons))
def fallback(update, context):
query = update.callback_query
button = [[InlineKeyboardButton("start", callback_data="numbers")]]
query.edit_message_text("Back to the beginning", reply_markup=InlineKeyboardMarkup(button))
def second_page(update, context):
query = update.callback_query
buttons = []
buttons_row = []
x = 0
for number in range(57, 115):
buttons_row.append(InlineKeyboardButton(number, callback_data=str(number)))
x += 1
if x == 6:
buttons.append(buttons_row)
buttons_row = []
x = 0
# failsafe, in case we have numbers left. Probably unnecessary, but its way too late for me to figure that out
if buttons_row:
buttons.append(buttons_row)
back_button = InlineKeyboardButton("back", callback_data="numbers")
next_button = InlineKeyboardButton("cancel", callback_data="cancel")
menu_buttons = [back_button, next_button]
buttons.append(menu_buttons)
query.edit_message_text("Numbers", reply_markup=InlineKeyboardMarkup(buttons))
def number(update, context):
query = update.callback_query
button = [[InlineKeyboardButton("beginning", callback_data="cancel")]]
query.edit_message_text(query.data, reply_markup=InlineKeyboardMarkup(button))
def main():
updater = Updater("TOKEN", use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start_command))
dp.add_handler(CallbackQueryHandler(fallback, pattern="cancel"))
dp.add_handler(CallbackQueryHandler(first_page, pattern="numbers"))
dp.add_handler(CallbackQueryHandler(second_page, pattern="next"))
# first regex the internet gave me, https://stackoverflow.com/a/9011537
dp.add_handler(CallbackQueryHandler(number, pattern="^[0-9]+$"))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment