PHP code example of brandonjbegle / slack-app-laravel-notification-channel

1. Go to this page and download the library: Download brandonjbegle/slack-app-laravel-notification-channel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

brandonjbegle / slack-app-laravel-notification-channel example snippets


class HealthStatusChangedNotification extends Notification
{
    use Queueable;

    private $site;
    private $prevStatus;
    private $newStatus;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(Site $site, $prevStatus, $newStatus)
    {
        $this->site = $site;
        $this->prevStatus = $prevStatus;
        $this->newStatus = $newStatus;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [SlackAppChannel::class];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param mixed $notifiable
     * //     * @return SlackAppMessage
     */
    public function toSlackApp($notifiable)
    {
        $now = now()->setTimezone('America/New_York')->toDayDateTimeString();
        return (new SlackAppMessage())
            ->text($this->site->name . " health status changed: {$this->prevStatus} to {$this->newStatus}")
            ->blocks([
                (new HeaderBlock)
                    ->content('Health Status Changed'),
                (new ContextBlock)
                    ->elements([
                        (new ContextBlockImage)->text('text')->url('https://' . $this->site->name . '/favicon.ico'),
                        (new ContextBlockText)->type('plain_text')->content($this->site->name),
                    ]),
                (new SectionBlock)
                    ->text("Status changed from {$this->prevStatus} to {$this->newStatus}")
                    ->fields([
                        [
                            'type' => 'mrkdwn',
                            'text' => "*{$now}*"
                        ],
                    ])
            ]);
    }
}



class Site extends Model
{
    use Notifiable;

public function routeNotificationForSlackApp()
    {
        // If you use the Nova 4 field, this will get the id of the Channel, otherwise simply return the id
        if ($this->slack_notification_channel) {
            return $this->slack_notification_channel['value'] ?? null ;
        }
        return null;
    }
shell
php artisan vendor:publish --provider="BrandonJBegle\SlackNotificationChannel\SlackAppChannelServiceProvider"