Add slack support to Firefly III

This commit is contained in:
James Cole
2022-09-24 12:14:27 +02:00
parent 2945f5fcde
commit 45d7042405
10 changed files with 138 additions and 17 deletions

View File

@@ -25,6 +25,7 @@ namespace FireflyIII\Notifications\Admin;
use FireflyIII\Mail\AdminTestMail;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
/**
@@ -54,7 +55,7 @@ class TestNotification extends Notification
*/
public function via($notifiable)
{
return ['mail'];
return ['mail', 'slack'];
}
/**
@@ -69,6 +70,16 @@ class TestNotification extends Notification
->markdown('emails.admin-test', ['email' => $this->address])
->subject((string) trans('email.admin_test_subject'));
}
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable)
{
return (new SlackMessage)->content((string)trans('email.admin_test_subject'));
}
/**
* Get the array representation of the notification.

View File

@@ -25,8 +25,12 @@ namespace FireflyIII\Notifications\Admin;
use FireflyIII\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
/**
* Class UserRegistration
*/
class UserRegistration extends Notification
{
use Queueable;
@@ -46,18 +50,18 @@ class UserRegistration extends Notification
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
return ['mail', 'slack'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
@@ -67,10 +71,21 @@ class UserRegistration extends Notification
->subject((string) trans('email.registered_subject_admin'));
}
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable)
{
return (new SlackMessage)->content((string) trans('email.admin_new_user_registered', ['email' => $this->user->email, 'id' => $this->user->id]));
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)

View File

@@ -22,9 +22,9 @@ declare(strict_types=1);
namespace FireflyIII\Notifications\Admin;
use FireflyIII\Models\Bill;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
/**
@@ -35,7 +35,7 @@ class VersionCheckResult extends Notification
{
use Queueable;
private string $message;
private string $message;
/**
* Create a new notification instance.
@@ -55,7 +55,7 @@ class VersionCheckResult extends Notification
*/
public function via($notifiable)
{
return ['mail'];
return ['mail', 'slack'];
}
/**
@@ -69,7 +69,21 @@ class VersionCheckResult extends Notification
return (new MailMessage)
->markdown('emails.new-version', ['message' => $this->message])
->subject((string)trans('email.new_version_email_subject'));
->subject((string) trans('email.new_version_email_subject'));
}
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable)
{
return (new SlackMessage)->content($this->message)
->attachment(function ($attachment) {
$attachment->title('Firefly III @ GitHub', 'https://github.com/firefly-iii/firefly-iii/releases');
});
}
/**

View File

@@ -25,6 +25,7 @@ namespace FireflyIII\Notifications\User;
use FireflyIII\Models\Bill;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
/**
@@ -58,7 +59,7 @@ class BillReminder extends Notification
*/
public function via($notifiable)
{
return ['mail'];
return ['mail', 'slack'];
}
/**
@@ -79,6 +80,28 @@ class BillReminder extends Notification
->subject($subject);
}
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable)
{
$message = (string) trans(sprintf('email.bill_warning_subject_%s', $this->field), ['diff' => $this->diff, 'name' => $this->bill->name]);
if (0 === $this->diff) {
$message = (string) trans(sprintf('email.bill_warning_subject_now_%s', $this->field), ['diff' => $this->diff, 'name' => $this->bill->name]);
}
$bill = $this->bill;
$url = route('bills.show', [$bill->id]);
return (new SlackMessage)
->warning()
->attachment(function ($attachment) use ($bill, $url) {
$attachment->title((string) trans('firefly.visit_bill', ['name' => $bill->name]), $url);
})
->content($message);
}
/**
* Get the array representation of the notification.
*