Atualizacao
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
|
||||
use App\Models\turmas;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use App\Jobs\Envio_Mensagem_Whatsapp;
|
||||
|
||||
class EnvioWhatsap extends Controller
|
||||
{
|
||||
@@ -14,48 +15,17 @@ class EnvioWhatsap extends Controller
|
||||
'id_turma' => 'required|exists:turmas,id',
|
||||
'tipo_envio' => 'required|in:texto,imagem',
|
||||
'mensagem' => 'nullable|string',
|
||||
'imagem' => 'required_if:tipo_envio,imagem|image|mimes:jpeg,png,webp|max:2048',
|
||||
'imagem' => 'required_if:tipo_envio,imagem|image|mimes:jpeg,png,webp|max:8192',
|
||||
]);
|
||||
|
||||
$turma = turmas::findOrFail($request->id_turma);
|
||||
// 🔥 Apenas coleta os dados brutos
|
||||
Envio_Mensagem_Whatsapp::dispatch(
|
||||
$request->id_turma,
|
||||
$request->tipo_envio,
|
||||
$request->mensagem,
|
||||
$request->file('imagem')?->store('tmp') // salva temporariamente
|
||||
);
|
||||
|
||||
// Corrigir formatação para WhatsApp
|
||||
$mensagem = $request->mensagem ?? '';
|
||||
$mensagem = str_replace(['**', '__', '~~'], ['*', '_', '~'], $mensagem);
|
||||
|
||||
$payload = [
|
||||
'chatId' => $turma->id_whatsapp,
|
||||
'mensagem' => $mensagem,
|
||||
];
|
||||
|
||||
// ✅ Se for envio com imagem
|
||||
if ($request->tipo_envio === 'imagem' && $request->hasFile('imagem')) {
|
||||
|
||||
$file = $request->file('imagem');
|
||||
|
||||
$base64 = base64_encode(
|
||||
file_get_contents($file->getRealPath())
|
||||
);
|
||||
|
||||
$mime = $file->getMimeType(); // image/png, image/jpeg, etc
|
||||
|
||||
// 🔥 AQUI está o prefixo obrigatório
|
||||
$base64Completo = "data:$mime;base64,$base64";
|
||||
|
||||
$payload['imagem'] = $base64Completo;
|
||||
}
|
||||
|
||||
// ✅ Enviar para API
|
||||
$response = Http::withHeaders([
|
||||
'X-API-KEY' => 'UoCkSF4ApgADhpvDkkE5XO1fD761yOZX',
|
||||
'Content-Type' => 'application/json',
|
||||
])->post('https://n8n.cae.app.br/webhook/envioWpp', $payload);
|
||||
|
||||
// ✅ Retorno amigável
|
||||
if ($response->successful()) {
|
||||
return back()->with('success', 'Mensagem enviada com sucesso!');
|
||||
}
|
||||
|
||||
return back()->with('error', 'Erro ao enviar: ' . $response->body());
|
||||
return back()->with('success', 'Envio colocado na fila com sucesso!');
|
||||
}
|
||||
}
|
||||
|
||||
10
app/Http/Controllers/EnviosWppController.php
Normal file
10
app/Http/Controllers/EnviosWppController.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class EnviosWppController extends Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -49,4 +49,9 @@ class TurmasController extends Controller
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function detalhes ($id) {
|
||||
$turma = turmas::findOrFail($id);
|
||||
return view('escolas.turmas_detalhes', compact('turma'));
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
20
app/Models/envios_wpp.php
Normal file
20
app/Models/envios_wpp.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class envios_wpp extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'id',
|
||||
'status',
|
||||
'id_turma',
|
||||
'detalhes',
|
||||
'id_job',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'detalhes' => 'array',
|
||||
];
|
||||
}
|
||||
@@ -19,4 +19,8 @@ class turmas extends Model
|
||||
{
|
||||
return $this->hasMany(alunos::class, 'id', 'id_turma');
|
||||
}
|
||||
|
||||
public function enviosWpp() {
|
||||
return $this->hasMany(envios_wpp::class, 'id_turma', 'id');
|
||||
}
|
||||
}
|
||||
|
||||
36
app/Providers/HorizonServiceProvider.php
Normal file
36
app/Providers/HorizonServiceProvider.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Laravel\Horizon\Horizon;
|
||||
use Laravel\Horizon\HorizonApplicationServiceProvider;
|
||||
|
||||
class HorizonServiceProvider extends HorizonApplicationServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
// Horizon::routeSmsNotificationsTo('15556667777');
|
||||
// Horizon::routeMailNotificationsTo('example@example.com');
|
||||
// Horizon::routeSlackNotificationsTo('slack-webhook-url', '#channel');
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the Horizon gate.
|
||||
*
|
||||
* This gate determines who can access Horizon in non-local environments.
|
||||
*/
|
||||
protected function gate(): void
|
||||
{
|
||||
Gate::define('viewHorizon', function ($user = null) {
|
||||
return in_array(optional($user)->email, [
|
||||
//
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user