Repository navigation
Expand file tree
/
Copy pathbot.php
More file actions
293 lines (249 loc) · 12.6 KB
/
Copy pathbot.php
File metadata and controls
293 lines (249 loc) · 12.6 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
<?php
include "config.php";
include "include/database.php";
include "include/api.php";
include "include/logger.php";
include "include/telegram.php";
include "include/image.php";
include "include/help.php";
include "include/ai.php";
include "include/moderation.php";
include "include/orders.php";
include "include/events.php";
include "include/dispatcher.php";
include "include/message.php";
$db = new SQLite3(DB_FILE);
initDatabase();
$update = json_decode(file_get_contents('php://input'), true);
file_put_contents(logPath('debug'), print_r($update, true) . "\n\n", FILE_APPEND);
// Rispondi subito 200 a Telegram per evitare timeout e retry
http_response_code(200);
header('Connection: close');
header('Content-Length: 0');
ob_end_flush();
flush();
// Se disponibile, chiudi la connessione FastCGI e continua in background
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
}
////////////////////////////////////////////////////////////////////////
////////////////////// Aggiornamento della chat ///////////////////
////////////////////////////////////////////////////////////////////////
if (isset($update['message'])) {
$message = $update['message'];
$groupId = $message['chat']['id'];
$userName = $message['from']['first_name'] ?? 'Utente';
$userId = $message['from']['id'] ?? null;
$chatType = $message['chat']['type'];
// Gestisci testo
$messageText = $message['text'] ?? '';
$messageText = str_replace('@rootbotbot', '', $messageText);
$messageText = str_replace('@rootbot', '', $messageText);
$messageText = str_replace('@bot', '', $messageText);
$messageText = str_replace('@root', '', $messageText);
// Skip vision quando qualcuno in chat sta caricando immagini menu pappatoia:
// in quel flusso l'analisi è lavoro amministrativo e la vision (3-10s via QBert)
// ritardava inutilmente il feedback "immagine salvata".
// Gli stati pappatoia in orders.php sono chat-wide (senza user_id), quindi
// filtro solo per chat_id — diversamente da getUserState() che vuole user_id.
$skipVision = false;
$stateStmt = $db->prepare("SELECT 1 FROM user_states WHERE chat_id = :chat_id AND state IN ('waiting_images', 'waiting_menu_images') LIMIT 1");
$stateStmt->bindValue(':chat_id', $groupId, SQLITE3_INTEGER);
if ($stateStmt->execute()->fetchArray(SQLITE3_ASSOC)) {
$skipVision = true;
}
// PRIMA: Gestisci immagini - analizza e salva nel contesto PRIMA di processMessage
// Così l'AI avrà il contesto dell'immagine quando risponde
$botImageAnalysis = null; // Analisi del bot da salvare separatamente
if (isset($message['photo'])) {
$photos = $message['photo'];
$fileId = $photos[count($photos) - 1]['file_id']; // Prendi la versione più grande
$caption = $message['caption'] ?? '';
// Analizza l'immagine con AI vision (passa anche la caption per contesto)
$imageDescription = $skipVision ? null : analyzeImage($fileId, $caption, $groupId);
// Messaggio utente: ha condiviso un'immagine (+ eventuale caption)
if ($caption) {
$messageText = "[ha condiviso un'immagine] $caption";
} else {
$messageText = "[ha condiviso un'immagine]";
}
if ($imageDescription) {
// Salva l'analisi come messaggio separato del bot
$botImageAnalysis = "[analisi immagine: $imageDescription]";
// Salva file_id per ri-analisi futura
saveImageLog($groupId, $userId, $userName, $fileId, $imageDescription);
// Controlla se la caption menziona il bot
$captionMentionsBot = !empty($caption) && (
preg_match('/@rootbotbot\b/', $caption) ||
preg_match('/@rootbot\b/', $caption) ||
preg_match('/@root\b/', $caption) ||
preg_match('/@bot\b/', $caption) ||
preg_match('/\brootbotbot\b/i', $caption) ||
preg_match('/\brootbot\b/i', $caption)
);
// Rispondi con l'analisi in chat privata o quando menzionato nel gruppo
if ($chatType == 'private' || $captionMentionsBot) {
sendTelegramMessage($groupId, $imageDescription, [
'reply_to_message_id' => $message['message_id'],
]);
}
}
}
// Gestisci documenti immagine (inviati senza compressione)
if (isset($message['document'])) {
file_put_contents(logPath('debug'), "=== DOCUMENT DETECTED ===\n", FILE_APPEND);
file_put_contents(logPath('debug'), "mime=" . ($message['document']['mime_type'] ?? 'none') . "\n", FILE_APPEND);
file_put_contents(logPath('debug'), "isImageDocument=" . (isImageDocument($message['document']) ? 'YES' : 'NO') . "\n", FILE_APPEND);
}
if (isset($message['document']) && isImageDocument($message['document'])) {
$fileId = $message['document']['file_id'];
$caption = $message['caption'] ?? '';
file_put_contents(logPath('debug'), "Processing document image, caption=" . substr($caption, 0, 50) . "\n", FILE_APPEND);
$imageDescription = $skipVision ? null : analyzeImage($fileId, $caption, $groupId);
file_put_contents(logPath('debug'), "analyzeImage returned: " . ($imageDescription ? "OK (" . strlen($imageDescription) . " chars)" : ($skipVision ? "SKIPPED (menu upload state)" : "NULL")) . "\n", FILE_APPEND);
// Messaggio utente: ha condiviso un'immagine (+ eventuale caption)
if ($caption) {
$messageText = "[ha condiviso un'immagine] $caption";
} else {
$messageText = "[ha condiviso un'immagine]";
}
if ($imageDescription) {
// Salva l'analisi come messaggio separato del bot
$botImageAnalysis = "[analisi immagine: $imageDescription]";
// Salva file_id per ri-analisi futura
saveImageLog($groupId, $userId, $userName, $fileId, $imageDescription);
$captionMentionsBot = !empty($caption) && (
preg_match('/@rootbotbot\b/', $caption) ||
preg_match('/@rootbot\b/', $caption) ||
preg_match('/@root\b/', $caption) ||
preg_match('/@bot\b/', $caption) ||
preg_match('/\brootbotbot\b/i', $caption) ||
preg_match('/\brootbot\b/i', $caption)
);
if ($chatType == 'private' || $captionMentionsBot) {
sendTelegramMessage($groupId, $imageDescription, [
'reply_to_message_id' => $message['message_id'],
]);
}
}
}
// Estrai reply_to_user_id se il messaggio è una risposta a un altro utente
$replyToUserId = null;
if (isset($message['reply_to_message']['from']['id'])) {
$replyToUserId = (int)$message['reply_to_message']['from']['id'];
}
// Salva nel contesto PRIMA di processMessage (così l'AI ha il contesto aggiornato)
if (!empty(trim($messageText))) {
saveMessageToContext($groupId, $userName, $messageText, $userId, $replyToUserId);
}
// Salva l'analisi immagine come messaggio separato del bot
if (!empty($botImageAnalysis)) {
saveMessageToContext($groupId, 'rootbot', $botImageAnalysis);
}
// Prima di processare, marca come "in elaborazione" se il messaggio invoca il bot
// (per evitare doppie risposte se il messaggio viene editato durante l'elaborazione)
$textToCheck = $message['text'] ?? $message['caption'] ?? '';
$mentionsBot = preg_match('/@rootbotbot\b/', $textToCheck) ||
preg_match('/@rootbot\b/', $textToCheck) ||
preg_match('/@root\b/', $textToCheck) ||
preg_match('/@bot\b/', $textToCheck) ||
preg_match('/\brootbotbot\b/i', $textToCheck) ||
preg_match('/\brootbot\b/i', $textToCheck) ||
$chatType == 'private';
if ($mentionsBot) {
markAsReplied($groupId, $message['message_id']);
}
// POI: Processa il messaggio (comandi, AI, ecc.)
// Le reply a un'immagine col bot menzionato sono instradate dal dispatcher
// come qualsiasi altro messaggio: gli agenti che accettano foto in input
// (image_query, 3d_gen, ...) leggono il file_id da ctx['raw']['reply_to_message'].
processMessage($message);
} elseif (isset($update['edited_message'])) {
// Gestisce i messaggi editati solo se menzionano il bot (o chat privata) e non abbiamo già risposto
$editedMessage = $update['edited_message'];
$chatId = $editedMessage['chat']['id'];
$messageId = $editedMessage['message_id'];
$chatType = $editedMessage['chat']['type'];
$text = $editedMessage['text'] ?? $editedMessage['caption'] ?? '';
// Controlla se il messaggio menziona il bot
$mentionsBot = preg_match('/@rootbotbot\b/', $text) ||
preg_match('/@rootbot\b/', $text) ||
preg_match('/@root\b/', $text) ||
preg_match('/@bot\b/', $text) ||
preg_match('/\brootbotbot\b/i', $text) ||
preg_match('/\brootbot\b/i', $text);
// Processa solo se: (menziona il bot O è chat privata) E non abbiamo già risposto
if (($mentionsBot || $chatType == 'private') && !hasAlreadyReplied($chatId, $messageId)) {
// Marca subito come "in elaborazione" per evitare race condition
if (markAsReplied($chatId, $messageId)) {
// Salva nel contesto e processa come un messaggio normale
$userName = $editedMessage['from']['first_name'] ?? 'Utente';
$userId = $editedMessage['from']['id'] ?? null;
$messageText = str_replace('@rootbotbot', '', $text);
$messageText = str_replace('@rootbot', '', $messageText);
$messageText = str_replace('@bot', '', $messageText);
$messageText = str_replace('@root', '', $messageText);
$editReplyToUserId = isset($editedMessage['reply_to_message']['from']['id'])
? (int)$editedMessage['reply_to_message']['from']['id'] : null;
if (!empty(trim($messageText))) {
saveMessageToContext($chatId, $userName, $messageText, $userId, $editReplyToUserId);
}
processMessage($editedMessage);
}
}
} elseif (isset($update['callback_query'])) {
$callbackQuery = $update['callback_query'];
$data = $callbackQuery['data'];
// Callback ordini/pappatoie
if (strpos($data, 'seleziona_pappatoia:') === 0) {
gestisci_selezione_pappatoia($callbackQuery);
} elseif (strpos($data, 'delete_pappatoia:') === 0) {
handle_delete_pappatoia($callbackQuery);
} elseif (strpos($data, 'confirm_delete_pappatoia:') === 0) {
confirm_delete_pappatoia($callbackQuery);
} elseif ($data === 'cancel_delete_pappatoia') {
cancel_delete_pappatoia($callbackQuery);
} elseif (strpos($data, 'show_pappatoia_images:') === 0) {
handle_show_pappatoia_images($callbackQuery);
} elseif (strpos($data, 'select_pappatoia_for_menu:') === 0) {
handle_select_pappatoia_for_menu($callbackQuery);
} elseif (strpos($data, 'menu_action:') === 0) {
handle_menu_action($callbackQuery);
}
// Callback eventi
elseif (strpos($data, 'partecipo_evento:') === 0) {
handlePartecipoEvento($callbackQuery);
} elseif (strpos($data, 'lista_partecipanti:') === 0) {
handleListaPartecipanti($callbackQuery);
} elseif (strpos($data, 'annullo_tipo:') === 0) {
handleAnnulloTipo($callbackQuery);
} elseif (strpos($data, 'modifica_evento_select:') === 0) {
handleModificaEventoSelect($callbackQuery);
} elseif (strpos($data, 'modifica_campo:') === 0) {
handleModificaCampo($callbackQuery);
} elseif (strpos($data, 'chiudi_evento_select:') === 0) {
handleChiudiEventoSelect($callbackQuery);
} elseif (strpos($data, 'conferma_chiudi_evento:') === 0) {
handleConfermaChiudiEvento($callbackQuery);
} elseif ($data === 'annulla_chiudi_evento') {
handleAnnullaChiudiEvento($callbackQuery);
} elseif (strpos($data, 'ospite_evento:') === 0) {
handleOspiteEvento($callbackQuery);
} elseif (strpos($data, 'annullo_ospite:') === 0) {
handleAnnulloOspiteCallback($callbackQuery);
}
// Callback TTS
elseif ($data === 'tts') {
handleTTSCallback($callbackQuery);
} elseif ($data === 'tts_generating') {
makeAPIRequest('answerCallbackQuery', [
'callback_query_id' => $callbackQuery['id'],
'text' => 'Generazione già in corso, attendi...',
'show_alert' => false
]);
}
} elseif (isset($update['poll_answer'])) {
// Gestione risposte ai quiz
handlePollAnswer($update['poll_answer']);
}
?>