dealerinspire/slack-notifications
Composer 安装命令:
composer require dealerinspire/slack-notifications
包简介
This package provides a way to easily send Slack notifications with attachments.
关键字:
README 文档
README
An easy, fluent implementation of a Slack notification system.
Adding the Package to Your Project
Requiring in Composer
composer require dealerinspire/slack-notifications
Adding to Your Code
At the top of the class file where you wish to use:
use DealerInspire\SlackNotifications\SlackNotification;
Basic Implementation
$slackNotification = (new SlackNotification)
->toHook('abc\123\xyz')
->withUsername('Newt Scamander')
->withIcon('unicorn_face')
->toChannel('#mythical_beasts')
->withTitle('Testing Slack Notification')
->withText('Just checking to make sure the notification goes through')
->unfurlLinks(false)
->unfurlMedia(false)
->send();
With Simple Attachment
At the top of the class file where you wish to use:
use DealerInspire\SlackNotifications\SlackNotification;
use DealerInspire\SlackNotifications\SlackAttachment;
In your class code:
$slackAttachment = (new SlackAttachment)
->createFromArray([
'fallback' => 'The Mythical Animal is a Unicorn',
'title' => 'What are Mythical Animals?',
'text' => 'Mythical animals are creatures that dont exist',
]);
$slackNotification = (new SlackNotification)
->toHook('abc\123\xyz')
->withUsername('Newt Scamander')
->withIcon('unicorn_face')
->toChannel('#mythical_beasts')
->withTitle('Testing Slack Notification')
->withText('Just checking to make sure the notification goes through')
->unfurlLinks(false)
->unfurlMedia(false);
->withAttachments([$slackAttachment])
->send();
With More Complex Attachment
At the top of the class file where you wish to use:
use DealerInspire\SlackNotifications\SlackNotification;
use DealerInspire\SlackNotifications\SlackAttachment;
use DealerInspire\SlackNotifications\SlackAttachment\SlackAttachmentAction;
use DealerInspire\SlackNotifications\SlackAttachment\SlackAttachmentField;
In your class code:
$unicornAttachmentField = (new SlackAttachmentField)
->createFromArray([
'title' => 'Mythical Animal',
'value' => 'Unicorn',
'short' => true,
]);
$unicornAttachmentAction = (new SlackAttachmentAction)
->createFromArray([
'text' => 'Find more about Unicorns',
'url' => 'https://en.wikipedia.org/wiki/Unicorn',
]);
$slackAttachment = (new SlackAttachment)
->createFromArray([
'fallback' => 'The Mythical Animal is a Unicorn',
'title' => 'What are Mythical Animals?',
'text' => 'Mythical animals are creatures that dont exist',
]);
$slackAttachment->addField($unicornAttachmentField);
$slackAttachment->addAction($unicornAttachmentAction);
$slackNotification = (new SlackNotification)
->toHook('abc\123\xyz')
->withUsername('Newt Scamander')
->withIcon('unicorn_face')
->toChannel('#mythical_beasts')
->withTitle('Testing Slack Notification')
->withText('Just checking to make sure the notification goes through')
->unfurlLinks(false)
->unfurlMedia(false);
->withAttachments([$slackAttachment])
->send();
Notification configured with an array
At the top of the class file where you wish to use:
use DealerInspire\SlackNotifications\SlackNotification;
In your class code:
$slackAttachmentConfiguration = [
[
'fallback' => 'The Mythical Animal is a Unicorn',
'title' => 'What are Mythical Animals?',
'text' => 'Mythical animals are creatures that dont exist',
'actions' => [
[
'text' => 'Find more about Unicorns',
'url' => 'https://en.wikipedia.org/wiki/Unicorn',
],
],
'fields' => [
[
'title' => 'Mythical Animal',
'value' => 'Unicorn',
'short' => true,
],
],
],
];
$slackNotification = (new SlackNotification)
->toHook('abc\123\xyz')
->withUsername('Newt Scamander')
->withIcon('unicorn_face')
->toChannel('#mythical_beasts')
->withTitle('Testing Slack Notification')
->withText('Just checking to make sure the notification goes through')
->unfurlLinks(false)
->unfurlMedia(false);
->withAttachments($slackAttachmentConfiguration)
->send();
Using Environmental Variables
You may also use environmental variables to default some settings. The environmental variables are contained within your project's .env file.
SLACK_HOOK_PATH=abc\123\xyz
SLACK_USERNAME=Newt Scamander
SLACK_ICON=unicorn_face
SLACK_CHANNEL=#mythical_beasts
When you use environmental variables this, a basic use case can become even simpler. Of course, you can always overwrite these settings with the methods shown in the examples above.
At the top of the class file where you wish to use:
use DealerInspire\SlackNotifications\SlackNotification;
In your class code:
(new SlackNotification)
->withTitle('Testing Slack Notification')
->withText('Just checking to make sure the notification goes through')
->send();
Contributing
See the CONTRIBUTING guide.
统计信息
- 总下载量: 1.01k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-24