tdx / notify-laravel
从 Laravel 发送通知
v2.0.2
2021-05-25 04:22 UTC
Requires
- php: >=7.0
- laravel/framework: >=5.3
- laravel/slack-notification-channel: ^2.3.1
README
一个简单的 PHP 包,通过 Slack(https://slack.com)的入站 Webhook 或电子邮件从 Laravel 发送通知。此包将自动格式化异常对象实例、字符串文本或数组作为消息。在发送异常作为消息时,它会附加 "用户代理"、"请求 URI" 和 "IP 地址" 的信息。对于 Laravel,建议在 Exceptions\Handler.php 类中使用此类。
需求
- PHP >=7.0
- laravel/framework >=5.3
- laravel/slack-notification-channel: "^2.3.1"
安装
1. 使用 composer 安装此包。
composer require tdx/notify-laravel
2. 在 config\app.php 中添加 'provider' 和 'alias'。
'providers' => [ ...
Notify\Laravel\NotifyServiceProvider::class,
...],
'aliases' => [ ...
'Notify' => Notify\Laravel\Facades\Notify::class,
...],
3. 发布必要的配置和视图文件。
php artisan vendor:publish --tag='notify-laravel'
添加 --force
选项以覆盖先前发布的文件。
这些命令应该创建
/config/notify.php,
/resources/views/vendor/notify/mail.blade.php
如果这些发布命令不起作用,请尝试
php artisan config:clear
这将清除配置缓存。
4. 在您的 Slack 账户上 创建一个入站 Webhook。您需要在 config\slack.php 文件中写入 Webhook URL 以通过 Slack 发送消息。
设置
为某些配置文件写入值。
在 config\slack.php 中,
'endpoint'= (e.g.) 'https://hooks.slack.com/services/xxx/yyy/zzz' //webhook URL for your incoming webhook
'channel'= (e.g.) '#general' // channnel or username where you want to send a message. null for default
'username'= (e.g.) 'Robot' // username that is going to be displayed on the message. null for default
'link_names' = (e.g.) true // needs to be true to send with mention <- NEW
在 .env 中,为 mail 设置合适的值,
MAIL_DRIVER= (e.g.) smtp
MAIL_HOST= (e.g.) smtp.gmail.com
MAIL_PORT= (e.g.) 465
MAIL_USERNAME= (e.g.) YOUR_EMAIL_ADDRESS
MAIL_PASSWORD= (e.g.) YOUR_EMAIL_PASSWORD
MAIL_ENCRYPTION= (e.g.) ssl
并添加
NOTIFY_SLACK=(e.g.) true
NOTIFY_MAIL=(e.g.) true
NOTIFY_SLACK_MENTION=(e.g.) @here
如果值 = true,适配器将被启用(适配器可以发送消息)。如果值 = false,适配器将被关闭(适配器不能发送消息)。如果 .env 文件中没有定义值,则默认为 false。提及将附加到内容开头。
如何发送消息
此类自动格式化和发送消息。内容可以是异常对象、字符串或数组。
- 从 Facade 发送消息。
\Notify::send($content); // sends an exception with default setting.
\Notify::send($content, $options, 'slack'); // keys of options array for Slack =['from', 'to', 'icon', 'fields', 'max_retry', 'force']
\Notify::send($content, $options, 'mail'); // keys of options array for Mail =['from', 'to', 'subject', 'fields', 'max_retry', 'force']
\Notify::force($content); // force method forces to send the content regardless of what the active value is.
\Notify::send($content, ['mention' => '@here']); // sends an exception with mention.
- 从实例发送消息。
$notify = new \Notify\Laravel\Notify(); // instance of Notify with default setting.
$notify->setTo($address); // change address. (channel or username for slack)
$notify->setFrom($username); // change username on the message.
$notify->setAdapter($adapter_name); // set adapter to 'slack' or 'mail'
$notify->send($content); // send message
// or use $notify->force($content) to force to send.
适配器的选项
对于 SlackAdapter,
对于 MailAdapter,
使用 Laravel 异常处理程序实现示例
在 App\Exceptions\Handler 类中,
use Notify\Laravel\Exception\NotifyException;
public function report(Exception $exception)
{
if ($this->shouldntReport($e)) {
return;
}
parent::report($exception);
try {
try {
// Use Notify class here.
\Notify::send($exception); // Send with default settings.
} catch (NotifyException $ne) {
try {
// send via mail. (Another way to send a notification if first one failed.)
\Notify::send($ne, ['to' => 'YOUR_EMAIL_ADDRESS'], 'mail');
parent::report($ne);
} catch (NotifyException $ne2) {
// Problem of mail settings. Dont't use Notify class here to avoid loop.
parent::report($ne2);
}
}
} catch (Exception $e) {
// Notify class should throw only NotifyException, but just in case, catch other Exception here to avoid loop.
parent::report($e);
}
}
如何创建其他适配器
- 在 Adapters 文件夹中创建一个实现 AdapterInterface 的类。
- 将类命名为 xxxAdapter。xxx 将是即将调用的适配器名称。
- 修改 config/config.php 文件以定义适配器的默认值。
注意
如果适配器发送消息失败,它将自动重试发送。在选项数组中写入 ['max_retry' = SOME_NUMBER] 以更改尝试次数(默认 max_retry = 3)。如果所有尝试都失败,它将抛出 NotifyException。要获取更具体的错误信息,您应该检查 laravel.log 文件(日志文件捕获所有尝试的错误)。