sky/yii2-slack-client

Yii2 slack client and error target

Installs: 3 275

Dependents: 1

Suggesters: 0

Security: 0

Stars: 3

Watchers: 3

Forks: 0

Open Issues: 0

Type:yii2-extension

v2.0.3 2021-10-05 06:33 UTC

This package is auto-updated.

Last update: 2024-04-07 07:12:25 UTC


README

Install Package

php composer.phar require sky/yii2-slack-client "*"

or add in composer.json

"sky/yii2-slack-client" : "*"

Set Client Configuration

'message' => [
    'class' => 'sky\slack\SlackClient',
    'defaultChannel' => 'general',
    'webhookUrls' => [
        // channels web hook
        'general' => 'https://hooks.slack.com/services/[key]',
        'error' => 'https://hooks.slack.com/services/[key]',
        'tester' => 'https://hooks.slack.com/services/[key]',
    ],
],

Set Up Error Target Configuration (Optional)

'log' => [
    'traceLevel' => YII_DEBUG ? 3 : 0,
    'targets' => [
        [
            'class' => 'sky\slack\SlackTarget',
            'levels' => ['error', 'warning'],
            'channel' => 'error',
        ],
    ],
],

Basic how to use

use sky\slack\SlackClient;
use Yii;

Yii::$app->message->sendText('Hello World');

Yii::$app->message->setChannel('tester')->sendText('Hello World');

// use attachment
Yii::$app->message->send([
    'text' => 'Hello World',
    'attachments' => [
        [
            'text' => 'Attachment 1',
            'fields' => SlackClient::fieldsAttribute($model, [
                'id',
                'name',
                'country' => 'country.name',
            ])
        ]
    ],
]);

Slack Builder

$builder = Yii::$app->message->createBuilder();
// or 
$builder = new SlackBuilder();

// set text
$builder->setText('Hello World');

// add Blocks
$builder->addHeaderBlock(['text' => 'Helllo']);
$builder->addDividerBlock();

$sectionBlock = new SectionBlock(['text' => 'this is object block']);
$builder->addBlock($section);

// send builder
$builder->send();

// or
$builder->send(Yii::$app->message);

// or 
Yii::$app->message->sendBuilder($builder);

If this library is useful for you, say thanks buying me a beer 🍺!