Skip to content

Commit

Permalink
Merge pull request #1 from APIBrasil/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
jhowbhz authored Aug 18, 2024
2 parents 16f483b + 7f766f4 commit 721f1bd
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 31 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Esse é o roadmap planejado para a plataforma
| OK | Editar templates |
| OK | Deletar templates |
| OK | Criar dispositivo |
| Em andamento | Editar dispositivo |
| OK | Editar dispositivo |
| Em andamento | Deletar dispositivo |
| Em andamento | Criar usuário |
| Em andamento | Editar usuário |
Expand All @@ -39,11 +39,17 @@ Dashboard
Uploads de contatos
![Contatos](screen-upload-contatos.png)

Cadastro de disparo
![Contatos](screen-novo-disparos.png)
Novo de disparo
![Novo de disparo](screen-novo-disparos.png)

Disparador
![Contatos](screen-disparos.png)
Disparando
![Disparador](screen-disparando.png)

Novo dispositivo
![Novo dispositivo](screen-novo-dispositivos.png)

Dispositivo
![Dispositivo](screen-dispositivos.png)

Historico
![Contatos](screen-historico.png)
83 changes: 69 additions & 14 deletions app/Console/Commands/SendMessagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,82 @@ public function handle()
sleep($sleep);
}

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

if (!$sendText or $sendText->response->result != 200) {
$message->status = 'error';
$message->save();
continue;
if($message->template->type == 'text') {
$sendText = Service::WhatsApp("sendText", [
"Bearer" => $token,
"DeviceToken" => $random_device->device_token,
"body" => [
"number" => $message->contato->number,
"text" => $message->template->text
]
]);

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

if($message->template->type == 'image') {

$sendFile = Service::WhatsApp("sendImage", [
"Bearer" => $token,
"DeviceToken" => $random_device->device_token,
"body" => [
"number" => $message->contato->number,
"path" => $message->template->path,
"caption" => $message->template->text
]
]);

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

}

if($message->template->type == 'file') {

$sendFile = Service::WhatsApp("sendFile", [
"Bearer" => $token,
"DeviceToken" => $random_device->device_token,
"body" => [
"number" => $message->contato->number,
"path" => $message->template->path,
]
]);

if($message->template->text){

sleep(3);

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

}

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

}

$qt_disparo++;

$message->status = 'sent';
$message->send_at = now();
$message->save();


}

Expand Down
74 changes: 63 additions & 11 deletions app/Http/Controllers/Admin/DispositivosController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace App\Http\Controllers\Admin;

use App\Models\API;
use ApiBrasil\Service;
use GuzzleHttp\Client;
use App\Models\Servidores;

use App\Models\Servidores;
use App\Models\Dispositivos;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
Expand All @@ -25,15 +26,12 @@ public function index()
return $dispositivo->type == 'cellphone' or $dispositivo->type == 'tablet';
});

//apis type whatsapp or baileys
$apis = array_filter($apis, function($api) {
// or $api->type == 'baileys'
return $api->type == 'whatsapp';
return $api->type == 'whatsapp' or $api->type == 'baileys';
});

//server type whatsapp
$servidores = array_filter($servidores, function($servidor) {
return $servidor->type == 'whatsapp';
return $servidor->type == 'whatsapp' or $servidor->type == 'baileys';
});

return view('admin.dispositivos')
Expand Down Expand Up @@ -61,9 +59,7 @@ public function store(Request $request)
'device_name' => $request->device_name,
'device_key' => $request->device_key,
'device_ip' => $request->device_ip,
'server_search' => $request->server_search,
'webhook_wh_message' => $request->webhook_wh_message,
'webhook_wh_status' => $request->webhook_wh_status
'server_search' => $request->server_search
]);

$request = new RequestGuzzle('POST', 'https://gateway.apibrasil.io/api/v2/devices/store', $headers, $body);
Expand All @@ -90,15 +86,71 @@ public function store(Request $request)
*/
public function show(string $id)
{
//
try {

$show = Service::Device("show", [
"Bearer" => Auth::user()->bearer_token_api_brasil,
"method" => "GET",
"body" => [
"search" => $id
]
]);

return response()->json($show);

} catch (\Throwable $th) {
return response()->json([
'error' => true,
'message' => $th->getMessage()
], 400);
}
}

/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
try {

$client = new Client(['verify' => false]);

$token = Auth::user()->bearer_token_api_brasil;

$headers = [
'Content-Type' => 'application/json',
'SecretKey' => $request->secretkey,
'Authorization' => "Bearer $token"
];

$body = json_encode([
'search' => $id,
'server_search' => $request->server_search,
'type' => 'cellphone',

'device_name' => $request->device_name,
'device_key' => $request->device_key,
'device_ip' => $request->device_ip

]);

$request = new RequestGuzzle('POST', 'https://gateway.apibrasil.io/api/v2/devices/search', $headers, $body);
$res = $client->sendAsync($request)->wait();

$response = json_decode($res->getBody()->getContents());

return response()->json($response);

} catch (\GuzzleHttp\Exception\RequestException $e) {

$errorAsString = $e->getResponse()->getBody()->getContents();

return response()->json([
'error' => true,
'message' => json_decode($errorAsString)
], 400);

}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function up(): void
$table->string('description')->nullable();
$table->text('text')->nullable();
$table->string('path')->nullable();
$table->string('type')->nullable();
$table->enum('type', ['text', 'image', 'file'])->default('text');

$table->enum('status', ['active', 'inactive'])->default('active');

Expand Down
89 changes: 89 additions & 0 deletions resources/views/admin/dispositivos.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
</td>

<td>
<a href="#" class="btn btn-sm btn-primary text-white" onclick="getItems('{{ $item->search }}')"><i class="fas fa-edit"></i></a>
<a href="#" class="btn btn-sm btn-primary text-white"><i class="fas fa-qrcode"></i></a>
<a href="#" class="btn btn-sm btn-danger text-white"><i class="fas fa-trash"></i></a>
</tr>
Expand Down Expand Up @@ -262,6 +263,94 @@
}
const getItems = (id) => {
fetch(`/dispositivos/${id}/show`)
.then(response => response.json())
.then(data => {
const myModalAlternative = new bootstrap.Modal('#modalItem', {
keyboard: false,
backdrop: 'static'
});
document.getElementById('modalItemLabel').innerHTML = `Editar item ${data.name}`;
document.getElementById('type').value = data.type;
document.getElementById('server_search').value = data.server_search;
document.getElementById('device_name').value = data.device_name;
document.getElementById('device_key').value = data.device_key;
document.getElementById('device_ip').value = data.device_ip;
document.getElementById('secretkey').value = data.service.search;
myModalAlternative.show();
document.querySelector('#modalItem .modal-footer button').setAttribute('onclick', `updateItem('${id}')`);
console.log(data);
});
}
const updateItem = async (id) => {
let _token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
const bodyData = JSON.stringify({
_token: _token,
device_name: document.getElementById('device_name').value,
device_key: document.getElementById('device_key').value,
device_ip: document.getElementById('device_ip').value,
type: document.getElementById('type').value,
server_search: document.getElementById('server_search').value,
secretkey: document.getElementById('secretkey').value,
});
fetch(`/dispositivos/${id}/update`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: bodyData
})
.then(response => response.json())
.then(data => {
if (data.error == true) {
Swal.fire({
title: 'Erro!',
text: 'Erro ao salvar item!',
icon: 'error',
confirmButtonText: 'Fechar',
});
} else {
const myModal = bootstrap.Modal.getInstance(document.getElementById('modalItem'));
myModal.hide();
location.reload();
}
})
.catch((error) => {
Swal.fire({
title: 'Erro!',
text: 'Erro ao salvar item!',
icon: 'error',
confirmButtonText: 'Fechar',
});
console.error('Error:', error);
});
}
</script>

@endsection
Expand Down
1 change: 1 addition & 0 deletions resources/views/admin/templates.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<label for="type" class="form-label">Tipo</label>
<select class="form-select" id="type" name="type" required>
<option value="text">Texto</option>
<option value="image">Imagem</option>
<option value="file">Arquivo</option>
</select>
</div>
Expand Down
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
Route::group(['prefix' => 'dispositivos'], function () {
Route::get('/', [DispositivosController::class, 'index'])->name('dispositivos.index');
Route::post('/store', [DispositivosController::class, 'store'])->name('dispositivos.store');
Route::get('/{id}/show', [DispositivosController::class, 'show'])->name('dispositivos.show');
Route::patch('/{id}/update', [DispositivosController::class, 'update'])->name('dispositivos.update');
});

Route::group(['prefix' => 'templates'], function () {
Expand Down
Binary file added screen-disparando.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screen-dispositivos.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screen-novo-dispositivos.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 721f1bd

Please sign in to comment.