Skip to content

Commit c2874a7

Browse files
author
Daniil Alekseev
committed
added function write to file
1 parent b026cfb commit c2874a7

File tree

1 file changed

+87
-6
lines changed

1 file changed

+87
-6
lines changed

telegram_bot.py

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,50 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
439439
return
440440

441441

442+
# Handle write command states
443+
user_data = context.user_data
444+
445+
446+
if 'write_state' in user_data:
447+
file_path = user_data.get('write_file')
448+
current_state = user_data['write_state']
449+
450+
451+
logger.info(f"Processing write state {current_state} for {user_id}")
452+
453+
454+
if current_state == 'awaiting_content':
455+
try:
456+
with open(file_path, 'w', encoding='utf-8') as f:
457+
f.write(text)
458+
459+
460+
logger.info(f"Text set to {file_path}")
461+
462+
463+
del user_data['write_state']
464+
465+
466+
await update.message.reply_text("📝 Текст успешно записан в файл")
467+
468+
469+
except Exception as e:
470+
logger.error(f"Write error: {str(e)}")
471+
472+
473+
await update.message.reply_text("❌ Ошибка записи в файл")
474+
475+
476+
return
477+
478+
479+
elif user_data.get('pending_upload'):
480+
await handle_upload_confirmation(update, context)
481+
482+
483+
return
484+
485+
442486
logger.info(f"User {user_id} have permissions")
443487

444488

@@ -517,7 +561,7 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
517561

518562
return
519563

520-
564+
521565
open(os.path.join(current_dir, ' '.join(args)), 'a').close()
522566

523567

@@ -614,6 +658,46 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
614658
with open(file_path, 'r', encoding='utf-8') as f:
615659
content = f.read(4090) + "..." if os.path.getsize(file_path) > 4096 else f.read()
616660
response = f"<code>{content}</code>"
661+
662+
663+
case 'write':
664+
if not args:
665+
await update.message.reply_text("📛 Укажите название файла")
666+
667+
668+
logger.info(f"Write command without args from {user_id}")
669+
670+
671+
return
672+
673+
674+
file_name = ' '.join(args)
675+
file_path = os.path.join(current_dir, file_name)
676+
677+
678+
logger.info(f"Write command to {file_path}")
679+
680+
681+
if os.path.exists(file_path):
682+
if os.path.isdir(file_path):
683+
await update.message.reply_text("📁 Это папка, укажите файл")
684+
685+
686+
return
687+
688+
689+
else:
690+
open(file_path, 'a').close()
691+
692+
693+
user_data['write_file'] = file_path
694+
user_data['write_state'] = 'awaiting_content'
695+
696+
697+
await update.message.reply_text("📝 Введите текст или 'exit' для выхода:")
698+
699+
700+
return
617701

618702

619703
case 'download':
@@ -813,15 +897,13 @@ async def handle_upload_confirmation(update: Update, context: ContextTypes.DEFAU
813897

814898

815899
await update.message.reply_text(
816-
f"✅ Файл {file_info['file_name']} перезаписан",
817-
reply_markup=ReplyKeyboardMarkup(get_buttons(), one_time_keyboard=True)
900+
f"✅ Файл {file_info['file_name']} перезаписан"
818901
)
819902

820903

821904
else:
822905
await update.message.reply_text(
823-
"❌ Загрузка отменена",
824-
reply_markup=ReplyKeyboardMarkup(get_buttons(), one_time_keyboard=True)
906+
"❌ Загрузка отменена",
825907
)
826908

827909

@@ -894,7 +976,6 @@ def main() -> None:
894976

895977
# Registering handlers
896978
handlers = [
897-
MessageHandler(filters.Text(['Да', 'Нет']) & ~filters.COMMAND, handle_upload_confirmation),
898979
CommandHandler("start", start),
899980
CommandHandler("user", user),
900981
CommandHandler("help", help),

0 commit comments

Comments
 (0)