Skip to content

Commit

Permalink
Merge pull request #8 from APIBrasil/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
jhowbhz authored Aug 22, 2024
2 parents 8d19f7b + a6a381a commit 2a3e86c
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_TIMEZONE=UTC
APP_TIMEZONE=America/Sao_Paulo
APP_URL=http://localhost
API_URL=https://gateway.apibrasil.io/api

Expand Down
35 changes: 21 additions & 14 deletions app/Console/Commands/SendMessagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ public function handle()

// dd($user->bearer_token_api_brasil);
$token = $user->bearer_token_api_brasil;
$devices_online = Dispositivos::online($user->id);
$devicesOnline = Dispositivos::online($user->id);

if (count($devices_online) == 0) {
if (count($devicesOnline) == 0) {
echo "No devices online for user {$user->name}\n";
continue;
}

$random_device = $devices_online[array_rand($devices_online)];
$messagesPending = $disparos->messagesPending;

if (count($messagesPending) == 0) {
Expand All @@ -56,27 +55,37 @@ public function handle()

foreach ($messagesPending as $message) {

if ($disparos->status == 'paused') {
echo "Dispatch paused, waiting for user action\n";
break;
}

$messageParsed = $this->parseMessage($message);
$randomDevice = $devicesOnline[array_rand($devicesOnline)];

echo "Usando o dispositivo {$randomDevice->device_token}\n";

switch ($disparos->mode) {
case 'agressive':
$random = rand(1, 10);
$sleep = rand(1, 2);
$sleep = rand(10, 30);
break;
case 'normal':
$random = rand(1, 5);
$sleep = rand(5, 60);
$sleep = rand(60, 120);
break;
case 'slow':
$random = rand(1, 2);
$sleep = rand(60, 120);
$sleep = rand(120, 240);
break;
default:
$random = rand(1, 10);
$sleep = rand(1, 2);
$random = rand(1, 5);
$sleep = rand(60, 120);
break;
}

$qt_disparo++;

echo "Disparo {$qt_disparo} de {$message->contato->name}\n";

if ($qt_disparo >= $random) {
Expand All @@ -89,21 +98,19 @@ public function handle()

$sendText = Service::WhatsApp("sendText", [
"Bearer" => $token,
"DeviceToken" => $random_device->device_token,
"DeviceToken" => $randomDevice->device_token,
"body" => [
"number" => $message->contato->number,
"text" => $messageParsed
]
]);

if (!$sendText or !isset($sendText->response->result) or $sendText->response->result != 200) {
if (!isset($sendText->response->result) or $sendText->response->result != 200) {
$message->status = 'error';
$message->log = json_encode($sendText);
$message->save();
continue;
}

$qt_disparo++;

$message->status = 'sent';
$message->send_at = now();
Expand All @@ -115,7 +122,7 @@ public function handle()

$sendFile = Service::WhatsApp("sendFile", [
"Bearer" => $token,
"DeviceToken" => $random_device->device_token,
"DeviceToken" => $randomDevice->device_token,
"body" => [
"number" => $message->contato->number,
"path" => $message->template->path,
Expand All @@ -127,7 +134,7 @@ public function handle()
]
]);

if (!$sendFile or !isset($sendFile->response->result) or $sendFile->response->result != 200) {
if (!isset($sendFile->response->result) or $sendFile->response->result != 200) {
$message->status = 'error';
$message->log = json_encode($sendFile);
$message->save();
Expand Down
13 changes: 11 additions & 2 deletions app/Http/Controllers/Admin/DisparosController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ public function index()

public function datatables()
{

$disparos = Disparos::orderBy('id', 'desc')
->whereNotNull('templates_id')
->with('tag')
->with('templates')
->withCount('messagesPending')
->withCount('messagesSent')
->where('user_id', Auth::user()->id)
->get();

$disparos->each(function($disparo) {
$templateIds = explode(',', $disparo->templates_id);
$disparo->templates = Templates::whereIn('id', $templateIds)->get();
});

return DataTables::of($disparos)->make(true);

Expand Down Expand Up @@ -138,6 +142,11 @@ public function destroy(string $id)
{
try {

// Delete all messages
Mensagens::where('disparo_id', $id)
->where('user_id', Auth::user()->id)
->delete();

$disparo = Disparos::where('id', $id)
->where('user_id', Auth::user()->id)
->first();
Expand Down
22 changes: 11 additions & 11 deletions app/Models/Disparos.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ public function messagesSent()
}

public function getTemplates()
{
$templates = explode(',', $this->templates_id);
$templates_name = [];
{
// Check if templates_id is null or empty
if (is_null($this->templates_id) || trim($this->templates_id) === '') {
return collect([]);
}

foreach ($templates as $template_id) {
$template = Templates::find(trim($template_id));
// Split the comma-separated IDs into an array
$templateIds = explode(',', $this->templates_id);

if ($template) {
$templates_name[] = $template->name;
}
}
// Fetch the templates matching the IDs
$templates = Templates::whereIn('id', $templateIds)->get();

return implode(', ', $templates_name);
}
return $templates;
}

}
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
|
*/

'timezone' => env('APP_TIMEZONE', 'UTC'),
'timezone' => env('APP_TIMEZONE', 'America/Sao_Paulo'),

/*
|--------------------------------------------------------------------------
Expand Down
31 changes: 28 additions & 3 deletions resources/views/admin/disparos.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
{ data: 'messages_pending_count', name: 'messages_pending_count' },
{ data: 'messages_sent_count', name: 'messages_sent_count' },
{ data: 'mode', name: 'mode' },
{ data: 'status', name: 'status' },
{ data: 'actions', name: 'actions' },
],
Expand All @@ -177,9 +178,19 @@
{
targets: 3,
render: function (data, type, row) {
return data.map(template => {
return `<span class="badge bg-primary">${template.name}</span>`;
}).join(' ');
let templates = row.templates;
let template = '';
for (let i = 0; i < templates.length; i++) {
template += `<span class="badge bg-primary">${templates[i].name}</span></br>`;
}
return template;
// return data.map(template => {
// return `<span class="badge bg-primary">${template.name ?? ''}</span>`;
// }).join(' ');
},
},
Expand All @@ -196,8 +207,22 @@
},
},
//status
{
targets: 7,
render: function (data, type, row) {
if (data == 'pending') {
return `<span class="badge bg-warning">Pendente</span>`;
} else if (data == 'paused') {
return `<span class="badge bg-secondary">Pausado</span>`;
} else {
return `<span class="badge bg-success">Finalizado</span>`;
}
},
},
{
targets: 8,
render: function (data, type, row) {
return `<button class="btn btn-sm btn-primary" onclick="getItems(${row.id})"><i class="fas fa-edit"></i></button>
<button class="btn btn-sm btn-danger" onclick="deleteItem(${row.id})"><i class="fas fa-trash"></i></button>`;
Expand Down
2 changes: 1 addition & 1 deletion resources/views/admin/dispositivos.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
socket.on(`${device_token}`, (events) => {
document.getElementById('message').innerHTML = events?.data?.message?.message ? events.data.message.message : 'Aguarde...';
document.getElementById('message').innerHTML = events.data ? events.data.message.message : 'Aguarde...';
if (events.data.wook == 'QRCODE') {
Expand Down
10 changes: 9 additions & 1 deletion resources/views/admin/historico.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<tr>
<th scope="col">Destino</th>
<th scope="col">Tag</th>
<th scope="col">Mensagem</th>
<th scope="col">Template</th>
<th scope="col">Criado em</th>
<th scope="col">Enviado em</th>
<th scope="col">Status</th>
Expand Down Expand Up @@ -125,6 +125,14 @@
return `<span class="badge" style="background-color: ${row.tag.color}"> ${row.tag.name}</span>`;
},
},
{
targets: 2,
render: function (data, type, row) {
return `<div class="badge bg-secondary">${row.template.name}</div>`;
}
},
{
targets: 3,
render: function (data, type, row) {
Expand Down

0 comments on commit 2a3e86c

Please sign in to comment.