קוד לאלפון אם זיהוי קולי
-
<?php // מדידת זמן התחלה $start_time = microtime(true); ob_start(); require __DIR__ . '/vendor/autoload.php'; use Google\Cloud\Speech\V1\SpeechClient; use Google\Cloud\Speech\V1\RecognitionConfig; use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding; use Google\Cloud\Speech\V1\RecognitionAudio; /* ---------------- פונקציית לוג ---------------- */ function write_log($message) { $timestamp = date("Y-m-d H:i:s"); file_put_contents('debug_log.txt', "[$timestamp] $message\n", FILE_APPEND); } write_log("--- תחילת ריצה חדשה ---"); /* ---------------- פונקציות עזר ---------------- */ function normalizeText($text) { $text = mb_strtolower($text, 'UTF-8'); $text = preg_replace('/[^\p{Hebrew}\p{Latin}\s]/u', '', $text); return trim(preg_replace('/\s+/', ' ', $text)); } function smartNameMatchScore($transcript, $nameTemplate) { $wordsTranscript = explode(' ', normalizeText($transcript)); $wordsTemplate = explode(' ', normalizeText($nameTemplate)); $totalScore = 0; foreach ($wordsTemplate as $wordTemplate) { $best = 0; foreach ($wordsTranscript as $wordTranscript) { similar_text($wordTemplate, $wordTranscript, $percent); if ($percent > $best) $best = $percent; } $totalScore += $best; } return $totalScore / max(count($wordsTemplate), 1); } /* ---------------- ניהול Session ---------------- */ $callId = $_GET['ApiCallId'] ?? 'unknown'; $callCacheDir = __DIR__ . '/call_sessions'; if (!is_dir($callCacheDir)) { if (@mkdir($callCacheDir, 0775, true)) { write_log("נוצרה תיקיית סשן: $callCacheDir"); } else { write_log("שגיאה ביצירת תיקיית סשן!"); } } $sessionFile = $callCacheDir . '/' . $callId . '.json'; /* ---------------- טיפול בהקשות ---------------- */ if (isset($_GET['MySelection'])) { $selection = $_GET['MySelection']; write_log("התקבלה בחירה מהמשתמש: $selection עבור שיחה: $callId"); $sessionData = file_exists($sessionFile) ? json_decode(file_get_contents($sessionFile), true) : []; $name = $sessionData['name'] ?? 'לא ידוע'; $phone = $sessionData['phone'] ?? ''; $moreinfo = $sessionData['moreinfo'] ?? ''; $sayText = "נמצאו הפרטים הבאים. שם: $name. טלפון: $phone. מידע נוסף: $moreinfo. לשמיעה חוזרת הקישו 1, לחיוג הקישו 2, לחיפוש חדש הקישו 3, לביטול וחזרה הקש 4."; ob_end_clean(); header('Content-Type: text/plain; charset=utf-8'); if ($selection === '1') { echo "read=t-$sayText=MySelection,no,1,1,7,no,no,no,,1.2.3.4,,,,,no"; } elseif ($selection === '2') { @unlink($sessionFile); echo 'go_to_folder=NIT'; } elseif ($selection === '3') { @unlink($sessionFile); echo 'go_to_folder=/15'; } elseif ($selection === '4') { @unlink($sessionFile); echo 'go_to_folder=/16/4'; } write_log("בוצע טיפול בבחירה והוחזרה פקודה."); exit; } /* ---------------- תמלול וחיפוש ---------------- */ $token = $_GET['token'] ?? ''; $rapi = $_GET['rapi'] ?? ''; $templateId = $_GET['templateId'] ?? ''; if (!$token || !$rapi) { write_log("מחסור בפרמטרים: token או rapi חסרים."); exit(); } write_log("מתחיל הורדת קובץ שמע..."); $downloadUrl = "https://www.call2all.co.il/ym/api/DownloadFile?token=" . urlencode($token) . "&path=ivr2:" . urlencode($rapi); $audioData = @file_get_contents($downloadUrl); if (!$audioData) { write_log("שגיאה: הורדת קובץ השמע נכשלה."); exit(); } write_log("קובץ שמע הורד בהצלחה. גודל: " . strlen($audioData) . " בייטים."); $transcriptText = ''; try { write_log("מתחבר לגוגל לתמלול (Timeout מוגדר ל-10 שניות)..."); $client = new SpeechClient([ 'credentials' => __DIR__ . '/key.json', 'transportConfig' => ['rest' => ['timeout' => 10000]] ]); $audio = (new RecognitionAudio())->setContent($audioData); $config = (new RecognitionConfig())->setEncoding(AudioEncoding::LINEAR16)->setLanguageCode('he-IL'); $response = $client->recognize($config, $audio); foreach ($response->getResults() as $result) { $alt = $result->getAlternatives(); if (!empty($alt[0])) $transcriptText .= $alt[0]->getTranscript() . " "; } write_log("תמלול הסתיים. טקסט שזוהה: " . ($transcriptText ?: "כלום")); } catch (Exception $e) { write_log("שגיאת תמלול: " . $e->getMessage()); ob_end_clean(); exit('go_to_folder=/95'); } finally { if (isset($client)) $client->close(); } $normalized = normalizeText($transcriptText); if (!$normalized) { write_log("לא נמצא טקסט נורמלי לעיבוד."); ob_end_clean(); exit('go_to_folder=/95'); } /* ---------------- Cache וחיפוש בתבנית ---------------- */ $peopleCacheDir = __DIR__ . '/people_cache'; if (!is_dir($peopleCacheDir)) @mkdir($peopleCacheDir, 0775, true); $peopleCacheFile = $peopleCacheDir . '/' . md5($normalized) . '.json'; $matchedEntry = null; if (file_exists($peopleCacheFile)) { write_log("נמצאה התאמה ב-Cache המקומי."); $matchedEntry = json_decode(file_get_contents($peopleCacheFile), true); } else { write_log("מחפש בתבנית ימות המשיח (ID: $templateId)..."); $templateUrl = "https://www.call2all.co.il/ym/api/GetTemplateEntries?token=" . urlencode($token) . "&templateId=" . urlencode($templateId); $templateData = json_decode(@file_get_contents($templateUrl), true); $bestScore = 0; if (isset($templateData['entries'])) { foreach ($templateData['entries'] as $entry) { $score = smartNameMatchScore($transcriptText, $entry['name'] ?? ''); if ($score > $bestScore) { $bestScore = $score; $matchedEntry = $entry; } } } if ($matchedEntry && $bestScore >= 75) { write_log("נמצאה התאמה בתבנית. ציון: $bestScore. שומר ל-Cache."); file_put_contents($peopleCacheFile, json_encode($matchedEntry, JSON_UNESCAPED_UNICODE)); } else { write_log("לא נמצאה התאמה מספקת בתבנית."); } } /* ---------------- פלט סופי ---------------- */ $total_execution_time = round(microtime(true) - $start_time, 3); write_log("סיום תהליך. זמן ביצוע כולל: $total_execution_time שניות."); ob_end_clean(); header('Content-Type: text/plain; charset=utf-8'); if ($matchedEntry) { file_put_contents($sessionFile, json_encode($matchedEntry, JSON_UNESCAPED_UNICODE)); $resName = $matchedEntry['name'] ?? 'לא ידוע'; $resPhone = $matchedEntry['phone'] ?? ''; $resInfo = $matchedEntry['moreinfo'] ?? 'אין מידע נוסף'; echo "read=t-שם: $resName. טלפון: $resPhone. כתובת: $resInfo. לשמיעה חוזרת הקישו 1, לחיוג הקישו 2, לחיפוש חדש הקישו 3, לביטול וחזרה הקש 4.=MySelection,no,1,1,7,no,no,no,,1.2.3.4,,,,,no"; } else { echo 'go_to_folder=/95'; } // הדפסת זמן הביצוע להצגה בדפדפן בלבד (לא יופיע בטלפון בגלל ה-ob_end_clean) echo "\n\n--- Debug Info (Browser Only) ---\nTotal Execution Time: $total_execution_time seconds";כתבתי את הקוד הזה בסיוע בינה
שאני מריץ מהדפדפן הוא מתמלל ומציג את המידע
שאני שולח מהמערכת זה מנתק את השיחה מי שיכול להסביר לי מה הבעיה אני יודה לו מאד -
@BEN-ZION כתבת ללוג?
-
@anti-malware לא הבנתי? מה התכוונת
-
@BEN-ZION תכתוב בקוד שלך כתיבה ללוג של כל הפעולות השגיאות והאזהרות והפלט.
תשאל את בינה -
@anti-malware כן שאלתי אותו והכל תקין
בסוף כתבתי קוד חדש והוא עובד לא יודע מה הבעיה בזה
תודה -
B BEN ZION סימן נושא זה כנפתר