@@ -439,6 +439,50 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
439
439
return
440
440
441
441
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
+
442
486
logger .info (f"User { user_id } have permissions" )
443
487
444
488
@@ -517,7 +561,7 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
517
561
518
562
return
519
563
520
-
564
+
521
565
open (os .path .join (current_dir , ' ' .join (args )), 'a' ).close ()
522
566
523
567
@@ -614,6 +658,46 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
614
658
with open (file_path , 'r' , encoding = 'utf-8' ) as f :
615
659
content = f .read (4090 ) + "..." if os .path .getsize (file_path ) > 4096 else f .read ()
616
660
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
617
701
618
702
619
703
case 'download' :
@@ -813,15 +897,13 @@ async def handle_upload_confirmation(update: Update, context: ContextTypes.DEFAU
813
897
814
898
815
899
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' ]} перезаписан"
818
901
)
819
902
820
903
821
904
else :
822
905
await update .message .reply_text (
823
- "❌ Загрузка отменена" ,
824
- reply_markup = ReplyKeyboardMarkup (get_buttons (), one_time_keyboard = True )
906
+ "❌ Загрузка отменена" ,
825
907
)
826
908
827
909
@@ -894,7 +976,6 @@ def main() -> None:
894
976
895
977
# Registering handlers
896
978
handlers = [
897
- MessageHandler (filters .Text (['Да' , 'Нет' ]) & ~ filters .COMMAND , handle_upload_confirmation ),
898
979
CommandHandler ("start" , start ),
899
980
CommandHandler ("user" , user ),
900
981
CommandHandler ("help" , help ),
0 commit comments