<?php
namespace App\Console\Commands;
use Throwable;
use Carbon\Carbon;
use App\Models\User;
use App\Models\Country;
use App\Models\Reports;
use App\Models\SpamWord;
use App\Models\SendingToken;
use App\Models\SendingSetting;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Models\SendingServerCoverage;
use Illuminate\Support\Str;
class PusherWalongDny extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'pusher:walongdny';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
private function sendCallBack($uid,$userId,$recipient,$status,$statusdesc)
{
$callBack = User::where('id',$userId)->first();
if (isset($callBack->url_dlr)) {
$dataCallBack = [
'uid' => $uid,
'recipient' => $recipient,
'status' => $status,
'statusdesc' => $statusdesc
];
$dataCallBack = json_encode($dataCallBack);
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $callBack->url_dlr);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $dataCallBack);
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_exec($curlHandle);
curl_close($curlHandle);
}
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$walongSender = SendingToken::where('token_status_old','UP')
->where('token_group', 1)
->get();
if ($walongSender->isNotEmpty()) {
for ($i=1; $i<=30; $i++) {
$walongInQueue = Reports::select('id')
->where('sms_type','whatsapp')
->where('status','InQueue')
->orderby('created_at','asc')
->get();
if ($walongInQueue->isNotEmpty()) {
$walongProcessingId = substr(str_shuffle(MD5(microtime())),0,10);
Reports::where('sms_type','whatsapp')->where('status','InQueue')->orderby('priority','asc')->limit(1)
->update(['status' => 'Processing', 'processing_uid' => $walongProcessingId]);
$walongProcessingDatas = Reports::where('processing_uid', $walongProcessingId)->orderby('created_at','asc')->get();
foreach ($walongProcessingDatas as $walongProcessingData) {
$countryCodes = Country::all();
$number = $walongProcessingData->to;
$data_array = array();
foreach ($countryCodes as $countryCode) {
$data_array[$countryCode->country_code] = $countryCode->country_code;
}
$countrys = $data_array;
$i = 4;
$country = "";
while ($i > 0) {
if (isset($countrys[substr($number, 0, $i)])) {
$country = $countrys[substr($number, 0, $i)];
break;
}
else {
$i--;
}
}
$user = User::find($walongProcessingData->user_id);
if ($user->sms_unit > 0) {
$waSenderTurn = SendingToken::where('token_status_old', 'UP')
->where('token_group', 1)
->orderby('updated_at','asc')
->first();
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $waSenderTurn->send_url.'/api/isRegisteredNumber',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => "apiKey=".$waSenderTurn->token_device_number."&phone=".$walongProcessingData->to,
));
$response = curl_exec($curl);
curl_close($curl);
$response_json = json_decode($response);
if ($response_json->code == 400) {
if (isset($response_json->results->state)) {
SendingToken::where('token_device_number', $waSenderTurn->token_device_number)->update(['token_status_old' => 'DO']);
Reports::where('id', $walongProcessingData->id)->update(['status' => 'InQueue']);
continue;
}
else {
$uid = $walongProcessingData->uid;
$userId = $walongProcessingData->user_id;
$recipient = $walongProcessingData->to;
$status = 'Undelivered';
$statusdesc = 'not active wa';
$this->sendCallBack($uid, $userId, $recipient, $status, $statusdesc);
Reports::where('id', $walongProcessingData->id)
->update([
'status' => $status,
'statusdesc' => $statusdesc,
'send_by_device' => $waSenderTurn->token_device_id,
'send_by_vendor' => $waSenderTurn->vendor_id,
'sent_at' => now(),
'dr_at' => now(),
'updated_at' => now()
]);
continue;
}
}
if ($user->need_check_bad_words == 1) {
$message = "Halo, ini adalah TESTING Kirim Whatsapp. Pesan anda adalah :\n\n".$walongProcessingData->message."\n\nWaspada Penipuan !!!";
} else {
$message = $walongProcessingData->message;
}
if ($user->need_unique_char == 1) {
$message = $message."\n\n".substr(str_shuffle(MD5(microtime())),0,16);
}
$cost = SendingServerCoverage::where('country_id',$country)
->where('server_id',$walongProcessingData->sending_server_id)
->where('user_id', $walongProcessingData->user_id)
->first();
if (isset($cost->price_wa)) {
$cost = $cost->price_wa;
}
else {
$cost = SendingServerCoverage::where('country_id',$country)
->where('server_id',$walongProcessingData->sending_server_id)
->where('currency',$user->currency_text)
->first();
if (isset($cost->price_wa)){
$cost = $cost->price_wa;
} else {
$uid = $walongProcessingData->uid;
$userId = $walongProcessingData->user_id;
$recipient = $walongProcessingData->to;
$status = 'Undelivered';
$statusdesc = 'unknown country dest';
$this->sendCallBack($uid, $userId, $recipient, $status, $statusdesc);
Reports::where('id', $walongProcessingData->id)
->update([
'status' => $status,
'statusdesc' => $statusdesc,
'updated_at' => now()
]);
continue;
}
}
try {
DB::beginTransaction();
$dnyWalongSender = SendingToken::where('token_status_old','UP')
->where('token_group', 1)
->orderby('updated_at','asc')
->sharedLock()
->first();
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $dnyWalongSender->send_url."/api/sendMessage",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => "apiKey=".$dnyWalongSender->token_device_number."&phone=".$walongProcessingData->to."&message=".$message,
));
$response = curl_exec($curl);
curl_close($curl);
$response_json = json_decode($response);
if ($response_json->code == 200 ) {
$uid = $walongProcessingData->uid;
$userId = $walongProcessingData->user_id;
$recipient = $walongProcessingData->to;
$status = 'Sent';
$statusdesc = 'success sent';
$this->sendCallBack($uid, $userId, $recipient, $status, $statusdesc);
Reports::where('id', $walongProcessingData->id)
->update([
'status' => $status,
'status2' => $response_json->results->id_message,
'statusdesc' => $statusdesc,
'send_by_device' => $dnyWalongSender->token_device_id,
'send_by_vendor' => $dnyWalongSender->vendor_id,
'cost' => $cost,
'sent_at' => now()
]);
$dnyWalongSender->update(['updated_at' => now()]);
$user->decrement('sms_unit', $cost);
} else {
if (isset($response_json->results->state)) {
SendingToken::where('token_device_number', $dnyWalongSender->token_device_number)
->update(['token_status_old' => 'DO']);
Reports::where('id', $walongProcessingData->id)->update(['status' => 'InQueue']);
}
}
DB::commit();
} catch (\Throwable $th) {
DB::rollback();
Reports::where('id', $walongProcessingData->id)->update(['status' => 'InQueue']);
}
}
else {
$uid = $walongProcessingData->uid;
$userId = $walongProcessingData->user_id;
$$recipient = $walongProcessingData->to;
$status = 'Undelivered';
$statusdesc = 'not enough balance';
$this->sendCallBack($uid, $userId, $recipient, $status, $statusdesc);
Reports::where('uid', $walongProcessingData->uid)
->update([
'status' => $status,
'statusdesc' => $statusdesc,
]);
continue;
}
}
}
sleep(2);
}
}
}
}
Helo, Laravel 11 was deleted /app/Console/kernel.php file. If we using Laravel 11 version, we should put all of our command inside file routes/console.php
Laravel's command scheduler offers a fresh approach to managing scheduled tasks on your server. The scheduler allows you to fluently and expressively define your command schedule within your Laravel application itself.
In Development web or App, sometimes we need to check if record is exist in our record. To do this, we can execute php code like below