sky/yii2-slack-client

Yii2 Slack客户端和错误目标

安装次数: 3,351

依赖项: 1

建议者: 0

安全性: 0

星标: 3

关注者: 3

分支: 0

公开问题: 0

类型:yii2-extension

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

This package is auto-updated.

Last update: 2024-09-07 08:02:49 UTC


README

安装包

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

或在composer.json中添加

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

设置客户端配置

'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]',
    ],
],

设置错误目标配置(可选)

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

基本使用方法

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 = 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);

如果这个库对您有用,请表示感谢 买我一杯啤酒 🍺