Created
March 23, 2021 12:06
-
-
Save Poolitzer/dfc73283e52d4d9b67359fbb6865396a to your computer and use it in GitHub Desktop.
This is a callbackquery paginator
This file contains hidden or 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
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update | |
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, CallbackContext | |
def navigation_buttons_generator(index: str): | |
navigation_buttons = [[InlineKeyboardButton("⏪", callback_data="minus_" + index), | |
InlineKeyboardButton("⏩", callback_data="plus_" + index)]] | |
return InlineKeyboardMarkup(navigation_buttons) | |
def start(update: Update, _: CallbackContext): | |
keyboard = [[InlineKeyboardButton("Short", callback_data="select_short")], | |
[InlineKeyboardButton("Long", callback_data="select_long")]] | |
update.effective_message.reply_text("Select what you want", | |
reply_markup=InlineKeyboardMarkup(keyboard)) | |
def selection(update: Update, context: CallbackContext): | |
query = update.callback_query | |
if query.data.split("_")[1] == "short": | |
query.edit_message_text("Great, this is short enough to be in one message") | |
return | |
long_message_split_into_list = ["This is the first part", "the second", "third", | |
"and fourth part of the message"] | |
context.user_data["long_message"] = long_message_split_into_list | |
nav_buttons = navigation_buttons_generator(str(0)) | |
query.edit_message_text(long_message_split_into_list[0], reply_markup=nav_buttons) | |
def forward(update: Update, context: CallbackContext): | |
query = update.callback_query | |
index = int(query.data.split("_")[1]) + 1 | |
try: | |
text = context.user_data["long_message"][index] | |
except IndexError: | |
index = 0 | |
text = context.user_data["long_message"][index] | |
nav_buttons = navigation_buttons_generator(str(index)) | |
query.edit_message_text(text, reply_markup=nav_buttons) | |
def backwards(update: Update, context: CallbackContext): | |
query = update.callback_query | |
index = int(query.data.split("_")[1]) - 1 | |
try: | |
text = context.user_data["long_message"][index] | |
except IndexError: | |
index = -1 | |
text = context.user_data["long_message"][index] | |
nav_buttons = navigation_buttons_generator(str(index)) | |
query.edit_message_text(text, reply_markup=nav_buttons) | |
def main(): | |
updater = Updater("TOKEN") | |
updater.dispatcher.add_handler(CommandHandler("start", start)) | |
updater.dispatcher.add_handler(CallbackQueryHandler(selection, pattern="select")) | |
updater.dispatcher.add_handler(CallbackQueryHandler(forward, pattern="plus")) | |
updater.dispatcher.add_handler(CallbackQueryHandler(backwards, pattern="minus")) | |
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