mirkhamidov/yii2-notifications

通知系统

安装: 47

依赖: 0

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:yii2-extension

0.3.1 2018-08-06 07:42 UTC

This package is auto-updated.

Last update: 2024-08-29 04:54:21 UTC


README

  • 从模块配置 components
  • 从模块配置 queueNotification
  • 在 composer.json 中添加要求
  • Slack 通知
  • 电子邮件通知

通知提供者

Telegram

详细信息请参阅下面的示例

默认 Yii2 邮件发送器

在主配置文件 main.php 中配置

return [
    'modules' => [
        ...
        'notifications' => [
            'class' => mirkhamidov\notifications\Module::class,
            'queueIn' => 'queueNotifications',
            'providers' => [
                ...
                'default-mailer' => [
                    'class' => MailerProvider::class,
                    'from' => [{SENDER EMAIL} => {SENDER NAME}],
                ],
            ],
        ],
    ],
];

使用视图文件和视图参数发送

Yii::$app->notifications->sendTemplateMail([
    'to' => {RECEPIENT EMAIL},
    'subject' => {SUBJECT},
    'view' => ['html' => {HTML VIEW}, 'text' => {TEXT VIEW}],
    'params' => [
        {KEY-VALUE params for views}
    ],
]);

或已渲染/准备好的文本和其他参数

Yii::$app->notifications->sendTemplateMail([
    'to' => {RECEPIENT EMAIL},
    'subject' => 'Test messages fo ' . rand(1, 99999),
    'htmlBody' => $msg,
    ['textBody' => $msg,]
    ['cc' => {CC},]
    ['bcc' => {BCC},]
    ['replyTo' => {REPLY TO EMAIL},]
    ['attachFilePath' => {PATH TO FILE TO ATTACH},]
]);

配置

在主应用程序配置文件中

return [
    'bootstrap' => [
        'queueNotifications',
    ],
    'components' => [
        'queueNotifications' => [
            'class' => \yii\queue\db\Queue::class,
            'as log' => \yii\queue\LogBehavior::class,
            'db' => 'db', // DB connection component or its config
            'tableName' => '{{%queue}}', // Table name
            'channel' => 'notification', // Queue channel key
            'mutex' => \yii\mutex\PgsqlMutex::class, // Mutex that used to sync queries
            'mutexTimeout' => 0,
            'ttr' => 5 * 60, // Max time for anything job handling
            'attempts' => 5, // Max number of attempts
        ],
        'notifications' => [
            'class' => \mirkhamidov\notifications\Notifications::class,
        ],
    ],
    'modules' => [
        'notifications' => [
            'class' => mirkhamidov\notifications\Module::class,
            'queueIn' => 'queueNotifications',
            'providers' => [
                'telegram' => [
                    'class' => \mirkhamidov\notifications\providers\Telegram::class,
                ],
            ],
        ],
    ],
];

记录到不同的文件

在应用程序配置文件中

'components' => [
    'log' => [
        'targets' => [
            ...
            [
                'class' => 'yii\log\FileTarget',
                'categories' => [
                    'mirkhamidov\notifications\providers\*',
                ],
                'logFile' => '@app/runtime/logs/notification-providers.log',
                'logVars' => [],
                'prefix' => function ($message) {
                    return '';
                }
            ],
        ],
    ],
],

示例

Telegram 发送消息

use mirkhamidov\notifications\providers\TelegramProvider;

$msg = 'any message';
\Yii::$app->notifications->send($msg, TelegramProvider::ID, [
    'providerParams' => [
        'chat_id' => {CHAT_ID},
    ],
]);

更多 providerParams 请参阅Telegram SendMessage API

带文件的 Telegram 消息

use mirkhamidov\notifications\providers\TelegramProvider;

$msg = 'any message';

\Yii::$app->notifications->send($msg, TelegramProvider::ID, [
    'providerParams' => [
        'chat_id' => Yii::$app->params['telegram']['miroff'],
        'file' => $model->getPdfFilePath(),
        'fileParams' => [
            // custom params
            ['fileType' => TelegramProvider::FILE_TYPE_DOCUMENT,]
            ['messageMergeType' => TelegramProvider::FILE_MESSAGE_MERGE_TYPE_AS_REPLY,]

            // any other Telegram API params, see below
            ['disable_notification' => true,]
        ],
    ],
]);
  • file 文件的完整路径
  • fileParams 管理文件的参数

要仅发送文件(不包含消息),只需将 $mgs 设置为 null