edbox/slack-notifier

PSR-4 Slack Webhook Notifier with formatting, throttling, and context providers.

Installs: 6

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/edbox/slack-notifier

v1.1.3 2025-11-17 18:03 UTC

This package is auto-updated.

Last update: 2025-12-17 18:21:47 UTC


README

A clean, PSR-4 compliant Slack Webhook notification library for PHP.

This package provides:

  • πŸ”₯ Easy Slack message sending
  • ⚠️ Message levels: info, warning, error
  • 🎨 Rich formatting (emoji + Slack code blocks)
  • 🌍 Context providers (domain/IP or custom)
  • πŸ›‘ Throttling with pluggable storage
  • 🧩 Fully framework-agnostic (PrestaShop, Laravel, Symfony, CLI, cron)
  • πŸ“¦ Zero dependencies except psr/log

πŸš€ Installation

Install via Composer:

composer require edbox/slack-notifier

Or if using a local package:

"repositories": [
  {
    "type": "path",
    "url": "slack_notifier_psr"
  }
]

🧩 Basic Usage

1. Implement a Webhook Provider

use Edbox\Slack\WebhookProviderInterface;

class MyWebhookProvider implements WebhookProviderInterface {
    public function getWebhookUrl(): ?string {
        return 'https://hooks.slack.com/services/XXXX/YYYY/ZZZZ';
    }
}

2. Choose throttling storage

use Edbox\Slack\ArrayThrottleStorage;

$storage = new ArrayThrottleStorage(); // In‑memory

3. Create SlackClient

use Edbox\Slack\SlackClient;
use Edbox\Slack\SlackFormatter;
use Edbox\Slack\SlackThrottle;
use Edbox\Slack\DefaultContextProvider;

$slack = new SlackClient(
    new MyWebhookProvider(),
    $logger,                          // Any PSR‑3 logger
    new SlackFormatter(),
    new SlackThrottle(3600, $storage), // 1 hour throttle
    new DefaultContextProvider()
);

4. Send messages

use Edbox\Slack\SlackMessage;

$slack->send(SlackMessage::info("Cache refreshed"));
$slack->send(SlackMessage::warning("API limit approaching"));
$slack->send(SlackMessage::error("Token expired"));

5. Add context

$msg = SlackMessage::error("Feed parsing failed")
    ->withContext(['source' => 'instagram', 'id' => 18]);

$slack->send($msg);

Slack output:

πŸ”₯ [ERROR]
yourdomain.com (1.1.1.1) | Feed parsing failed
Context: {
    "source": "instagram",
    "id": 18
}

🌍 Custom Context Provider

use Edbox\Slack\ContextProviderInterface;

class MyContextProvider implements ContextProviderInterface {
    public function getPrefix(): array {
        return ['billing', 'v2.1'];
    }
}

πŸ›‘ Throttling Explained

Throttling prevents Slack spam.

A message is uniquely identified by:

  • Context prefix
  • Message level
  • Message text

Configured via:

new SlackThrottle(3600, $storage); // 1 hour

πŸ“œ License

Private β€” Β© 2024 EDBOX. All rights reserved.