mouliraj56 commited on
Commit
e74123e
·
1 Parent(s): b277b43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -19
app.py CHANGED
@@ -1,11 +1,14 @@
 
1
  import logging
2
- from telegram import Update
3
- from telegram.ext import Updater, CommandHandler, MessageHandler, CallbackContext, Filters
4
- import PyPDF2 # Import the PyPDF2 library
5
  import io
 
 
 
 
6
 
7
  # Your provided BOT_TOKEN
8
- BOT_TOKEN = '6573536783:AAFxDVflga8vYXdqhJG-14zjhdmvOrtbQuQ'
9
 
10
  # Enable logging
11
  logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
@@ -14,10 +17,8 @@ logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s
14
  updater = Updater(token=BOT_TOKEN, use_context=True)
15
  dispatcher = updater.dispatcher
16
 
17
- # Define the start command handler
18
- def start(update: Update, context: CallbackContext):
19
- update.message.reply_text("Welcome to the PDF Bot! Send me a PDF file, and I will send it back to you. "
20
- "You can also say hello, and I will respond with a greeting.")
21
 
22
  # Define a custom filter to check if a message contains a PDF file
23
  def is_pdf(update: Update) -> bool:
@@ -35,31 +36,97 @@ def handle_user_message(update: Update, context: CallbackContext):
35
  else:
36
  update.message.reply_text("I can handle PDF files. Please send me a PDF file.")
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  # Define the echo handler to handle PDF files
39
  def echo(update: Update, context: CallbackContext):
40
  # Get the PDF file
41
  file = context.bot.get_file(update.message.document.file_id)
42
- # Send the same file back to the user
43
- context.bot.send_document(chat_id=update.message.chat_id, document=file.file_id)
44
 
45
  # Get the PDF file's name
46
  pdf_name = update.message.document.file_name
47
 
48
- # Download the PDF file as a bytearray
49
- pdf_file = file.download_as_bytearray()
50
 
51
  # Use PyPDF2's PdfReader to calculate the number of pages
52
- pdf_reader = PyPDF2.PdfReader(io.BytesIO(pdf_file))
53
- num_pages = len(pdf_reader.pages) # Use len to get the number of pages
54
-
55
- # Send a message with PDF details
56
- response_message = f"Here is the PDF file: {pdf_name}\nNumber of pages: {num_pages}"
57
- context.bot.send_message(chat_id=update.message.chat_id, text=response_message)
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  # Add the command handler and message handlers to the dispatcher
60
- dispatcher.add_handler(CommandHandler("start", start))
61
  dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_user_message)) # Handles greetings and more
62
  dispatcher.add_handler(MessageHandler(Filters.document & Filters.update, echo)) # Handles PDFs
 
63
 
64
  # Start the bot
65
  updater.start_polling()
 
1
+ BOT_TOKEN = '6573536783:AAFxDVflga8vYXdqhJG-14zjhdmvOrtbQuQ'
2
  import logging
3
+ import PyPDF2
 
 
4
  import io
5
+ import win32print
6
+ import win32ui
7
+ from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
8
+ from telegram.ext import Updater, CommandHandler, MessageHandler, CallbackContext, Filters, CallbackQueryHandler
9
 
10
  # Your provided BOT_TOKEN
11
+
12
 
13
  # Enable logging
14
  logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
 
17
  updater = Updater(token=BOT_TOKEN, use_context=True)
18
  dispatcher = updater.dispatcher
19
 
20
+ # Define the PDF file and printer name
21
+ printer_name = win32print.GetDefaultPrinter()
 
 
22
 
23
  # Define a custom filter to check if a message contains a PDF file
24
  def is_pdf(update: Update) -> bool:
 
36
  else:
37
  update.message.reply_text("I can handle PDF files. Please send me a PDF file.")
38
 
39
+ # Define a function to handle the user's choice (single side or double side)
40
+ def handle_choice(update: Update, context: CallbackContext):
41
+ query = update.callback_query
42
+ choice = query.data
43
+
44
+ # Get the PDF file's name and number of pages from the context
45
+ pdf_name = context.user_data.get('pdf_name')
46
+ num_pages = context.user_data.get('num_pages')
47
+
48
+ # Calculate the price based on the user's choice
49
+ if choice == 'single':
50
+ price = num_pages * 3
51
+ elif choice == 'double':
52
+ price = (num_pages // 2) * 4 + (num_pages % 2) * 3
53
+
54
+ # Send a message with the price
55
+ response_message = f"PDF: {pdf_name}\nNumber of pages: {num_pages}\nPrice: {price} INR"
56
+ query.edit_message_text(text=response_message)
57
+
58
+ # Print the document
59
+ #print_pdf(pdf_name)
60
+
61
+ # Define a function to print the PDF document
62
+ def print_pdf(pdf_file):
63
+ # Open the PDF file
64
+ pdf = PyPDF2.PdfReader(open(pdf_file, "rb"))
65
+
66
+ # Define the printer settings
67
+ printer_handle = win32print.OpenPrinter(printer_name)
68
+ printer_info = win32print.GetPrinter(printer_handle, 2)
69
+ printer_dc = win32ui.CreateDC()
70
+ printer_dc.CreatePrinterDC(printer_name)
71
+
72
+ # Start a new page
73
+ printer_dc.StartDoc(pdf_file)
74
+ printer_dc.StartPage()
75
+
76
+ # Loop through the pages in the PDF and print each page
77
+ for page_num in range(len(pdf.pages)):
78
+ page = pdf.pages[page_num]
79
+ text = page.extract_text()
80
+ printer_dc.TextOut(100, 100, text)
81
+
82
+ # Print status for each page
83
+ print(f"Printing page {page_num + 1}/{len(pdf.pages)}")
84
+
85
+ # End the page and the document
86
+ printer_dc.EndPage()
87
+ printer_dc.EndDoc()
88
+
89
+ # Close the printer
90
+ win32print.ClosePrinter(printer_handle)
91
+
92
+ # Print a completion message
93
+ print("Printing completed.")
94
+
95
  # Define the echo handler to handle PDF files
96
  def echo(update: Update, context: CallbackContext):
97
  # Get the PDF file
98
  file = context.bot.get_file(update.message.document.file_id)
 
 
99
 
100
  # Get the PDF file's name
101
  pdf_name = update.message.document.file_name
102
 
103
+ # Download the PDF file as 'downloaded.pdf'
104
+ file.download('downloaded.pdf')
105
 
106
  # Use PyPDF2's PdfReader to calculate the number of pages
107
+ pdf_reader = PyPDF2.PdfReader('downloaded.pdf')
108
+ num_pages = len(pdf_reader.pages)
109
+
110
+ # Store the PDF details in context for later use
111
+ context.user_data['pdf_name'] = pdf_name
112
+ context.user_data['num_pages'] = num_pages
113
+
114
+ # Create inline keyboard buttons for the user to choose single side or double side
115
+ keyboard = [
116
+ [InlineKeyboardButton("Single Side", callback_data='single')],
117
+ [InlineKeyboardButton("Double Side", callback_data='double')],
118
+ ]
119
+ reply_markup = InlineKeyboardMarkup(keyboard)
120
+
121
+ # Ask the user to choose the printing preference
122
+ update.message.reply_text(f"PDF: {pdf_name}\nNumber of pages: {num_pages}\nPlease choose your printing preference:",
123
+ reply_markup=reply_markup)
124
 
125
  # Add the command handler and message handlers to the dispatcher
126
+ dispatcher.add_handler(CommandHandler("start", handle_user_message))
127
  dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_user_message)) # Handles greetings and more
128
  dispatcher.add_handler(MessageHandler(Filters.document & Filters.update, echo)) # Handles PDFs
129
+ dispatcher.add_handler(CallbackQueryHandler(handle_choice)) # Handles user choice
130
 
131
  # Start the bot
132
  updater.start_polling()