Question
PYTHON :: write the required libraries to this code to make it work 100% the code :: ############################### # Set up logging logging.basicConfig(format='%(asctime)s - %(name)s
PYTHON :: write the required libraries to this code to make it work 100% the code :: ###############################
# Set up logging logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
# Initialize variables pdf_name = "default.pdf" pdf_pages = 0
def start(update, context): """Send a message when the command /start is issued.""" update.message.reply_text('Welcome, I am a pdf converter bot. Please send me images to convert them to pdf')
def handle_photo(update, context): global pdf_pages """Handle incoming photos and add them to the pdf""" photo_file = update.message.photo[-1].get_file() photo_file.download(f'temp-{pdf_pages}.jpg') pdf_pages += 1 update.message.reply_text(f'Added {pdf_pages} pages to your pdf.') markup = InlineKeyboardMarkup([[InlineKeyboardButton("Generate PDF", callback_data='generate'), InlineKeyboardButton("Rename PDF", callback_data='rename')]]) update.message.reply_text('What do you want to do next?', reply_markup=markup) def generate_pdf(update, context): global pdf_name, pdf_pages """Generate a pdf from the images and send it to the user""" c.save() pdf.seek(0) context.bot.send_document(chat_id=update.message.chat_id, document=pdf, filename=pdf_name) pdf_name = 'default.pdf' pdf_pages = 0 for i in range(pdf_pages): os.remove(f'temp-{i}.jpg')
def rename_pdf(update, context): """Rename the pdf file""" update.message.reply_text("Please send me the new name for the pdf file.")
def rename_pdf_name(update, context): global pdf_name """Update the pdf name""" pdf_name = update.message.text update.message.reply_text(f"The pdf name is now {pdf_name}")
def main(): """Start the bot.""" # Create the Updater and pass it your bot's token. updater = Updater("5808542962:AAERdSY2ePlRhXGtoWiCvWjjOUpIaaVO5Z8")
# Get the dispatcher to register handlers dp = updater.dispatcher
# on different commands - answer in Telegram dp.add_handler(CommandHandler("start", start))
# on noncommand i.e message - echo the message on Telegram dp.add_handler(MessageHandler(Filters.photo, handle_photo)) dp.add_handler(CallbackQueryHandler(generate_pdf, pattern='generate')) dp.add_handler(CallbackQueryHandler(rename_pdf, pattern='rename')) dp.add_handler(MessageHandler(Filters.text, rename_pdf_name))
# Start the Bot updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT, # SIGTERM or SIGABRT. This should be used most of the time, since # start_polling() is non-blocking and will stop the bot gracefully. updater.idle()
if __name__ == '__main__': main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started