klabcyscorpions/slack-notify

This package is abandoned and no longer maintained. No replacement package was suggested.

send notification to Slack for PHP

v2.0.1 2016-05-24 07:33 UTC

README

Build Status

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 count and duration values

Installation

  • We recommend using Composer:
composer require klabcyscorpions/slack-notify:v2.0.0
  • Alternatively, you can download the slack-notify.zip in the release page. Just extract it and include the files under src/ directory

Setup (Slack)

  1. Create an Incoming WebHook integration.
  2. Select a channel where to post the message.
  3. Note for the WebHook URL and add it to the config.

Usage

  1. 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);
  2. Create new Limit, Memcached, Notify and Slack classes with the defined settings

    use 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);
  3. Create new instance of Client based from the created instances of Slack and Notify classes

    return new Client($slack, $notify);
  4. 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 SlackNotifyException if calling slack webhook url did not return an ok response. 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