BOT_TOKEN = '6573536783:AAFxDVflga8vYXdqhJG-14zjhdmvOrtbQuQ' import logging import PyPDF2 import io import win32print import win32ui from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup from telegram.ext import Updater, CommandHandler, MessageHandler, CallbackContext, Filters, CallbackQueryHandler # Your provided BOT_TOKEN # Enable logging logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) # Initialize the updater and dispatcher updater = Updater(token=BOT_TOKEN, use_context=True) dispatcher = updater.dispatcher # Define the PDF file and printer name printer_name = win32print.GetDefaultPrinter() # Define a custom filter to check if a message contains a PDF file def is_pdf(update: Update) -> bool: return update.message.document and update.message.document.file_name.endswith('.pdf') # Define a function to handle greetings and other user messages def handle_user_message(update: Update, context: CallbackContext): user_message = update.message.text.lower() if user_message == 'hello': update.message.reply_text("Hello! How can I assist you today?") elif 'thank you' in user_message: update.message.reply_text("You're welcome!") elif 'how are you' in user_message: update.message.reply_text("I'm just a bot, but I'm here to help you!") else: update.message.reply_text("I can handle PDF files. Please send me a PDF file.") # Define a function to handle the user's choice (single side or double side) def handle_choice(update: Update, context: CallbackContext): query = update.callback_query choice = query.data # Get the PDF file's name and number of pages from the context pdf_name = context.user_data.get('pdf_name') num_pages = context.user_data.get('num_pages') # Calculate the price based on the user's choice if choice == 'single': price = num_pages * 3 elif choice == 'double': price = (num_pages // 2) * 4 + (num_pages % 2) * 3 # Send a message with the price response_message = f"PDF: {pdf_name}\nNumber of pages: {num_pages}\nPrice: {price} INR" query.edit_message_text(text=response_message) # Print the document #print_pdf(pdf_name) # Define a function to print the PDF document def print_pdf(pdf_file): # Open the PDF file pdf = PyPDF2.PdfReader(open(pdf_file, "rb")) # Define the printer settings printer_handle = win32print.OpenPrinter(printer_name) printer_info = win32print.GetPrinter(printer_handle, 2) printer_dc = win32ui.CreateDC() printer_dc.CreatePrinterDC(printer_name) # Start a new page printer_dc.StartDoc(pdf_file) printer_dc.StartPage() # Loop through the pages in the PDF and print each page for page_num in range(len(pdf.pages)): page = pdf.pages[page_num] text = page.extract_text() printer_dc.TextOut(100, 100, text) # Print status for each page print(f"Printing page {page_num + 1}/{len(pdf.pages)}") # End the page and the document printer_dc.EndPage() printer_dc.EndDoc() # Close the printer win32print.ClosePrinter(printer_handle) # Print a completion message print("Printing completed.") # Define the echo handler to handle PDF files def echo(update: Update, context: CallbackContext): # Get the PDF file file = context.bot.get_file(update.message.document.file_id) # Get the PDF file's name pdf_name = update.message.document.file_name # Download the PDF file as 'downloaded.pdf' file.download('downloaded.pdf') # Use PyPDF2's PdfReader to calculate the number of pages pdf_reader = PyPDF2.PdfReader('downloaded.pdf') num_pages = len(pdf_reader.pages) # Store the PDF details in context for later use context.user_data['pdf_name'] = pdf_name context.user_data['num_pages'] = num_pages # Create inline keyboard buttons for the user to choose single side or double side keyboard = [ [InlineKeyboardButton("Single Side", callback_data='single')], [InlineKeyboardButton("Double Side", callback_data='double')], ] reply_markup = InlineKeyboardMarkup(keyboard) # Ask the user to choose the printing preference update.message.reply_text(f"PDF: {pdf_name}\nNumber of pages: {num_pages}\nPlease choose your printing preference:", reply_markup=reply_markup) # Add the command handler and message handlers to the dispatcher dispatcher.add_handler(CommandHandler("start", handle_user_message)) dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_user_message)) # Handles greetings and more dispatcher.add_handler(MessageHandler(Filters.document & Filters.update, echo)) # Handles PDFs dispatcher.add_handler(CallbackQueryHandler(handle_choice)) # Handles user choice # Start the bot updater.start_polling() updater.idle()