laravel / slack-notification-channel Goto Github PK
View Code? Open in Web Editor NEWSlack Notification Channel for laravel.
Home Page: https://laravel.com/docs/notifications#slack-notifications
License: MIT License
Slack Notification Channel for laravel.
Home Page: https://laravel.com/docs/notifications#slack-notifications
License: MIT License
The method from
is not supported.
return (new SlackMessage)
->from('MyBot', ':robot_face:')
->to('#general')
->content('Hi!');
While support of Slack's Block kit is welcome, the PR in #64 for some reason implemented only a very limited subset of the available blocks and elements, and provided no easy way to manually add them. The Input block and its associated elements, as well as the checkbox and radio elements for the Action block are particularly glaring oversights.
I will likely submit a PR in the next few weeks, but wanted to file this issue just in case I'm not able to get to it.
Missing Blocks:
File
Input
Video
Missing Elements:
Checkbox group
Date picker
Datetime picker
Email input
Multi-select menus
Number input
Overflow menu
Radio group
Select menu
Time picker
URL input
Workflow button
I see that to()
is not currently supported as noted in #54 .
We're not using to()
, as the channel is configured within the Slack App settings. In a greenfield project, we're still unable to send Slack notifications.
There doesn't appear to be a way to even create the legacy Incoming Webhooks anymore, and Slack's current webhook docs list creating a Slack App as the first step.
On the "Custom Integrations" area of the workspace admin dashboard, there's a warning:
We recommend replacing your custom integrations with Slack apps, which have more features and use the latest APIs.
In the Slack workspaces I have admin permissions on, there's no option to even create a custom integration. You must create a Slack App.
Create a Slack App and an incoming webhook on that app. Add the webhook URI in .env
and attempt to send a notification to Slack.
Our current implementation within a Notification class looks like:
// ...
public function via($notifiable)
{
return ['slack'];
}
public function routeNotificationForSlack($notification)
{
return config('app.slack_webhook_url');
}
public function toSlack($notifiable)
{
$tags = json_encode($this->tags);
$callLink = url('/nova/resources/calls/' . $this->callId);
return (new SlackMessage)
->success()
->content('Applied Tags to Call')
->attachment(function ($attachment) use ($tags, $callLink) {
$attachment->title('Call Id ' . $this->callId, $callLink)
->fields([
'Caller Name' => $this->callerName,
'Caller Phone Number' => $this->callerPhoneNumber,
'Applied Tags' => $tags,
'Call Details Page' => $callLink,
]);
});
}
Thank you so much for this library. Very useful!
For your next release, I'm wondering if you'd consider adding support for https://api.slack.com/docs/message-formatting#parsing_modes
E.g. I'd love to be able to specify a parse
mode (string property) on https://github.com/laravel/slack-notification-channel/blob/1.0/src/Messages/SlackMessage.php
And then have https://github.com/laravel/slack-notification-channel/blob/1.0/src/Channels/SlackWebhookChannel.php#L55 add it to the payload.
Thanks for the great work!
To set a Slack message timestamp, I used Illuminate\Support\InteractsWithTime\SlackAttachment::timestamp(time());
This makes sense. It sets the timestamp, or so the phpdoc says... But in reality it doesn't.
I just sent a message today (20 Feb 2021), but Slack states that it was sent on 20 Feb 2072.
Let's look at the timestamp() method, in Illuminate\Notifications\Messages:
/**
* Set the timestamp.
*
* @param \DateTimeInterface|\DateInterval|int $timestamp
* @return $this
*/
public function timestamp($timestamp)
{
$this->timestamp = $this->availableAt($timestamp);
return $this;
}
This code calls Illuminate\Support\InteractsWithTime:: availableAt($delay):
/**
* Get the "available at" UNIX timestamp.
*
* @param \DateTimeInterface|\DateInterval|int $delay
* @return int
*/
protected function availableAt($delay = 0)
{
$delay = $this->parseDateInterval($delay);
return $delay instanceof DateTimeInterface
? $delay->getTimestamp()
: Carbon::now()->addRealSeconds($delay)->getTimestamp();
}
So what's happening is my time stamp of now is being added as a delay, and turned into the year 2072.
Create a new Slack Attachement and use the timestamp() method.
I'm happy to submit a fix, by removing the calling of availableAt() method. I'm just wondering if there is some more logic needed here too?
Can you maintain Attachment
type with the new Block Kit
? Currently it only possible use Message
type.
Specifying an alternative channel to send a slack notification to, does not work. Probably linked to #34 from back in 2020.
// The app is set to send to a channel named #test-channel, but I want to send to #general
return (new SlackMessage())
->from('Ghost', ':ghost:')
->to('#general')
->content('This will be sent to #general');
3.1.0
10.34.2
8.1.26
MySQL 5.7
I'm trying to send a Slack message via a notification toSlack
. My user has a Slack incoming webhook URL that they wish to receive alerts with, but when sending a message, an error is thrown.
Illuminate\Notifications\Channels\SlackWebhookChannel::buildJsonPayload(): Argument #1 ($message) must be of type Illuminate\Notifications\Messages\SlackMessage, Illuminate\Notifications\Slack\SlackMessage given, called in /var/www/html/vendor/laravel/slack-notification-channel/src/Channels/SlackWebhookChannel.php on line 43
The docs say to use Illuminate\Notifications\Slack\SlackMessage
which I'm using, put I get an error with that
Here's my routeNotificationForSlack
function:
/**
* Route notifications for the Slack channel.
*/
public function routeNotificationForSlack(Notification $notification): string|null
{
return 'https://hooks.slack.com/services/XXXXXX';
}
Here's my toSlack
function
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
*/
public function toSlack($notifiable): SlackMessage
{
$data = [
'type' => 'monitor-down',
'name' => $this->emailData['monitor_name'],
'url' => $this->emailData['monitor_url'],
'value' => 'down',
];
try {
$this->createHistoryEntry('slack', 'Monitor Down', $data, $notifiable);
} catch (\Exception $e) {
}
$monitorName = $this->emailData['monitor_name'];
$monitorURL = $this->emailData['monitor_url'];
return (new SlackMessage)
->text('One of your invoices has been paid!')
->headerBlock('Invoice Paid')
->contextBlock(function (ContextBlock $block) {
$block->text('Customer #1234');
})
->sectionBlock(function (SectionBlock $block) {
$block->text('An invoice has been paid.');
$block->field("*Invoice No:*\n1000")->markdown();
$block->field("*Invoice Recipient:*\n[email protected]")->markdown();
})
->dividerBlock()
->sectionBlock(function (SectionBlock $block) {
$block->text('Congratulations!');
});
// return (new SlackMessage)
// ->from('Domain Monitor')
// ->image('https://domain-monitor.io/slack-logo.png')
// ->error()
// ->attachment(function ($attachment) use ($monitorName, $monitorURL) {
// $attachment->title('Monitor down')
// ->content("Your monitor *$monitorName* -- _($monitorURL)_ -- has just gone *DOWN*")
// ->markdown(['text']);
// });
}
I'm importing
use Illuminate\Notifications\Slack\BlockKit\Blocks\ContextBlock;
use Illuminate\Notifications\Slack\BlockKit\Blocks\SectionBlock;
use Illuminate\Notifications\Slack\BlockKit\Composites\ConfirmObject;
use Illuminate\Notifications\Slack\SlackMessage;
2.5.0
10.4.1
8.2.4
No response
The use of is_callable()
at SlackAttachment.php has undesirable and potentially dangerous side-effects:
public function field($title, $content = '')
{
if (is_callable($title)) {
$callback = $title;
$callback($attachmentField = new SlackAttachmentField);
So, imagine you are creating a message with an attachment field like this:
(new \Illuminate\Notifications\Messages\SlackMessage())
->attachment(fn($attachment) => $attachment->field('dd', 'Whoops')
);
In this case, is_callable('dd')
will of course return true and call dd()
with the SlackAttachmentField as a parameter:
Illuminate\Notifications\Messages\SlackAttachmentField^ {#5154 // vendor/laravel/slack-notification-channel/src/Messages/SlackAttachment.php:216
#title: null
#content: null
#short: true
The same goes for any other callable functions, things like DB::supportedDrivers
, session_destroy
... I leave it up to the readers' imagination what this could be used for.
A possible fix would be to accept closures only and check by if ($title instanceof Closure)
. While this would be a breaking change, I think it is worth consideration.
What do you think?
(new \Illuminate\Notifications\Messages\SlackMessage())
->attachment(fn($attachment) => $attachment->field('dd', 'Whoops')
);
2.5.0
10
8.1
No response
Seems like the way to generate or write a slack notification has completely changed in v3. For example currently in v2, the content
method is used, while in v3 it seems like it has been replaced with a new method called text
? Also, seems like configuration has changed drastically as well. For example, currently no tokens for a bot are required to get going. We can just have a routeNotificationForSlack
method on the User
model and it returns a slack hook url. It would be helpful and hence would like to request for an upgrade guide if possible. The PRs linked in the release section also doesn't have any helpful context for managing user-land code during the upgrade. Thank you for all the work being put in to manage this repo ๐
couldn't find the discussions tab hence had to create an issue.
I am creating a slack message, now I want to add an input text. But it turns out, the inputBlock doesn't exist at all. I see that there has been a question about this before, the issue has been closed but no solution has been found.
Why wasn't this added?
I like to hear it.
Kind regards, Govert
Are there plans to support the new Slack Blocks any time soon? Just wondering if I should hack it myself for now or wait for this package to natively support them.
https://api.slack.com/changelog/2018-12-a-bric-a-brac-of-broadcasts-built-with-blocks
Thanks!
(Driver [slack] not supported) Driver not supported when slack notification is implemented with queue jobs.
Everything works when not implemented with queue.
3.0
10.4
8.1
No response
In the latest update it seems that return type for toSlack()
changed from \Illuminate\Notifications\Messages\SlackMessage
to Illuminate\Notifications\Slack\SlackMessage
which results in
TypeError: Illuminate\Notifications\Slack\SlackChannel::buildJsonPayload(): Argument #1 ($message) must be of type Illuminate\Notifications\Slack\SlackMessage, Illuminate\Notifications\Messages\SlackMessage given, called in /vendor/laravel/slack-notification-channel/src/Slack/SlackChannel.php on line 38
This should probably be documented as a breaking change for the upgrade.
Upgrade slack notifications to v3.0
3.0.1
10.2.8
8.1
No response
There is currently no way to set unfurl_links or unfurl_media to false. The only options currently are null (default) or true (by executing the unfurlLinks
or unfurlMedia
methods.
Unless specifically set to false, Slack will enable unfurl for links and media.
[...]
public function toSlack(object $notifiable): SlackMessage
{
return (new SlackMessage)
->text("Test Notification")
->headerBlock('Test Notification')
->sectionBlock(function (SectionBlock $block) {
$block->field("*Domain*\n<https://www.google.com/|www.google.com>")->markdown();
});
}
[...]
unfurlLinks
method.Very simple issue to describe. The SlackAttachment@field
method checks if the $title
is callable using is_callable
. If you use 'Date' as a field name (or any other name of a global function), it'll be used as a function instead of a string. Example:
$attachment->field('Date', $someDate);
Leads to
date() expects parameter 1 to be string, object given
is_callable
is not a good way to overload the method, and it should be changed. For example, using an extra boolean parameter to indicate the first argument should be used as a callable.
I am using laravel version 5.8. What is the proper way to send a warning message to slack? The following code does not generate a warning message...
public function toSlack($notifiable)
{
return (new SlackMessage)
->content('One of your invoices has been paid!')
->warning();
}
Please, don't close this Issue telling to open a PR: I am here because i have no idea on how to use an undocumented feature of this packges
I can see the class vendor/laravel/slack-notification-channel/src/Messages/SlackAttachment.php
in my project.
I would like to add an attachment, but the only way to use is via vendor/laravel/slack-notification-channel/src/Slack/SlackMessage.php
But this is not the class we (and the doc) use, because we (and the dodc) are instead using vendor/laravel/slack-notification-channel/src/Slack/SlackMessage.php
Actually I'm trying to understand the code and if there is a way to use SlackAttachment
It's undocumented
And I cannot create a docuementation PR because I'm not able to document how to use a thing I have no idea how to use.
Disabling link unfurling in slack messages does not work. Optional paramters with value of false gets dropped by array_filter when building json payload in SlackWebhookChannel. Slack ignores string values for unfurl_media and unfurl_links.
Create new SlackMessage with a full valid url, set unfurlLinks(false), notify slack channel.
I've upgraded Laravel version from 5.7 to 5.8 and installed slack notification channel package via composer. But somehow slack notifications stopped working. composer dumpautoload
discovers this package but seems like it's not registering slack notification channel.
I've traced the code with xdebug and found that createSlackDriver()
method is not found on laravel/framework/src/Illuminate/Support/Manager.php:95
. Due to this "Driver [slack] not supported" exception occurs.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.