File size: 4,983 Bytes
e74123e
b277b43
e74123e
b277b43
e74123e
 
 
 
b277b43
 
e74123e
b277b43
 
 
 
 
 
 
 
e74123e
 
b277b43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e74123e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b277b43
 
 
 
 
 
 
 
e74123e
 
b277b43
 
e74123e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b277b43
 
e74123e
b277b43
 
e74123e
b277b43
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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()