import sys sys.path.append(".") import os import io import traceback from telegram import Update, ReplyKeyboardMarkup from telegram.ext import filters, ApplicationBuilder, MessageHandler, CommandHandler, ContextTypes from backend.agent import agent, redis from pypdf import PdfReader VACANCIES = "πŸ‘₯ Вакансии" RESUME = "πŸ“„ РСзюмС" async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): text = "ΠŸΡ€ΠΈΠ²Π΅Ρ‚! Π― ΠΊΠ°Ρ€ΡŒΠ΅Ρ€Π½Ρ‹ΠΉ ΠΊΠΎΠΏΠΈΠ»ΠΎΡ‚: ΠΏΠΎΠΌΠΎΠ³Ρƒ с Ρ€Π°Π±ΠΎΡ‚ΠΎΠΉ, ΠΈΠ½Ρ‚Π΅Ρ€Π²ΡŒΡŽ ΠΈ расскаТу новости ΠΏΠΎ Ρ€Ρ‹Π½ΠΊΡƒ, ΡΠΏΠ΅Ρ†ΠΈΠ°Π»ΡŒΠ½ΠΎ для тСбя. Π‘ Ρ‡Π΅Π³ΠΎ Π½Π°Ρ‡Π½Π΅ΠΌ?" reply_markup = ReplyKeyboardMarkup([[VACANCIES, RESUME]], resize_keyboard=True, one_time_keyboard=False) await context.bot.send_message(chat_id=update.effective_chat.id, text=text, reply_markup=reply_markup) async def prompt(update: Update, context: ContextTypes.DEFAULT_TYPE): if update.message.text == RESUME: resume = await redis.get(update.effective_user.id) text = "ΠŸΡ€ΠΈΡˆΠ»ΠΈ ΠΌΠ½Π΅ Ρ„Π°ΠΉΠ» с Ρ‚Π²ΠΎΠΈΠΌ Ρ€Π΅Π·ΡŽΠΌΠ΅, Ρ‡Ρ‚ΠΎΠ±Ρ‹ я смог Π½Π°ΠΉΡ‚ΠΈ подходящиС вакансии Π½Π° основС Ρ‚Π²ΠΎΠΈΡ… ΠΊΠΎΠΌΠΏΠ΅Ρ‚Π΅Π½Ρ†ΠΈΠΉ!" if resume: text = "Π― ΡƒΠΆΠ΅ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΠ» Ρ‚Π²ΠΎΠ΅ Ρ€Π΅Π·ΡŽΠΌΠ΅. Если Ρ…ΠΎΡ‡Π΅ΡˆΡŒ Π΅Π³ΠΎ ΠΎΠ±Π½ΠΎΠ²ΠΈΡ‚ΡŒ, ΠΏΡ€ΠΈΡˆΠ»ΠΈ ΠΌΠ½Π΅ Π½ΠΎΠ²Ρ‹ΠΉ Ρ„Π°ΠΉΠ»!" await context.bot.send_message(update.effective_chat.id, text) return user_prompt = update.message.text if update.message.text == VACANCIES: user_prompt = "ΠŸΡ€ΠΈΡˆΠ»ΠΈ ΠΌΠ½Π΅ Π°ΠΊΡ‚ΡƒΠ°Π»ΡŒΠ½Ρ‹Π΅ вакансии Π½Π° основС ΠΌΠΎΠ΅Π³ΠΎ Ρ€Π΅Π·ΡŽΠΌΠ΅" message = await context.bot.send_message(update.effective_chat.id, "πŸ“ ΠžΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°ΡŽ Ρ‚Π²ΠΎΠΉ запрос. ΠŸΠΎΠΆΠ°Π»ΡƒΠΉΡΡ‚Π°, ΠΏΠΎΠ΄ΠΎΠΆΠ΄ΠΈΡ‚Π΅...") response = await agent.ainvoke( input={"messages": [{"role": "user", "content": f"user_id = {update.effective_user.id}\n{user_prompt}"}]}, config={"configurable": {"thread_id": update.effective_user.id}}, ) await context.bot.editMessageText(response['messages'][-1].content, update.effective_chat.id, message.id) async def error_handler(update: object, context: ContextTypes.DEFAULT_TYPE) -> None: traceback.print_exception(context.error) await context.bot.send_message(chat_id=update.effective_chat.id, text="ΠŸΡ€ΠΎΠΈΠ·ΠΎΡˆΠ»Π° ошибка. ΠŸΠΎΠ²Ρ‚ΠΎΠΈΡ‚Π΅ ΠΏΠΎΠΏΡ‹Ρ‚ΠΊΡƒ ΠΏΠΎΠ·ΠΆΠ΅.") async def handle_document(update: Update, context: ContextTypes.DEFAULT_TYPE): if not update.message.document: await context.bot.send_message(chat_id=update.effective_chat.id, text="НС ΡƒΠ΄Π°Π»ΠΎΡΡŒ ΠΏΡ€ΠΎΡ‡ΠΈΡ‚Π°Ρ‚ΡŒ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡŽ ΠΈΠ· Ρ„Π°ΠΉΠ»Π°! ΠŸΠΎΠΏΡ€ΠΎΠ±ΡƒΠΉΡ‚Π΅ Π΄Ρ€ΡƒΠ³ΠΎΠΉ Ρ„ΠΎΡ€ΠΌΠ°Ρ‚.") return buffer = io.BytesIO() file = await update.message.document.get_file() await file.download_to_memory(buffer) reader = PdfReader(buffer) resume = "\n".join(page.extract_text() for page in reader.pages) await redis.set(update.effective_user.id, resume) await context.bot.send_message(chat_id=update.effective_chat.id, text="ΠžΡ‚Π»ΠΈΡ‡Π½ΠΎ! Π—Π°ΠΏΠΎΠΌΠ½ΠΈΠ» Π’Π°ΡˆΠ΅ Ρ€Π΅Π·ΡŽΠΌΠ΅.") def main(): application = ApplicationBuilder().token(os.environ["BOT_TOKEN"]).build() start_handler = CommandHandler('start', start) application.add_handler(start_handler) application.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), prompt)) application.add_handler(MessageHandler((filters.Document.ALL | filters.PHOTO) & (~filters.COMMAND), handle_document)) application.add_error_handler(error_handler) application.run_polling() if __name__ == '__main__': main()