rulecom/notifier

通过Rule和其他提供商发送通知

1.2.8 2023-01-30 10:43 UTC

README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

通过Rule(电子邮件/短信)和Slack发送通知。受Laravel通知系统启发,可与Laravel框架或完全独立使用。

安装

通过Composer

$ composer require rulecom/notifier

使用方法

要发送通知,您需要创建通知对象。这些对象负责通知通过哪些渠道发送通知消息以及每个相应渠道消息应包含什么内容。

use RuleCom\Notifier\Channels\Email;
use RuleCom\Notifier\Channels\Slack;

class UserHasRegistered
{
    /**
     * Here we specify through which channels we want to
     * send our notification.
    */
    public function via()
    {
        return ['email', 'slack'];
    }

    /**
     * Each via method needs a correspondng "to" method.
    */
    public function toEmail()
    {
        // Specify what the email message should contain.
    }

    /**
     * Each via method needs a correspondng "to" method.
    */
    public function toSlack()
    {
        // Specify what the Slack message should contain.
    }
}

// To send the notification to all specified channels:
$notifier = new RuleCom\Notifier\Notifier();
$notifier->send(new UserHasRegistered());

渠道

目前此包支持以下渠道提供商

  • Rule:发送电子邮件和短信。
  • Slack:向Slack发送消息。

通过(Rule)发送电子邮件

public function toEmail()
{
    return (new RuleCom\Notifier\Channels\Email(new GuzzleHttp\Client()))
        ->apikey('YOUR-RULE-API-KEY') // If using Laravel you can set this in config/rule-notifier.php
        ->subject('Hello, world!')
        ->from([
            'name' => 'John Doe',
            'email' => 'john@doe.com'
        ])
        ->to([
            'name' => 'Jane Doe',
            'email' => 'jane@doe.com'
        ])
        ->content([
            'html' => '<h1>Notification sent via Rule!</h1>',
            'html' => 'Notification sent via Rule!'
        ]);
}

Slack

public function toSlack()
{
    return (new RuleCom\Notifier\Channels\Slack(new GuzzleHttp\Client()))
        ->endpoint('YOUR-SLACK-INCOMING-WEBHOOK') // If using Laravel you can set this in config/rule-notifier.php
        ->channel('#notification') // Here you can override the channel specified in Slack, or send DM by passing @username
        ->message('Hello, world!');
}

与Laravel一起使用

此包可以轻松集成到laravel中,具有以下优点。

  • 无需自己传递渠道依赖。
  • 可以指定配置,例如Rule的API密钥和Slack的webhook。
  1. 在您的config/app.php中添加以下服务提供者
RuleCom\Notifier\LaravelServiceProvider::class
  1. 发布配置
php artisan vendor:publish
// Without Laravel you will have to pass the channel dependency on your own:
(new RuleCom\Notifier\Channels\Slack(new GuzzleHttp\Client()))

// With Laravel you can resolve the channels with dependencies through the ioc container:
app(RuleCom\Notifier\Channels\Slack::class)

调试

如果您需要调试渠道,您可以将其设置为调试模式。当渠道处于调试模式时,它将记录通知而不是将其分派到指定的渠道。

启用调试

  1. Monolog\Logger注入到渠道中。
  2. 调用debug方法并传递到您的日志文件路径。
return (new RuleCom\Notifier\Channels\Slack(new GuzzleHttp\Client(), new Monolog\Logger('Notification logger')))
        ->debug('path/to/file.log') // If using Laravel you can set both debug mode and log path in config/rule-notifier.php
        ->endpoint('YOUR-SLACK-INCOMING-WEBHOOK') // If using Laravel you can set this in config/rule-notifier.php
        ->channel('#notification') // Here you can override the channel specified in Slack, or send DM by passing @username
        ->message('Hello, world!');

更改日志

请参阅CHANGELOG了解最近更改的信息。

测试

$ composer test

贡献

请参阅CONTRIBUTINGCONDUCT以获取详细信息。

安全性

如果您发现任何安全相关的问题,请通过电子邮件matthis.stenius@rule.se而不是使用问题跟踪器。

鸣谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件