File "SMSChannel.php"

Full Path: /home/clickysoft/public_html/jmapi5.clickysoft.net/app/Channels/SMSChannel.php
File size: 1.26 KB
MIME-type: text/x-php
Charset: utf-8

<?php
namespace App\Channels;

use Brevo\Client\Api\TransactionalSMSApi;
use Brevo\Client\Configuration;
use Brevo\Client\Model\SendSms;
use Brevo\Client\Model\SendTransacSms;
use GuzzleHttp\Client;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Log;

class SMSChannel
{

    public function send($notifiable, Notification $notification): bool|SendSms
    {
        $message = $notification->toSms($notifiable);

        if(!$message) {
            return false;
        }

        $to = $notifiable->routeNotificationFor('Sms');
        $apiKey = config('app.brevo_api_key');
        $sender = config('app.brevo_sms_sender');
        $config = Configuration::getDefaultConfiguration()->setApiKey('api-key', $apiKey);

        $apiInstance = new TransactionalSMSApi(
            new Client(),
            $config
        );

        $sendTransacSms = new SendTransacSms();
        $sendTransacSms->setSender($sender);
        $sendTransacSms->setRecipient($to);
        $sendTransacSms->setContent($message->content);

        try {
            return $apiInstance->sendTransacSms($sendTransacSms);
        } catch (\Exception $e) {
            Log::info('SMS failure : '. $to);
            Log::info($e->getMessage());
            return false;
        }
    }
}