Skip to content

Notification Service is a flexible and extensible backend service written in pure PHP 8.2+, designed to send notifications through various channels: Email, SMS, Telegram, Webhook, Push notifications, and any other channels that can be connected via adapters.

License

Notifications You must be signed in to change notification settings

MuhammadIbrahimli/Notification-Service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Notification Center

Modular service for sending notifications via various communication channels in pure PHP 8.2+ without frameworks.

πŸ“‹ Description

Notification Center is a production-ready service that accepts HTTP requests and sends notifications through different channels:

  • πŸ“§ Email β€” via SMTP or mail() function
  • πŸ“± SMS β€” via providers' REST API
  • πŸ’¬ Telegram β€” via Bot API
  • πŸ”— Webhook β€” sending to arbitrary HTTP endpoints

✨ Features

  • βœ… Modular architecture with separation of concerns (SRP)
  • βœ… Task queue for asynchronous processing
  • βœ… Retry mechanism for all drivers (3 attempts)
  • βœ… Logging of all operations
  • βœ… Extensible driver system
  • βœ… Full typing with declare(strict_types=1)
  • βœ… Docker environment for development and production
  • βœ… RESTful API

πŸ—οΈ Architecture

Request β†’ Router β†’ Controller β†’ NotificationService β†’ DriverManager β†’ Drivers

Each driver implements the NotificationDriverInterface and can be easily extended.

πŸ“ Project Structure

project/
β”œβ”€β”€ public/              # Entry point
β”‚   └── index.php
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ Controllers/     # Controllers
β”‚   β”œβ”€β”€ Services/        # Business logic
β”‚   β”œβ”€β”€ Drivers/         # Notification drivers
β”‚   β”œβ”€β”€ Core/            # Core (Router, Request, Response)
β”‚   β”œβ”€β”€ Models/          # Data models
β”‚   β”œβ”€β”€ Queue/           # Queue system
β”‚   └── Database/        # Database operations
β”œβ”€β”€ config/              # Configuration files
β”œβ”€β”€ storage/             # Logs and files
└── docker/              # Docker configuration

πŸš€ Quick Start

Requirements

  • PHP 8.2+
  • Composer
  • MySQL 5.7+ or 8.0+
  • Docker and Docker Compose (optional)

Installation

  1. Clone the repository:
git clone <repository-url>
cd notification-service
  1. Install dependencies:
composer install
  1. Environment setup:

Create a .env file based on .env.example:

cp .env.example .env

Edit .env and specify database connection parameters and driver settings.

  1. Create database:
# Create DB manually or use migrations
mysql -u root -p -e "CREATE DATABASE notification_service CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
  1. Run migrations:
composer migrate

Or manually:

php -r "require 'vendor/autoload.php'; \NotificationService\Database\DB::migrate();"
  1. Start built-in PHP server:
php -S localhost:8000 -t public
  1. Start queue worker (in a separate terminal):
php src/Queue/Worker.php

Or via composer:

composer worker

🐳 Docker

Run via Docker Compose

  1. Start all services:
docker-compose up -d
  1. Run migrations:
docker-compose exec apache composer migrate
  1. Check status:
docker-compose ps
  1. View logs:
docker-compose logs -f worker
docker-compose logs -f apache
  1. Stop:
docker-compose down

The service will be available at: http://localhost:8080

πŸ“‘ API Endpoints

1. Send notification

POST /send

{
  "channel": "telegram",
  "to": "123456789",
  "message": "Hello, World!",
  "subject": "Optional subject",
  "payload": {
    "parse_mode": "HTML"
  }
}

Response:

{
  "status": "queued",
  "request_id": 12
}

2. Get notification status

GET /status/{id}

Response:

{
  "id": 12,
  "channel": "telegram",
  "payload": {
    "to": "123456789",
    "message": "Hello, World!"
  },
  "status": "completed",
  "created_at": "2024-01-15 10:30:00",
  "updated_at": "2024-01-15 10:30:05"
}

3. Get notification logs

GET /logs/{id}

Response:

{
  "request_id": 12,
  "logs": [
    {
      "id": 1,
      "driver": "telegram",
      "success": true,
      "response": {
        "ok": true,
        "result": {...}
      },
      "created_at": "2024-01-15 10:30:05"
    }
  ]
}

4. Health Check

GET /health

Response:

{
  "status": "healthy",
  "timestamp": "2024-01-15T10:30:00+00:00",
  "version": "1.0.0"
}

πŸ”Œ Supported Channels

Email

{
  "channel": "email",
  "to": "[email protected]",
  "subject": "Test Email",
  "message": "This is a test message",
  "payload": {
    "html": "<p>HTML content</p>"
  }
}

SMS

{
  "channel": "sms",
  "to": "+1234567890",
  "message": "Your verification code is 1234"
}

Telegram

{
  "channel": "telegram",
  "to": "123456789",
  "message": "Hello from Notification Center!",
  "payload": {
    "parse_mode": "HTML"
  }
}

Webhook

{
  "channel": "webhook",
  "payload": {
    "url": "https://example.com/webhook",
    "method": "POST",
    "data": {
      "event": "notification",
      "message": "Test"
    },
    "headers": {
      "X-Custom-Header": "value"
    }
  }
}

βš™οΈ Configuration

Driver settings are located in config/drivers.php. Environment variables are configured in .env:

  • EMAIL_SMTP_HOST β€” SMTP server
  • EMAIL_SMTP_PORT β€” SMTP port
  • EMAIL_SMTP_USER β€” SMTP user
  • EMAIL_SMTP_PASS β€” SMTP password
  • SMS_API_URL β€” API URL for SMS
  • SMS_API_KEY β€” API key for SMS
  • TELEGRAM_BOT_TOKEN β€” Telegram bot token
  • WEBHOOK_TIMEOUT β€” Timeout for webhook requests

πŸ”§ Development

Adding a new driver

  1. Create a driver class implementing NotificationDriverInterface:
<?php

declare(strict_types=1);

namespace NotificationService\Drivers;

class CustomDriver implements NotificationDriverInterface
{
    public function send(array $payload): NotificationResult
    {
        // Your sending logic
        return new NotificationResult(true, 'Success');
    }
}
  1. Add configuration to config/drivers.php:
'custom' => [
    'driver' => \NotificationService\Drivers\CustomDriver::class,
    'config' => [
        // Your settings
    ],
],
  1. Done! Now you can use the custom channel.

Queue Structure

Tasks in the queue have the following structure:

  • id β€” unique task identifier
  • payload β€” task data (JSON)
  • status β€” status (pending, processing, completed, failed)
  • attempts β€” number of attempts
  • created_at β€” creation time

πŸ“ Logging

Logs are saved in the storage/logs/ directory:

  • error-YYYY-MM-DD.log β€” application errors
  • DB logs can be viewed via Docker: docker-compose logs db

πŸ§ͺ Testing

Examples of test requests via cURL:

# Send notification
curl -X POST http://localhost:8000/send \
  -H "Content-Type: application/json" \
  -d '{
    "channel": "telegram",
    "to": "123456789",
    "message": "Test notification"
      }'

# Check status
curl http://localhost:8000/status/1

# Get logs
curl http://localhost:8000/logs/1

# Health check
curl http://localhost:8000/health

πŸ”’ Security

  • All SQL queries use prepared statements
  • Validation of all input data
  • Exception handling at all levels
  • Error logging without revealing sensitive information

πŸ“„ License

This project was created for educational purposes.

πŸ‘₯ Author

Muhammad Ibrahimli

🀝 Contribution

Suggestions and pull requests are welcome!


Version: 1.0.0
PHP: 8.2+
Status: Production Ready

About

Notification Service is a flexible and extensible backend service written in pure PHP 8.2+, designed to send notifications through various channels: Email, SMS, Telegram, Webhook, Push notifications, and any other channels that can be connected via adapters.

Resources

License

Stars

Watchers

Forks

Packages

No packages published