aranyasen/laravel-slack
Composer 安装命令:
composer require aranyasen/laravel-slack
包简介
A package to send messages to Slack
README 文档
README
Laravel Slack
This package allows you to easily compose and send Slack messages from Laravel applications
Installation
Step-1:
composer require aranyasen/laravel-slack
Step-2: Publish the config:
php artisan vendor:publish --provider=Aranyasen\\LaravelSlack\\SlackServiceProvider
It creates config/laravel-slack.php.
Step-3:
Add parameters SLACK_WORKSPACE and SLACK_TOKEN in .env
(See reference below on how to generate a Slack API token)
Usage
// Send a simple message to a channel, say "some-channel" (new SlackNotification()) ->channel('some-channel') ->text("Hello!") ->send(); // Send a section (Ref: https://api.slack.com/reference/block-kit/blocks#section) (new SlackNotification()) ->channel('some-channel') ->section() // Starts a section ->fields() // Starts a field in this section ->markdown(":fire: @here This is an emergency :fire:") ->endFields() ->endSection() ->send(); // Send a raw JSON block (example from https://api.slack.com/block-kit/building#block_basics) (new SlackNotification()) ->channel('some-channel') ->block([ "type" => "section", "text" => [ "type" => "mrkdwn", "text" => "New Paid Time Off request from <example.com|Fred Enriquez>\n\n<https://example.com|View request>", ], ]) ->send(); // Compose a message and dump the JSON that'll be sent to Slack. Useful for debugging. (new SlackNotification()) ->channel('some-channel') ->text("Hello!") ->dump(); // Upload a file (new SlackNotification()) ->channel('some-channel') ->file($filePath, 'Some filename') ->upload(); // Optionally, a title, or an accompanying message can be added with a file (new SlackNotification()) ->channel('some-channel') ->file($filePath, 'Some filename') ->withInitialComment('some comment') ->withTitle('some title') ->upload();
APIs:
channel() -> Channel
header() -> Create a header section
context() -> A small footer text
divider() -> A horizontal line
section() / endSection() --> A section block
lists() -> List of items
field() / endfield() --> Inside section
markdown() -> A markdown block, allowed only inside a section
block() -> Pre-composed block
send() -> Send to Slack
dump() -> Dump the final JSON that'd be sent to Slack API
file() -> Upload a file
withInitialComment() -> Add a message with a file upload
withTitle() -> Add a title with file upload
Testing:
Invoke SlackNotification::fake() to ensure HTTP requests to Slack are mocked. Internally it uses Laravel's Http::fake(),
so all available Http::assert* methods can be used for assertions.
Example:
SlackNotification::fake(); (new SlackNotification()) ->channel('channel-1') ->send(); Http::assertSent(static fn(Request $request) => $request['channel'] === 'channel-1');
References:
To create a Slack API token
- Visit https://api.slack.com/apps
- If no app is present, create an app (you may select "from scratch")
- If the app was created earlier, select the app under App Name
- On the left pane, under "Features" click "OAuth & Permissions"
- Under Scopes > Bot Token Scopes, click Add an OAuth Scope
- Add these scopes:
chat.writeandchat.write.public.
(note:channels.read,users.readmay be needed in future versions of this package, but not now) - Click "reinstall your app" in the yellow bar that appears above
- In the dropdown "Search for a channel", select a channel. Any channel would do - won't matter now.
- Allow it
- Copy the "Bot User OAuth Token" and share
Links:
- Slack Block reference docs: https://api.slack.com/block-kit
- Emoji cheat-sheet: https://github.com/ikatyang/emoji-cheat-sheet
- Color bar not supported in block kits yet (ref: https://stackoverflow.com/a/74826061/2014868)
aranyasen/laravel-slack 适用场景与选型建议
aranyasen/laravel-slack 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.38k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 04 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 aranyasen/laravel-slack 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aranyasen/laravel-slack 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.38k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-04