nwilging/laravel-slack-bot
Composer 安装命令:
composer require nwilging/laravel-slack-bot
包简介
A robust Slack messaging integration for Laravel
README 文档
README
A robust Slack messaging integration for Laravel
About
While Slack Incoming Webhooks are powerful, direct API interaction is powerful-er - or something like that.
This package leverages Slack bot tokens to interact with the Slack API. This allows the integration to do things such as search channels and post messages - both plain and rich text - to any channel* in a workspace.
* any public channel, or private channel that the Slack App bot is a member of
Installation
Pre Requisites
- Laravel v8+
- PHP 7.4+
Install with Composer
composer require nwilging/laravel-slack-bot
Usage with laravel/slack-notification-channel
If your app uses the default first-party Laravel package,
laravel/slack-notification-channel,
this package will conflict with the first-party one. This package is configured by default
to use the channel slack in your via() method.
If you wish to use this package alongside laravel/slack-notification-channel, simply
add the following to your .env:
SLACK_API_DRIVER_NAME=slackBot
You may replace slackBot with any driver name you'd like. The driver will be instantiated
with that name and you can provide it to your via() method. This will allow you to use
both this package and laravel/slack-notification-channel at the same time.
Example:
<?php namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Notification; use Nwilging\LaravelSlackBot\Contracts\Notifications\SlackApiNotificationContract; class SlackNotification extends Notification implements SlackApiNotificationContract { use Queueable; public function via($notifiable) { return ['slackBot'] } public function toSlackArray($notifiable): array { return [ 'contentType' => 'text', 'message' => 'test plain text message', 'channelId' => 'C012345', 'options' => [], // Message Options ]; } }
Configure
Use this guide to create your Slack App. Most integrations will use the following scopes. You may add additional OAuth scopes if needed.
channels:readchat:writechat:write.customizegroups:readgroups:writeim:readmpim:read
To configure your .env, simply add the following variable:
SLACK_API_BOT_TOKEN=xoxb-your-bot-token
Basic Usage
Message Examples
The SlackApiNotificationContract
Your notification must implement the interface
Nwilging\LaravelSlackBot\Contracts\Notifications\SlackApiNotificationContract.
Basic Usage
This package can be used automatically with Laravel notifications. Add slack to the
via() array of your notification and a toSlack() method that returns an array:
<?php namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Notification; use Nwilging\LaravelSlackBot\Contracts\Notifications\SlackApiNotificationContract; class SlackNotification extends Notification implements SlackApiNotificationContract { use Queueable; public function via($notifiable) { return ['slack']; // Or, `via['slackBot']` if you have configured this in .env } public function toSlackArray($notifiable): array { return [ 'contentType' => 'text', 'message' => 'test plain text message', 'channelId' => 'C012345', 'options' => [], // Message Options ]; } }
The channelId here is the ID or name of the channel you wish to send to.
Read more on Usage for information on the SlackApiService which can provide you channel
data (including ID) by a channel name.
Using alongside laravel/slack-notification-channel
<?php namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Notification; use Nwilging\LaravelSlackBot\Contracts\Notifications\SlackApiNotificationContract; class SlackNotification extends Notification implements SlackApiNotificationContract { use Queueable; public function via($notifiable) { return [ 'slack', // Using laravel/slack-notification-channel driver 'slackBot', // Using nwilging/laravel-slack-bot driver ]; } /** * The method to build a slack message for laravel/slack-notification-channel */ public function toSlack($notifiable) { // } /** * The method to build a slack message for nwilging/laravel-slack-bot */ public function toSlackArray($notifiable): array { return [ 'contentType' => 'text', 'message' => 'test plain text message', 'channelId' => 'C012345', 'options' => [], // Message Options ]; } }
Advanced Usage
This package also provides a SlackApiService and a handful of supporting classes such
as layout builders, layout blocks, elements, and composition objects. The most common usage
outside of sending plain text messages would be to send rich text messages using the layout
builders and related components. You can also go further to make use of the SlackApiService.
Slack API Service
Contract: Nwilging\LaravelSlackBot\Contracts\SlackApiServiceContract
Methods:
listConversations- Returns a paginated list of channels and groupsint $limit = 100- Page limit (default 100)?string $cursor = null- Page cursor, used to fetch a new pagebool $excludeArchived = false- Flag to exclude archived channels and groups?string $teamId = null- The team ID to list conversations for (required if token belongs to an org-wide app)bool $includePrivate = true- Flag to include private channels and groups (will only show channels the app is a member of)
getChannelByName- Retrieves channel data using a given namestring $name- The name of the channel to retrieve
sendTextMessage- Send a plain text messagestring $channelId- The ID of the channel to send tostring $message- The message text to send (may include markdown)array $options = []- Message options
sendBlocksMessage- Send a message using layout blocksstring $channelId- The ID of the channel to send toarray $blocks- Array ofBlocksarray $options = []- Message options
Message Options
Message options should be passed as an array with the following schema:
{
username?: string,
icon?: {
emoji?: string,
url?: string,
},
unfurl?: {
'media': bool,
'links': bool,
},
thread?: {
ts?: string,
send_to_channel?: bool,
},
link_names?: bool,
metadata?: array,
parse?: string,
markdown?: bool,
}
These options are essentially the optional arguments from chat.postMessage.
There is a SlackOptionsBuilder support class that makes building this array more expressive:
use Nwilging\LaravelSlackBot\Support\SlackOptionsBuilder; $builder = new SlackOptionsBuilder(); $builder ->username('My Bot') // Set a custom username ->iconUrl('https://...') // URL to icon (overrides iconEmoji) ->iconEmoji(':white_check_mark:') // Sets icon to an emoji ->unfurlMedia() ->unfurlLinks() ->threadTs('ThreadTS') ->threadReplySendToChannel() // Whether or not to send reply to channel when replying to a thread ->linkNames() ->parse('...') ->markdown() // Enable markdown (or disable by passing `false`) ->metadata([]); // Pass this to `SlackApiService` $apiServiceCompliantOptionsArray = $builder->toArray();
Layout Blocks
The layout blocks implementation generally follows Slack's layout blocks design.
Layout Builder
There is a layout Builder located at Nwilging\LaravelSlackBot\Support\LayoutBuilder\Builder,
implementing contract Nwilging\LaravelSlackBot\Contracts\Support\LayoutBuilder\BuilderContract.
The builder aims to provide expressive methods to build basic, yet robust, messages. This
is a useful tool for building your notification in its toSlack() method.
use Nwilging\LaravelSlackBot\Support\LayoutBuilder\Builder; use Nwilging\LaravelSlackBot\Support\LayoutBlocks\Blocks; use Nwilging\LaravelSlackBot\Support\LayoutBlocks\Elements; $layouBuilder = new Builder(); // Create a button element with text $buttonElement = new Elements\ButtonElement( $layoutBuilder->withPlainText('Button Text'), // Helper method to create a TextObject 'action-id' ); // Add the button element to an actions block $actionsBlock = new Blocks\ActionsBlock([$buttonElement]); $layoutBuilder ->header('Header Text') ->divider() ->addBlock($actionsBlock); // Pass this to `sendBlocksMessage` $apiServiceCompliantBlocksArray = $layoutBuilder->getBlocks();
Handling Slash Commands
Slack Slash Commands can be handled via this package and command handlers that are configured in your application.
The Nwilging\LaravelSlackBot\Services\SlackCommandHandlerService has a handle method
which accepts a Request object. This service will validate the incoming message using the
request headers and your supplied signing secret, and will then attempt to handle the command
request with a registered command handler.
Creating Command Handlers
Command handlers should implement the interface Nwilging\LaravelSlackBot\Contracts\SlackCommandHandlerContract.
An example handler:
<?php declare(strict_types=1); namespace App; use Nwilging\LaravelSlackBot\Support\SlackCommandRequest; use Symfony\Component\HttpFoundation\Response; class TestCommandHandler implements SlackCommandHandlerContract { public function handle(SlackCommandRequest $commandRequest): Response { return response('OK'); } }
Registering Command Handlers
Command handlers should be registered in a service provider so that they are available
once the application boots. You may do this in any service provider that is already
registered in your application. Place the command handler registrations inside the
register method of your service provider.
<?php namespace App\Providers; use App\TestCommandHandler; use Illuminate\Support\ServiceProvider; use Nwilging\LaravelSlackBot\Contracts\Services\SlackCommandHandlerFactoryServiceContract; class AppServiceProvider extends ServiceProvider { public function register() { /** @var SlackCommandHandlerFactoryServiceContract $slackCommandFactory */ $slackCommandFactory = $this->app->make(SlackCommandHandlerFactoryServiceContract::class); $slackCommandFactory->register(TestCommandHandler::class, 'command-name'); } }
nwilging/laravel-slack-bot 适用场景与选型建议
nwilging/laravel-slack-bot 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 49.94k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2022 年 04 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 nwilging/laravel-slack-bot 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nwilging/laravel-slack-bot 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 49.94k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 18
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-04-22