-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsfile.py
More file actions
76 lines (64 loc) · 2.98 KB
/
sfile.py
File metadata and controls
76 lines (64 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import telebot
import getpass
# Ваш токен бота Telegram
API_TOKEN = 'ВАШ_ТЕЛЕГРАМ_ТОКЕН'
CHAT_ID = 'ID_ЧАТА_ИЛИ_ПОЛЬЗОВАТЕЛЯ'
bot = telebot.TeleBot(API_TOKEN)
def find_file(file_name):
"""Функция для поиска файла по имени"""
for root, dirs, files in os.walk('/'):
if file_name in files:
return os.path.join(root, file_name)
return None
def send_file_to_telegram(file_path, chat_id):
"""Функция для отправки файла в Telegram"""
try:
with open(file_path, 'rb') as file:
bot.send_document(chat_id, file)
print(f"Файл {file_path} успешно отправлен в Telegram.")
except Exception as e:
print(f"Ошибка при отправке файла в Telegram: {e}")
def delete_file(file_path):
"""Функция для удаления файла"""
try:
os.remove(file_path)
print(f"Файл {file_path} был удалён.")
except Exception as e:
print(f"Ошибка при удалении файла: {e}")
@bot.message_handler(commands=['find'])
def handle_find_command(message):
"""Обрабатывает команду /find <имя файла>"""
try:
# Получаем имя файла из команды
file_name = message.text.split()[1]
except IndexError:
bot.reply_to(message, "Пожалуйста, укажите имя файла. Пример: /find example.txt")
return
# Ищем файл
file_path = find_file(file_name)
if file_path:
bot.reply_to(message, f"Файл найден: {file_path}. Отправляю файл...")
send_file_to_telegram(file_path, message.chat.id)
# Спрашиваем о необходимости удаления файла
markup = telebot.types.ReplyKeyboardMarkup(row_width=2, one_time_keyboard=True)
markup.add('Да', 'Нет')
msg = bot.reply_to(message, f"Хотите удалить файл {file_path}?", reply_markup=markup)
bot.register_next_step_handler(msg, lambda m: handle_delete_confirmation(m, file_path))
else:
bot.reply_to(message, f"Файл с именем {file_name} не найден.")
def handle_delete_confirmation(message, file_path):
"""Обрабатывает подтверждение удаления файла"""
if message.text.lower() == 'да':
delete_file(file_path)
bot.reply_to(message, f"Файл {file_path} был успешно удалён.")
else:
bot.reply_to(message, "Файл не был удалён.")
def main():
if getpass.getuser() != 'root':
print("Пожалуйста, запустите скрипт от имени суперпользователя (sudo).")
else:
print("Бот запущен. Ожидание команд...")
bot.polling()
if __name__ == "__main__":
main()