Atualizacao
This commit is contained in:
270
app/Jobs/Envio_Mensagem_Whatsapp.php
Normal file
270
app/Jobs/Envio_Mensagem_Whatsapp.php
Normal file
@@ -0,0 +1,270 @@
|
||||
<?php
|
||||
namespace App\Jobs;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\Models\turmas;
|
||||
use App\Models\envios_wpp;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Envio_Mensagem_Whatsapp implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public $tries = 3;
|
||||
public $timeout = 120;
|
||||
|
||||
public string $jobUuid;
|
||||
|
||||
protected $idTurma;
|
||||
protected $tipoEnvio;
|
||||
protected $mensagem;
|
||||
protected $caminhoImagem;
|
||||
|
||||
public function __construct($idTurma, $tipoEnvio, $mensagem = null, $caminhoImagem = null)
|
||||
{
|
||||
$this->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,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user