Skip to content

Instantly share code, notes, and snippets.

@flatanimals
Last active May 15, 2017 18:16
Show Gist options
  • Save flatanimals/2cbf4aa13842d69fdc87a214965f56b7 to your computer and use it in GitHub Desktop.
Save flatanimals/2cbf4aa13842d69fdc87a214965f56b7 to your computer and use it in GitHub Desktop.
cheap laravel slackbot
<?php
// Notify Subscribers (who do we notify?)
$slackbot = new SlackBot();
$slackbot->notify(new ShitHappened($youCanPassDataToo));
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\SlackAttachment;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
class ShitHappened extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['slack'];
}
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable)
{
return (new SlackMessage)
->success()
->content('Shit just happened')
->attachment(function (SlackAttachment $attachment) {
$attachment->content(str_random(500));
$attachment->fields([
'field 1' => 'value 1',
'field 2' => 'value 2',
'field 3' => 'value 3',
'field 4' => 'value 4'
]);
})
->attachment(function (SlackAttachment $attachment) {
$attachment->content(str_random(500));
$attachment->fields([
'field 11' => 'value 11',
'field 12' => 'value 12',
'field 13' => 'value 13',
'field 14' => 'value 14'
]);
});
}
}
<?php
/*
* SlackBot Channels
*
*/
return [
'default' => 'https://hooks.slack.com/services/path/to/your/room',
'other' => 'https://hooks.slack.com/services/path/to/your/otherRoom',
];
<?php namespace App\Integrations;
use Illuminate\Notifications\Notifiable;
class SlackBot
{
use Notifiable;
protected $room;
/**
* SlackBot constructor.
* @param $room
*/
public function __construct($room = 'default')
{
$this->room = $room;
}
/**
* Route notifications for the Slack channel.
*
* @return string
*/
public function routeNotificationForSlack()
{
return config()->get("slack.{$this->room}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment