idTurma = $idTurma; $this->tipoEnvio = $tipoEnvio; $this->mensagem = $mensagem; $this->caminhoImagem = $caminhoImagem; $this->jobUuid = str_replace('-', '', Str::uuid()->toString()); } public function handle(): void { $turma = turmas::findOrFail($this->idTurma); $detalhes = []; // ✅ Corrigir formatação WhatsApp $mensagem = $this->mensagem ?? ''; $mensagem = str_replace(['**', '__', '~~'], ['*', '_', '~'], $mensagem); if ($this->tipoEnvio === 'imagem' && $this->caminhoImagem) { $conteudo = Storage::get($this->caminhoImagem); $mime = Storage::mimeType($this->caminhoImagem); $base64 = base64_encode($conteudo); $imagem = "data:$mime;base64,$base64"; // 🧹 Remove arquivo temporário Storage::delete($this->caminhoImagem); $existeImagem = true; } else { $existeImagem = false; } $detalhes['etapas'][] = [ 'tipo de evento' => $this->tipoEnvio, 'data' => now()->format('d-m-Y H:i:s'), 'mensagem' => $mensagem, 'imagem' => $existeImagem, ]; try { envios_wpp::updateOrCreate(['id_job' => $this->jobUuid], [ 'id_job' => $this->jobUuid, 'status' => 'Em andamento', 'id_turma' => $this->idTurma, 'detalhes' => [ 'tipo_de_evento' => $this->tipoEnvio, 'mensagem' => $detalhes, ] ]); } catch (\Throwable $th) { Log::error('Erro, segue: ', [ 'erro' => $th->getMessage() ]); envios_wpp::updateOrCreate(['id_job' => $this->jobUuid], [ 'id_job' => $this->jobUuid, 'status' => 'Erro', 'id_turma' => $this->idTurma, 'detalhes' => [ 'tipo_de_evento' => $this->tipoEnvio, 'mensagem' => $detalhes, 'Erro: ' => $th->getMessage(), ] ]); } switch ($this->tipoEnvio) { case 'texto': $response = Http::withHeaders([ 'accept' => 'application/json', 'token' => '3aCSVE4jS9h233mNBr1awz3DF0In8CFk', ]) ->post('https://waha.cae.app.br/chat/send/text', [ 'phone' => $turma->id_whatsapp, 'body' => $mensagem, ]); if ($response->json()['code'] == 200) { $detalhes['etapas'][] = [ 'tipo de evento' => $this->tipoEnvio, 'data' => now()->format('d-m-Y H:i:s'), 'mensagem' => 'Mensagem enviada com sucessl' ]; try { envios_wpp::updateOrCreate(['id_job' => $this->jobUuid], [ 'id_job' => $this->jobUuid, 'status' => 'Concluido', 'id_turma' => $this->idTurma, 'detalhes' => [ 'tipo_de_evento' => $this->tipoEnvio, 'mensagem' => $detalhes, ] ]); } catch (\Throwable $th) { Log::error('Erro, segue: ', [ 'erro' => $th->getMessage() ]); envios_wpp::updateOrCreate(['id_job' => $this->jobUuid], [ 'id_job' => $this->jobUuid, 'status' => 'Erro', 'id_turma' => $this->idTurma, 'detalhes' => [ 'tipo_de_evento' => $this->tipoEnvio, 'data' => now()->format('d-m-Y H:i:s'), 'Erro: ' => $th->getMessage(), ] ]); } } else { $detalhes['etapas'][] = [ 'tipo de evento' => $this->tipoEnvio, 'data' => now()->format('d-m-Y H:i:s'), 'mensagem' => 'Mensagem não enviada' ]; try { envios_wpp::updateOrCreate(['id_job' => $this->jobUuid], [ 'id_job' => $this->jobUuid, 'status' => 'Erro', 'id_turma' => $this->idTurma, 'detalhes' => [ 'tipo_de_evento' => $this->tipoEnvio, 'mensagem' => $detalhes, ] ]); } catch (\Throwable $th) { Log::error('Erro, segue: ', [ 'erro' => $th->getMessage() ]); envios_wpp::updateOrCreate(['id_job' => $this->jobUuid], [ 'id_job' => $this->jobUuid, 'status' => 'Erro', 'id_turma' => $this->idTurma, 'detalhes' => [ 'tipo_de_evento' => $this->tipoEnvio, 'data' => now()->format('d-m-Y H:i:s'), 'Erro: ' => $th->getMessage(), ] ]); } } break; case 'imagem'; $response = Http::withHeaders([ 'accept' => 'application/json', 'token' => '3aCSVE4jS9h233mNBr1awz3DF0In8CFk', ]) ->post('https://waha.cae.app.br/chat/send/image', [ 'phone' => $turma->id_whatsapp, 'caption' => $mensagem, 'image' => $imagem, ]); if ($response->json()['code'] == 200) { $detalhes['etapas'][] = [ 'tipo de evento' => $this->tipoEnvio, 'data' => now()->format('d-m-Y H:i:s'), 'mensagem' => 'Mensagem enviada com sucessl' ]; try { envios_wpp::updateOrCreate(['id_job' => $this->jobUuid], [ 'id_job' => $this->jobUuid, 'status' => 'Concluido', 'id_turma' => $this->idTurma, 'detalhes' => [ 'tipo_de_evento' => $this->tipoEnvio, 'mensagem' => $detalhes, ] ]); } catch (\Throwable $th) { Log::error('Erro, segue: ', [ 'erro' => $th->getMessage() ]); envios_wpp::updateOrCreate(['id_job' => $this->jobUuid], [ 'id_job' => $this->jobUuid, 'status' => 'Erro', 'id_turma' => $this->idTurma, 'detalhes' => [ 'tipo_de_evento' => $this->tipoEnvio, 'data' => now()->format('d-m-Y H:i:s'), 'Erro: ' => $th->getMessage(), ] ]); } } else { $detalhes['etapas'][] = [ 'tipo de evento' => $this->tipoEnvio, 'data' => now()->format('d-m-Y H:i:s'), 'mensagem' => 'Mensagem não enviada' ]; try { envios_wpp::updateOrCreate(['id_job' => $this->jobUuid], [ 'id_job' => $this->jobUuid, 'status' => 'Erro', 'id_turma' => $this->idTurma, 'detalhes' => [ 'tipo_de_evento' => $this->tipoEnvio, 'mensagem' => $detalhes, ] ]); } catch (\Throwable $th) { Log::error('Erro, segue: ', [ 'erro' => $th->getMessage() ]); envios_wpp::updateOrCreate(['id_job' => $this->jobUuid], [ 'id_job' => $this->jobUuid, 'status' => 'Erro', 'id_turma' => $this->idTurma, 'detalhes' => [ 'tipo_de_evento' => $this->tipoEnvio, 'data' => now()->format('d-m-Y H:i:s'), 'Erro: ' => $th->getMessage(), ] ]); } } break; default: # code... break; } } public function tags() { return [ 'turma: ' . $this->idTurma, 'tipo: ' . $this->tipoEnvio, 'jobId: ' . $this->jobUuid, ]; } }