|
import logging |
|
from telegram import Update |
|
from telegram.ext import Updater, CommandHandler, MessageHandler, CallbackContext, Filters |
|
import PyPDF2 |
|
import io |
|
|
|
|
|
BOT_TOKEN = '6573536783:AAFxDVflga8vYXdqhJG-14zjhdmvOrtbQuQ' |
|
|
|
|
|
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) |
|
|
|
|
|
updater = Updater(token=BOT_TOKEN, use_context=True) |
|
dispatcher = updater.dispatcher |
|
|
|
|
|
def start(update: Update, context: CallbackContext): |
|
update.message.reply_text("Welcome to the PDF Bot! Send me a PDF file, and I will send it back to you. " |
|
"You can also say hello, and I will respond with a greeting.") |
|
|
|
|
|
def is_pdf(update: Update) -> bool: |
|
return update.message.document and update.message.document.file_name.endswith('.pdf') |
|
|
|
|
|
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.") |
|
|
|
|
|
def echo(update: Update, context: CallbackContext): |
|
|
|
file = context.bot.get_file(update.message.document.file_id) |
|
|
|
context.bot.send_document(chat_id=update.message.chat_id, document=file.file_id) |
|
|
|
|
|
pdf_name = update.message.document.file_name |
|
|
|
|
|
pdf_file = file.download_as_bytearray() |
|
|
|
|
|
pdf_reader = PyPDF2.PdfReader(io.BytesIO(pdf_file)) |
|
num_pages = len(pdf_reader.pages) |
|
|
|
|
|
response_message = f"Here is the PDF file: {pdf_name}\nNumber of pages: {num_pages}" |
|
context.bot.send_message(chat_id=update.message.chat_id, text=response_message) |
|
|
|
|
|
dispatcher.add_handler(CommandHandler("start", start)) |
|
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_user_message)) |
|
dispatcher.add_handler(MessageHandler(Filters.document & Filters.update, echo)) |
|
|
|
|
|
updater.start_polling() |
|
updater.idle() |
|
|