Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import logging
- import os
- import settings
- import math
- 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)
- logger = logging.getLogger(__name__)
- my_data = {
- 'op1': [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],
- 'op2': [21,22,23,24,25,26,27,28,29,30,31,32,33,34,35]
- }
- def format_search(data, index=0):
- text = "*Numbers*: \n"
- for i in range(index, index + 5):
- text += f"*index*: {data[i]}\n"
- return text
- def start(update, context):
- update.message.reply_text('hello')
- def area(update, context):
- args = context.args
- for arg in args:
- print(arg)
- ########
- results = my_data['op1']
- query = "op1"
- if args:
- query = "op2"
- results = my_data['op2']
- N = len(results)
- context.chat_data['results'] = {
- 'query': query,
- 'size': N
- }
- text = format_search(results)
- #########
- keyboard = [[InlineKeyboardButton("Prev", callback_data=0),
- InlineKeyboardButton(f"1/{math.ceil(N/5)}", callback_data='nothing'),
- InlineKeyboardButton("Next", callback_data=5)]]
- reply_markup = InlineKeyboardMarkup(keyboard)
- update.message.reply_text(text, reply_markup=reply_markup, parse_mode='markdown')
- def button(update, context):
- query = update.callback_query
- selected = query.data
- current = 0
- print(f"current {selected}")
- results = context.chat_data['results']
- key = results['query']
- N = results['size']
- data = my_data[key]
- if selected == "nothing":
- query.answer("Esse botão não faz nada.")
- else:
- current = int(selected)
- if current < 0:
- prev_button = 0
- query.answer("Você está no começo da lista.")
- elif current >= N:
- next_button = N - 5
- query.answer("Você está no fim da lista.")
- else:
- prev_button = current - 5
- next_button = current + 5
- keyboard = [[InlineKeyboardButton("Prev", callback_data=prev_button),
- InlineKeyboardButton(f"{current//5 + 1}/{math.ceil(N/5)}", callback_data='nothing'),
- InlineKeyboardButton("Next", callback_data=next_button)]]
- reply_markup = InlineKeyboardMarkup(keyboard)
- text = format_search(data, current)
- query.edit_message_text(text=text, reply_markup=reply_markup, parse_mode='markdown')
- def help(update, context):
- update.message.reply_text("Use /start to test this bot.")
- def error(update, context):
- """Log Errors caused by Updates."""
- logger.warning('Update "%s" caused error "%s"', update, context.error)
- def main():
- # Create the Updater and pass it your bot's token.
- # Make sure to set use_context=True to use the new context based callbacks
- # Post version 12 this will no longer be necessary
- updater = Updater(os.getenv("TOKEN"), use_context=True)
- updater.dispatcher.add_handler(CommandHandler('start', start))
- updater.dispatcher.add_handler(CommandHandler('area', area, pass_args=True, pass_chat_data=True))
- updater.dispatcher.add_handler(CallbackQueryHandler(button))
- updater.dispatcher.add_handler(CommandHandler('help', help))
- updater.dispatcher.add_error_handler(error)
- # Start the Bot
- updater.start_polling()
- # Run the bot until the user presses Ctrl-C or the process receives SIGINT,
- # SIGTERM or SIGABRT
- updater.idle()
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment