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
Requires (Dev)
- psr/log: ^3.0
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.