%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/komfo908/.trash/
Upload File :
Create Path :
Current File : /home/komfo908/.trash/chat_audio_update.php

<?php
// Substitua pela sua chave da API OpenAI
$apiKey = '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';

if (
  $_SERVER['REQUEST_METHOD'] === 'POST' &&
  isset($_FILES['audio']) &&
  isset($_POST['nome']) &&
  isset($_POST['telefone'])
) {
  $nomeCliente = trim($_POST['nome']);
  $telefoneCliente = trim($_POST['telefone']);
  $audioFile = $_FILES['audio'];
  $audioPath = $audioFile['tmp_name'];
  $audioName = uniqid() . '-' . basename($audioFile['name']);

  // Caminho de upload e URL pública (ajuste para seu domínio)
  $uploadDir = __DIR__ . '/uploads/';
  $publicBaseUrl = 'https://seudominio.com/uploads/'; // substitua pelo seu domínio real
  $publicAudioUrl = $publicBaseUrl . $audioName;

  // Cria pasta de uploads se não existir
  if (!file_exists($uploadDir)) {
      mkdir($uploadDir, 0777, true);
  }

  // Move o arquivo
  move_uploaded_file($audioPath, $uploadDir . $audioName);

  // 1. Transcrição com Whisper
  $whisperCurl = curl_init();
  curl_setopt_array($whisperCurl, [
      CURLOPT_URL => 'https://api.openai.com/v1/audio/transcriptions',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_POST => true,
      CURLOPT_HTTPHEADER => [
          "Authorization: Bearer $apiKey"
      ],
      CURLOPT_POSTFIELDS => [
          'file' => new CURLFile($uploadDir . $audioName),
          'model' => 'whisper-1',
          'language' => 'pt'
      ]
  ]);
  $whisperResponse = curl_exec($whisperCurl);
  curl_close($whisperCurl);

  $transcription = json_decode($whisperResponse, true);
  $texto = $transcription['text'] ?? '';

  // 2. Envia texto para o ChatGPT (opcional)
  $chatData = [
      'model' => 'gpt-4',
      'messages' => [
          ['role' => 'user', 'content' => $texto]
      ]
  ];

  $chatCurl = curl_init('https://api.openai.com/v1/chat/completions');
  curl_setopt_array($chatCurl, [
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_POST => true,
      CURLOPT_HTTPHEADER => [
          'Content-Type: application/json',
          "Authorization: Bearer $apiKey"
      ],
      CURLOPT_POSTFIELDS => json_encode($chatData)
  ]);
  $chatResponse = curl_exec($chatCurl);
  curl_close($chatCurl);

  $chatResult = json_decode($chatResponse, true);
  $resposta = $chatResult['choices'][0]['message']['content'] ?? 'Erro ao gerar resposta.';

  // Retorno no formato BotConversa
  echo json_encode([
      'mensagem' => [
          'tipo' => 'audio',
          'arquivo_url' => $publicAudioUrl
      ],
      'cliente' => [
          'nome' => $nomeCliente,
          'telefone' => $telefoneCliente
      ],
      'transcricao' => $texto,
      'resposta' => $resposta
  ]);
} else {
  echo json_encode(['erro' => 'Envie um arquivo de áudio, nome e telefone via POST.']);
}
?>

Zerion Mini Shell 1.0