Skip to content

mostbyte/mostbyte_notification

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mostbyte Notification

Laravel package for sending notifications via the Mostbyte Notification Service. Supports Telegram, SMS, Email, and Push channels.

Requirements

  • PHP 8.2+
  • Laravel 10, 11, or 12
  • Guzzle 7+

Installation

composer require mostbyte/notification

The package auto-discovers its service provider and facade.

Publish the config file:

php artisan vendor:publish --tag=notification-config

Configuration

Add to your .env:

NOTIFICATION_SERVICE_URL=http://notification-service:8000/api/v1
NOTIFICATION_SERVICE_API_KEY=your-api-key
NOTIFICATION_SERVICE_TIMEOUT=30

Tenant Resolution

Tenant ID is resolved dynamically per request. By default it reads auth()->user()->company->domain.

To customize, add to your AppServiceProvider::boot():

use Notification\Client\NotificationClient;

NotificationClient::resolveTenantUsing(fn () => auth()->user()->company->domain);

You can also pass tenantId explicitly in any DTO to override the resolver.

Usage

Use the NotificationService facade or inject NotificationClient directly.

use Notification\Client\Facades\NotificationService;
use Notification\Client\DTOs\SendNotificationDTO;
use Notification\Client\DTOs\BulkNotificationDTO;

Sending Notifications

// Send to a subscriber by external ID
$response = NotificationService::send(new SendNotificationDTO(
    externalId: $user->id,
    message: 'Your order #123 is ready for pickup.',
    channels: ['telegram', 'sms'],
));

$response->success;      // bool
$response->logIds;        // string[]
$response->channelsUsed;  // string[]
$response->message;       // string

// Send using a notification type template
$response = NotificationService::send(new SendNotificationDTO(
    externalId: $user->id,
    notificationType: 'order_ready',
    templateData: [
        'order_id' => '123',
        'pickup_time' => '14:00',
    ],
));

// Send to a direct recipient
$response = NotificationService::send(new SendNotificationDTO(
    recipient: '+998901234567',
    message: 'Your verification code: 1234',
    channels: ['sms'],
));

Bulk Notifications

$response = NotificationService::sendBulk(new BulkNotificationDTO(
    externalIds: ['user-1', 'user-2', 'user-3'],
    notificationType: 'promotion',
    templateData: ['discount' => '20%'],
));

$response->success;          // bool
$response->totalRecipients;  // int
$response->queued;           // int
$response->failed;           // int
$response->logIds;           // string[]
$response->errors;           // array

// Broadcast to all tenant subscribers (omit targets)
$response = NotificationService::sendBulk(new BulkNotificationDTO(
    message: 'System maintenance at 02:00',
    channels: ['telegram'],
));

Notification Logs

// Get delivery log
$log = NotificationService::getLog($logId);

$log->id;             // string
$log->channel;        // string (telegram, sms, email, push)
$log->status;         // string (pending, sent, delivered, failed)
$log->recipient;      // string
$log->errorMessage;   // ?string
$log->retryCount;     // int
$log->sentAt;         // ?string
$log->createdAt;      // string

// Retry a failed notification
$log = NotificationService::retryNotification($logId);

DTOs

Request DTOs

SendNotificationDTO

Parameter Type Default Description
subscriberId ?string null Direct subscriber ID
externalId ?string null External system ID
recipient ?string null Direct recipient (phone, email...)
notificationType ?string null Template code for lookup
message ?string null Direct message content
title ?string null Title for push/email
templateData ?array null Data for template rendering
channels ?array null Channels to use
priority int 5 Priority 1-10
tenantId ?string null Override tenant resolver
requestId ?string null Correlation ID for tracking
extraData ?array null Additional push notification data

BulkNotificationDTO

Parameter Type Default Description
subscriberIds ?array null List of subscriber IDs
externalIds ?array null List of external IDs
recipients ?array null List of direct recipients
notificationType ?string null Template code
message ?string null Direct message content
title ?string null Title for push/email
templateData ?array null Data for template rendering
channels ?array null Channels to use
priority int 5 Priority 1-10
tenantId ?string null Override tenant resolver
requestId ?string null Correlation ID

Error Handling

All methods throw NotificationException on failure:

use Notification\Client\Exceptions\NotificationException;

try {
    NotificationService::send(new SendNotificationDTO(
        externalId: $user->id,
        message: 'Hello!',
    ));
} catch (NotificationException $e) {
    $e->getMessage();       // Error message
    $e->getStatusCode();    // HTTP status code
    $e->getResponseBody();  // Full response body as array
}

Dependency Injection

Instead of the facade, you can inject the client directly:

use Notification\Client\NotificationClient;
use Notification\Client\DTOs\SendNotificationDTO;

class OrderService
{
    public function __construct(
        private NotificationClient $notification,
    ) {}

    public function notifyOrderReady(Order $order): void
    {
        $this->notification->send(new SendNotificationDTO(
            externalId: $order->user_id,
            notificationType: 'order_ready',
            templateData: [
                'order_id' => $order->id,
            ],
        ));
    }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages