mouliraj56 commited on
Commit
380ff4d
1 Parent(s): 72429f2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
12
+
13
+ # Initialize the updater and dispatcher
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:
24
+ return update.message.document and update.message.document.file_name.endswith('.pdf')
25
+
26
+ # Define a function to handle greetings and other user messages
27
+ def handle_user_message(update: Update, context: CallbackContext):
28
+ user_message = update.message.text.lower()
29
+ if user_message == 'hello':
30
+ update.message.reply_text("Hello! How can I assist you today?")
31
+ elif 'thank you' in user_message:
32
+ update.message.reply_text("You're welcome!")
33
+ elif 'how are you' in user_message:
34
+ update.message.reply_text("I'm just a bot, but I'm here to help you!")
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()
66
+ updater.idle()