-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
434 lines (382 loc) · 18.9 KB
/
main.py
File metadata and controls
434 lines (382 loc) · 18.9 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import datetime
import os
import re
import subprocess
import json
import telebot
from telebot import types
import time
import requests
import urllib3
# Отключение предупреждений для непроверенных HTTPS запросов
# Лучше использовать сертификаты, но если это невозможно:
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Импорт AI класса
from AI import AI
files = ['last.txt', 'send.txt', 're.txt', '1.txt']
betterstack_token = "sta83CJpdZgwEXmir3MHaAcJ"
betterstack_url = "https://s1262208.eu-nbg-2.betterstackdata.com"
def log_info(message):
"""Отправляет информационное сообщение в Better Stack."""
try:
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {betterstack_token}'
}
data = {
'dt': datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC'),
'message': str(message),
'level': 'info'
}
requests.post(betterstack_url, headers=headers, json=data, verify=False)
except Exception as e:
print(f"Ошибка отправки лога: {e}")
def log_error(message):
"""Отправляет сообщение об ошибке в Better Stack."""
try:
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {betterstack_token}'
}
data = {
'dt': datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC'),
'message': str(message),
'level': 'error'
}
requests.post(betterstack_url, headers=headers, json=data, verify=False)
except Exception as e:
print(f"Ошибка отправки лога: {e}")
# Проверка и создание config.json
if not os.path.exists('config.json'):
with open('config.json', 'w', encoding='utf-8') as f:
h = {
"user1": {
"chat_id": 12345,
"bot_token": "YOUR_BOT_TOKEN",
"gemini_api": "YOUR_GEMINI_API",
"gemini_model": "gemini-2.5-flash-preview-04-17",
"tags": ["#код", "#учёба", "#файлы", "#нейронка", "#прочее"],
"db_app_key": "your-key",
"db_app_secret": "your-secret",
"db_redirect_uri": "https://api.sppq.site/api/dropbox/",
"proxy": {
"http": "http://vyWVVn:718sxT@94.131.54.206:9491",
"https": "http://vyWVVn:718sxT@94.131.54.206:9491"
}
},
"user2": {
"chat_id": 67890,
"bot_token": "YOUR_BOT_TOKEN",
"gemini_api": "YOUR_GEMINI_API",
"gemini_model": "gemini-2.5-flash-preview-04-17",
"tags": ["#работа", "#заметки", "#файлы", "#важное", "#прочее"],
"db_app_key": "your-key",
"db_app_secret": "your-secret",
"db_redirect_uri": "https://api.sppq.site/api/dropbox/",
"proxy": {
"http": "http://vyWVVn:718sxT@94.131.54.206:9491",
"https": "http://vyWVVn:718sxT@94.131.54.206:9491"
}
}
}
json.dump(h, f, indent=4)
log_info('Файл "config.json" создан, требуется настройка')
print('Файл "config.json" создан, настройте его вручную.')
exit()
# Загрузка конфига
config = json.load(open('config.json', 'r', encoding='utf-8'))
USERS = {
config['user1']['chat_id']: config['user1'],
config['user2']['chat_id']: config['user2']
}
log_info('Конфигурация загружена успешно')
# Инициализация бота
bot = telebot.TeleBot(config['user1']['bot_token'])
log_info('Бот инициализирован')
# Словарь для хранения last_message_id
last_messages = {
config['user1']['chat_id']: None,
config['user2']['chat_id']: None
}
# Создание необходимых файлов для каждого пользователя
for user_id in USERS.keys():
for file in files:
filename = f'{file.split(".")[0]}_{user_id}.txt'
if not os.path.exists(filename):
with open(filename, 'w', encoding='utf-8') as f:
f.write('')
log_info(f'Создан файл {filename} для пользователя {user_id}')
def html_to_markdown(html_text):
"""Конвертирует HTML в Markdown."""
if not html_text:
return ""
html_text = html_text.replace('\n', '<br>')
html_text = re.sub(r'<b>(.*?)</b>', r'**\1**', html_text)
html_text = re.sub(r'<strong>(.*?)</strong>', r'**\1**', html_text)
html_text = re.sub(r'<a href="(.*?)">(.*?)</a>', r'[\2](\1)', html_text)
markdown_text = html_text.replace('<br>', '\n\n')
markdown_text = re.sub(r'\]\s*\(', r'](', markdown_text)
return markdown_text
def download_file(file_id, filename):
"""Скачивает файл из Telegram."""
file_info = bot.get_file(file_id)
downloaded_file = bot.download_file(file_info.file_path)
with open(filename, 'wb') as f:
f.write(downloaded_file)
@bot.message_handler(commands=['start'])
def start(message):
chat_id = message.chat.id
if chat_id not in USERS:
log_error(f'Попытка доступа к боту с неавторизованного чата {chat_id}')
bot.reply_to(message, "У вас нет доступа к этому боту.")
return
user_config = USERS[chat_id]
welcome_text = (
f"👋 Добро пожаловать!\n\n"
f"🏷 Ваши текущие теги:\n"
f"{', '.join(user_config['tags'])}\n\n"
f"Команды:\n"
f"/tags - управление тегами\n"
)
bot.reply_to(message, welcome_text)
log_info(f'Пользователь {chat_id} запустил бота')
@bot.message_handler(commands=['tags'])
def show_tags(message):
chat_id = message.chat.id
if chat_id not in USERS:
log_error(f'Попытка доступа к команде /tags с неавторизованного чата {chat_id}')
return
user_tags = USERS[chat_id]['tags']
if not user_tags:
bot.reply_to(message, "У вас пока нет тегов.")
log_info(f'Пользователь {chat_id} запросил теги, но их нет')
return
current_tag_index = 0
markup = types.InlineKeyboardMarkup()
# Кнопки навигации и удаления
nav_buttons = [
types.InlineKeyboardButton("⬅️", callback_data=f"prev_{current_tag_index}"),
types.InlineKeyboardButton("❌", callback_data=f"delete_{current_tag_index}"),
types.InlineKeyboardButton("➡️", callback_data=f"next_{current_tag_index}")
]
markup.row(*nav_buttons)
# Кнопка добавления нового тега
markup.row(types.InlineKeyboardButton("➕ Добавить тег", callback_data="add_tag"))
bot.reply_to(message, f"Тег {current_tag_index + 1}/{len(user_tags)}:\n{user_tags[current_tag_index]}",
reply_markup=markup)
log_info(f'Пользователь {chat_id} запросил просмотр тегов')
@bot.callback_query_handler(func=lambda call: True)
def handle_callback(call):
chat_id = call.message.chat.id
if chat_id not in USERS:
log_error(f'Callback-запрос с неавторизованного чата {chat_id}')
return
user_tags = USERS[chat_id]['tags']
# Сначала проверяем специальные команды
if call.data == "add_tag":
msg = bot.edit_message_text(
"Отправьте новый тег (с # или без):",
chat_id=chat_id,
message_id=call.message.message_id
)
bot.register_next_step_handler(msg, process_new_tag)
log_info(f'Пользователь {chat_id} начал добавление нового тега')
return
# Затем обрабатываем команды с индексами
try:
current_index = int(call.data.split('_')[1]) if '_' in call.data else 0
except ValueError:
log_error(f'Ошибка парсинга callback-запроса: {call.data} от пользователя {chat_id}')
current_index = 0
if call.data.startswith("next"):
current_index = (current_index + 1) % len(user_tags)
log_info(f'Пользователь {chat_id} перешел к следующему тегу')
elif call.data.startswith("prev"):
current_index = (current_index - 1) % len(user_tags)
log_info(f'Пользователь {chat_id} перешел к предыдущему тегу')
elif call.data.startswith("delete"):
tag_to_delete = user_tags[current_index]
USERS[chat_id]['tags'].remove(tag_to_delete)
save_config()
log_info(f'Пользователь {chat_id} удалил тег {tag_to_delete}')
if not user_tags:
bot.edit_message_text(
"Все теги удалены.",
chat_id=chat_id,
message_id=call.message.message_id
)
return
current_index = min(current_index, len(user_tags) - 1)
if user_tags:
markup = types.InlineKeyboardMarkup()
nav_buttons = [
types.InlineKeyboardButton("⬅️", callback_data=f"prev_{current_index}"),
types.InlineKeyboardButton("❌", callback_data=f"delete_{current_index}"),
types.InlineKeyboardButton("➡️", callback_data=f"next_{current_index}")
]
markup.row(*nav_buttons)
markup.row(types.InlineKeyboardButton("➕ Добавить тег", callback_data="add_tag"))
bot.edit_message_text(
f"Тег {current_index + 1}/{len(user_tags)}:\n{user_tags[current_index]}",
chat_id=chat_id,
message_id=call.message.message_id,
reply_markup=markup
)
def process_new_tag(message):
chat_id = message.chat.id
if chat_id not in USERS:
log_error(f'Попытка добавления тега с неавторизованного чата {chat_id}')
return
new_tag = message.text
if not new_tag.startswith('#'):
new_tag = '#' + new_tag
if new_tag in USERS[chat_id]['tags']:
bot.reply_to(message, "Такой тег уже существует!")
log_info(f'Пользователь {chat_id} пытался добавить существующий тег {new_tag}')
return
USERS[chat_id]['tags'].append(new_tag)
save_config()
bot.reply_to(message, f"Тег {new_tag} успешно добавлен!")
log_info(f'Пользователь {chat_id} добавил новый тег {new_tag}')
def save_config():
"""Сохраняет текущую конфигурацию в файл."""
try:
config_data = {
"user1": USERS[config['user1']['chat_id']],
"user2": USERS[config['user2']['chat_id']]
}
with open('config.json', 'w', encoding='utf-8') as f:
json.dump(config_data, f, indent=4, ensure_ascii=False)
log_info('Конфигурация успешно сохранена')
except Exception as e:
log_error(f'Ошибка при сохранении конфигурации: {e}')
@bot.message_handler(content_types=['text', 'photo', 'audio', 'video', 'document'])
def handle_messages(message):
chat_id = message.chat.id
if chat_id not in USERS:
log_error(f'Сообщение с неавторизованного чата {chat_id}')
return
user_config = USERS[chat_id]
start_time = datetime.datetime.now()
caption = None # Инициализируем caption значением по умолчанию
try:
# Обработка текста сообщения
if message.html_text:
text = message.html_text
caption = html_to_markdown(text)
with open(f're_{chat_id}.txt', 'w', encoding='utf-8') as f:
f.write(text)
if len(text) == 43:
bot.delete_message(chat_id, last_messages[chat_id])
bot.delete_message(chat_id, message.message_id)
log_info(f'Удалено сообщение с длиной 43 для пользователя {chat_id}')
return
# Обработка медиафайлов с подписями
if message.photo:
file_id = message.photo[-1].file_id
download_file(file_id, f'{chat_id}_{file_id}.jpg')
caption = caption or message.html_caption
if message.html_caption:
caption = html_to_markdown(message.html_caption)
log_info(f"Фото сохранено для пользователя {chat_id}")
print(f"Фото сохранено для пользователя {chat_id}!\n{caption}")
if message.audio:
file_id = message.audio.file_id
download_file(file_id, f'{chat_id}_{file_id}.mp3')
caption = caption or message.html_caption
if message.html_caption:
caption = html_to_markdown(message.html_caption)
log_info(f"Аудио сохранено для пользователя {chat_id}")
print(f"Аудио сохранено для пользователя {chat_id}!\n{caption}")
if message.video:
file_id = message.video.file_id
download_file(file_id, f'{chat_id}_{file_id}.mp4')
caption = caption or message.html_caption
if message.html_caption:
caption = html_to_markdown(message.html_caption)
log_info(f"Видео сохранено для пользователя {chat_id}")
print(f"Видео сохранено для пользователя {chat_id}!\n{caption}")
if message.document:
file_id = message.document.file_id
download_file(file_id, f'{chat_id}_{message.document.file_name}')
caption = caption or message.html_caption
if message.html_caption:
caption = html_to_markdown(message.html_caption)
log_info(f"Файл {message.document.file_name} сохранен для пользователя {chat_id}")
print(f"Файл сохранен для пользователя {chat_id}!\n{caption}")
if not caption:
return
# Сохранение текста
with open(f'1_{chat_id}.txt', 'w', encoding='utf-8') as f:
f.write(caption)
log_info(f'Текст заметки сохранен для пользователя {chat_id}')
bot.reply_to(message, "Заметка сохранена!")
end_time_1 = datetime.datetime.now()
# Обработка AI
ai_instance = AI()
ai_instance.config = user_config
ai_instance.process_and_upload(ai=False)
log_info(f'AI обработка завершена для пользователя {chat_id}')
end_time_2 = datetime.datetime.now()
with open(f'last_{chat_id}.txt', 'w', encoding='utf-8') as f:
f.write('1')
sent = bot.send_message(
chat_id,
f"Время выполнения: {end_time_1 - start_time} | {end_time_2 - start_time}"
)
last_messages[chat_id] = sent.message_id
print(f'Готово для пользователя {chat_id}')
log_info(f'Обработка заметки завершена для пользователя {chat_id}. Время: {end_time_2 - start_time}')
# Обработка ответов на сообщения
if message.reply_to_message and message.reply_to_message.message_id == last_messages[chat_id]:
with open(f're_{chat_id}.txt', 'w', encoding='utf-8') as f:
f.write(message.text)
log_info(f'Получен и сохранен ответ от пользователя {chat_id}')
print(f"Reply получен и сохранен в re_{chat_id}.txt: {message.text}")
except Exception as e:
error_msg = f"Произошла ошибка для пользователя {chat_id}: {e}"
log_error(error_msg)
print(error_msg)
bot.reply_to(message, f"Произошла ошибка! {e}")
def check_send_file():
"""Проверяет send.txt каждые 5 секунд и отправляет содержимое."""
log_info('Запущен мониторинг файлов send.txt')
while True:
for chat_id in USERS.keys():
try:
filename = f'send_{chat_id}.txt'
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as f:
message_text = f.read().strip()
if message_text:
if message_text == 'code':
bot.delete_message(chat_id, last_messages[chat_id])
bot.send_message(chat_id, 'Код получен!')
log_info(f'Отправлено подтверждение кода пользователю {chat_id}')
else:
sent = bot.send_message(chat_id, message_text)
last_messages[chat_id] = sent.message_id
log_info(f'Отправлено сообщение пользователю {chat_id}')
with open(filename, 'w', encoding='utf-8') as f:
f.write("")
except Exception as e:
log_error(f"Ошибка при проверке файла send для пользователя {chat_id}: {e}")
print(f"Error checking send file for user {chat_id}: {e}")
time.sleep(1)
venv_python = os.path.join("venv", "bin", "python3")
if os.path.exists(venv_python):
subprocess.Popen([venv_python, 'pooling.py'])
else:
print('venv не найден')
log_info('Запущен скрипт pooling.py')
import threading
threading.Thread(target=check_send_file, daemon=True).start()
log_info('Запущен поток мониторинга файлов')
while True:
try:
print('Запуск обсидиана...')
log_info('Запуск цикла обработки сообщений бота')
bot.polling(none_stop=True)
except Exception as e:
log_error(f'Ошибка в основном цикле бота: {e}')
pass