klabcyscorpions / slack-notify
send notification to Slack for PHP
Installs: 375
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 6
Forks: 0
Open Issues: 0
pkg:composer/klabcyscorpions/slack-notify
Requires
- php: >=5.4
- ext-curl: *
- ext-memcached: *
Requires (Dev)
- phpunit/phpunit: 4.8.24
This package is not auto-updated.
Last update: 2017-03-02 19:35:57 UTC
README
SlackNotify is a PHP Library that enables you to send messages to slack using incoming webhook integration.
Dependencies
This library requires Curl and Memcached extensions to be installed
- Curl extension is used to call and post payload to Slack's WebHook URL
- Memcached extension will be used for checking send limit.
Limit can be set as
countanddurationvalues
Installation
- We recommend using
Composer:
composer require klabcyscorpions/slack-notify:v2.0.0
- Alternatively, you can download the
slack-notify.zipin the release page. Just extract it and include the files undersrc/directory
Setup (Slack)
- Create an Incoming WebHook integration.
- Select a channel where to post the message.
- Note for the
WebHookURL and add it to the config.
Usage
Define required client settings.
define('WEBHOOK_URL', 'https://api.slack.com/webhook-url/xxxx/xxxx'); /** Memcached connection settings */ define('MEMCACHED_HOST', 'localhost'); define('MEMCACHED_PORT', 11211); define('MEMCACHED_KEY', 'slack-report-key'); /** Limit to max of 10 sends per 60 seconds */ define('LIMIT_COUNT', 10); define('LIMIT_DURATION', 60);
Create new
Limit,Memcached,NotifyandSlackclasses with the defined settingsuse KLabCyscorpions\SlackNotify\Limit; use KLabCyscorpions\SlackNotify\Notify; use KLabCyscorpions\SlackNotify\Slack; //create limit object $limit = new Limit(LIMIT_COUNT, LIMIT_DURATION); //create memcached object using defined Memcached class //this will be used to check send limit $memcached = new Memcached(); $memcached->addServer(MEMCACHED_HOST, MEMCACHED_PORT); //create notify object instance $notify = new Notify($memcached, $limit, MEMCACHED_KEY); //create slack object $slack = new Slack(WEBHOOK_URL);
Create new instance of
Clientbased from the created instances ofSlackandNotifyclassesreturn new Client($slack, $notify);Generate payload and send it to slack. See Slack Formatting API to check how to create payload data.
$payload = array( 'channel' => '#general', 'text' => 'This message is sent to #general by SlackNotify!' ); $client->send($payload);
Send method may throw
SlackNotifyExceptionif calling slack webhook url did not return anokresponse. See error log file to view the webhook response message.
Other Method(s)
| Methods | Description |
|---|---|
$client->disableLimit() |
Disable limit checking before sending to slack |
$client->getNotify() |
Get Notify class instance |
$client->getSlack() |
Get Notify class instance |