Skip to content

Instantly share code, notes, and snippets.

@stevepop
Created August 14, 2024 15:06
Show Gist options
  • Save stevepop/9c59c8e6236137163759fce0a980c8c7 to your computer and use it in GitHub Desktop.
Save stevepop/9c59c8e6236137163759fce0a980c8c7 to your computer and use it in GitHub Desktop.
SendWhatsAppMessageJob
<?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 Twilio\Rest\Client;
class SendWhatsAppMessageJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $message;
protected $recipient;
/**
* Create a new job instance.
* /
* public function __construct($recipient, $message)
* {
* $this->recipient = $recipient;
* $this->message = $message;
* }
*
* /**
* Execute the job.
* /
* public function handle(): void
* {
* $twilioClient = new Client(
* config('services.twilio.sid'),
* config('services.twilio.token')
* );
*
* $message = $twilioClient->messages->create(
* "whatsapp:{$this->recipient}",
* [
* 'from' => config('services.twilio.whatsapp_from'),
* 'body' => $this->message,
* ]
* );
* ray($message->id);
* }
* }
*/
public function __construct($recipient, $message)
{
$this->recipient = $recipient;
$this->message = $message;
}
/**
* Execute the job.
*/
public function handle(): void
{
$twilioClient = new Client(
config('services.twilio.sid'),
config('services.twilio.token')
);
$message = $twilioClient->messages->create(
"whatsapp:{$this->recipient}",
[
'from' => config('services.twilio.whatsapp_from'),
'body' => $this->message,
'template' => [
'name' => 'cec_message_template',
'language' => ['code' => 'en'],
'components' => [
[
'type' => 'body',
'parameters' => [
'type' => 'text',
'text' => $this->message,
]
]
],
],
]
);
ray($message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment