Last active
July 4, 2025 16:59
-
-
Save kura1420/c61a300eef63bf609c559e684971f9b5 to your computer and use it in GitHub Desktop.
Laravel Blast Message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Jobs; | |
| use Illuminate\Bus\Queueable; | |
| use Illuminate\Contracts\Queue\ShouldQueue; | |
| use Illuminate\Foundation\Bus\Dispatchable; | |
| use Illuminate\Queue\InteractsWithQueue; | |
| use Illuminate\Queue\SerializesModels; | |
| use Telegram\Bot\Laravel\Facades\Telegram; | |
| class BlastMessageJob implements ShouldQueue | |
| { | |
| use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | |
| /** | |
| * Create a new job instance. | |
| */ | |
| public function __construct(public int $loop) | |
| { | |
| // | |
| } | |
| /** | |
| * Execute the job. | |
| */ | |
| public function handle(): void | |
| { | |
| // | |
| Telegram::sendMessage([ | |
| 'chat_id' => env('TELEGRAM_ID_PERSONAL'), | |
| 'text' => "Message " . $this->loop, | |
| ]); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Console\Commands; | |
| use App\Jobs\BlastMessageJob; | |
| use App\Models\Customer; | |
| use Illuminate\Console\Command; | |
| class BlastCommand extends Command | |
| { | |
| /** | |
| * The name and signature of the console command. | |
| * | |
| * @var string | |
| */ | |
| protected $signature = 'blast:execute'; | |
| /** | |
| * The console command description. | |
| * | |
| * @var string | |
| */ | |
| protected $description = 'Command description'; | |
| /** | |
| * Execute the console command. | |
| */ | |
| public function handle() | |
| { | |
| // | |
| try { | |
| $customers = Customer::all(); | |
| $this->info('Total: ' . count($customers)); | |
| $delayInMinutes = 0; | |
| $loop = 1; | |
| foreach ($customers as $key => $customer) { | |
| $randomDelay = rand(1, 2); | |
| $delayInMinutes += $randomDelay; | |
| BlastMessageJob::dispatch($loop++) | |
| ->delay(now()->addMinutes($delayInMinutes)) | |
| ->onQueue('blast'); | |
| $this->info("Pesan ke {$loop} akan dikirim dalam {$delayInMinutes} menit."); | |
| } | |
| $this->info('Done'); | |
| } catch (\Throwable $th) { | |
| throw $th; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment