Skip to content

Commit

Permalink
Merge pull request #14 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 33222c1 + 7ec24c0 commit 37ead57
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 31 deletions.
83 changes: 55 additions & 28 deletions app/Console/Commands/SendMessagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ public function handle()

$user = User::find($disparos->user_id);

echo "Disparo: {$disparos->id} - User: {$user->name}\n";

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

// dd($devicesOnline);

if (count($devicesOnline) == 0) {
echo "No devices online for user {$user->name}\n";
continue;
Expand All @@ -55,6 +59,8 @@ public function handle()

foreach ($messagesPending as $message) {

$qt_disparo++;

if ($disparos->status == 'paused') {
echo "Dispatch paused, waiting for user action\n";
break;
Expand Down Expand Up @@ -84,26 +90,28 @@ public function handle()
break;
}

$qt_disparo++;
if($message->template->type == 'text') {

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

if ($qt_disparo >= $random) {
echo "Limit of {$random} messages reached, waiting {$sleep} seconds\n";
$qt_disparo = 0;
sleep($sleep);
}
$sendText = Service::WhatsApp("sendText", [
"Bearer" => $token,
"DeviceToken" => $randomDevice->device_token,
"body" => [
"number" => $message->contato->number,
"text" => $messageParsed
]
]);

if($message->template->type == 'text') {
} catch (\GuzzleHttp\Exception\RequestException $th) {

echo "Erro ao enviar mensagem: " . $th->getMessage() . "\n";
$disparos->status = 'canceled';
$disparos->save();

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

}

if (!isset($sendText->response->result) or $sendText->response->result != 200) {
$message->status = 'error';
Expand All @@ -120,20 +128,31 @@ public function handle()

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

$sendFile = Service::WhatsApp("sendFile", [
"Bearer" => $token,
"DeviceToken" => $randomDevice->device_token,
"body" => [
"number" => $message->contato->number,
"path" => $message->template->path,
"options" => [
"caption" => $messageParsed,
"createChat" > true,
"filename" => basename($message->template->path)
try {

$sendFile = Service::WhatsApp("sendFile", [
"Bearer" => $token,
"DeviceToken" => $randomDevice->device_token,
"body" => [
"number" => $message->contato->number,
"path" => $message->template->path,
"options" => [
"caption" => $messageParsed,
"createChat" > true,
"filename" => basename($message->template->path)
]
]
]
]);
]);

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

echo "Erro ao enviar mensagem: " . $th->getMessage() . "\n";
$disparos->status = 'cancelled';
$disparos->save();

return;

}
if (!isset($sendFile->response->result) or $sendFile->response->result != 200) {
$message->status = 'error';
$message->log = json_encode($sendFile);
Expand All @@ -148,6 +167,14 @@ public function handle()
$message->save();

}

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

if ($qt_disparo >= $random) {
echo "Limit of {$random} messages reached, waiting {$sleep} seconds\n";
$qt_disparo = 0;
sleep($sleep);
}

}

Expand Down
11 changes: 8 additions & 3 deletions routes/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;

Artisan::command('inspire', function () {
$this->comment(Inspiring::quote());
})->purpose('Display an inspiring quote')->hourly();
// Artisan::command('inspire', function () {
// $this->comment(Inspiring::quote());
// })->purpose('Display an inspiring quote')->hourly();

//run php artisan app:send-messages-command
// Artisan::command('app:send-messages-command', function () {
// $this->info('Messages sent successfully!');
// })->purpose('Send messages to users')->daily();

0 comments on commit 37ead57

Please sign in to comment.