nguyenanhung/slack
Composer 安装命令:
composer require nguyenanhung/slack
包简介
A simple PHP package for sending messages to Slack, with a focus on ease of use and elegant syntax. Includes Laravel support out of the box.
README 文档
README
A simple PHP package for sending messages to Slack with incoming webhooks, focussed on ease-of-use and elegant syntax. Includes Laravel 4 and 5 support out of the box.
Requirements
- PHP 5.4 or greater
Version
- V1.x support all PHP version
>=5.4 - V2.x support all PHP version
>=7.0
Installation
You can install the package using the Composer package manager. You can install it by running this command in your project root:
composer require nguyenanhung/slack
Then create an incoming webhook on your Slack account for the package to use. You'll need the webhook URL to instantiate the client (or for the configuration file if using Laravel).
Laravel
We include service providers and a facade for easy integration and a nice syntax for Laravel.
Firstly, add the nguyenanhung\Slack\SlackServiceProvider provider to the providers array in config/app.php (or app/config.php for Laravel 4)
'providers' => [ ... 'nguyenanhung\Slack\SlackServiceProvider', ],
and then add the facade to your aliases array
'aliases' => [ ... 'Slack' => 'nguyenanhung\Slack\Facades\Slack', ],
Configuration
Publish the configuration file with:
// Laravel 5, file will be at config/slack.php php artisan vendor:publish // Laravel 4, file will be at app/config/packages/nguyenanhung/slack/config.php php artisan config:publish nguyenanhung/slack
Head into the file and configure the defaults you'd like the package to use. If null is set for any, the package will fall back on the default set on the webhook.
The configuration file is used to bypass the client instantiation process to make using the package easier. Therefore, you can skip the the Instantiate the client section below and dive right into using the package.
Basic Usage
Instantiate the client
// Instantiate without defaults $client = new nguyenanhung\Slack\Client('http://your.slack.endpoint'); // Instantiate with defaults, so all messages created // will be sent from 'Cyril' and to the #accounting channel // by default. Any names like @regan or #channel will also be linked. $settings = [ 'username' => 'Cyril', 'channel' => '#accounting', 'link_names' => true ]; $client = new nguyenanhung\Slack\Client('http://your.slack.endpoint', $settings);
Settings
All settings are optional, but are a convenient way of specifying how the client should behave beyond the defaults.
channel: the default channel that messages will be sent to- string
- default: the setting on the webhook
username: the default username that messages will be sent from- string
- default: the setting on the webhook
icon: the default icon messages will be sent with, either :emoji: or a URL to an image- string
- default: the setting on the webhook
link_names: whether names like @regan or #accounting should be linked- bool
- default:
false
unfurl_links: whether Slack should unfurl text-based URLs- bool
- default:
false
unfurl_media: whether Slack should unfurl media-based URLs- bool
- default:
true
allow_markdown: whether Markdown should be parsed in messages- bool
- default:
true
markdown_in_attachments: which attachment fields should have Markdown parsed- array
- default:
[]
Sending messages
To send messages, you will call methods on your client instance, or use the Slack facade if you are using the package in Laravel.
Sending a basic message
// With an instantiated client $client->send('Hello world!'); // or the Laravel facade Slack::send('Hello world!');
Sending a message to a non-default channel
// With an instantiated client $client->to('#accounting')->send('Are we rich yet?'); // or the Laravel facade Slack::to('#accounting')->send('Are we rich yet?');
Sending a message to a user
$client->to('@regan')->send('Yo!');
Sending a message to a channel as a different username
$client->from('Jake the Dog')->to('@FinnTheHuman')->send('Adventure time!');
Sending a message with a different icon
// Either with a Slack emoji $client->to('@regan')->withIcon(':ghost:')->send('Boo!'); // or a URL $client->to('#accounting')->withIcon('http://example.com/accounting.png')->send('Some accounting notification');
Send an attachment
$client->to('@regan')->attach([ 'fallback' => 'It is all broken, man', // Fallback text for plaintext clients, like IRC 'text' => 'It is all broken, man', // The text for inside the attachment 'pretext' => 'From user: JimBob', // Optional text to appear above the attachment and below the actual message 'color' => 'bad', // Change the color of the attachment, default is 'good' ])->send('New alert from the monitoring system');
Send an attachment with fields
$client->to('#operations')->attach([ 'fallback' => 'It is all broken, man', 'text' => 'It is all broken, man', 'pretext' => 'From user: JimBob', 'color' => 'bad', 'fields' => [ [ 'title' => 'Metric 1', 'value' => 'Some value' ], [ 'title' => 'Metric 2', 'value' => 'Some value', 'short' => true // whether the field is short enough to sit side-by-side other fields, defaults to false ] ] ])->send('New alert from the monitoring system');
Send a message modifying Markdown parsing on the fly
$client->to('#weird')->disableMarkdown()->send('Disable *markdown* just for this message'); $client->to('#general')->enableMarkdown()->send('Enable _markdown_ just for this message');
Send an attachment specifying Markdown parsing on the fly
$client->to('#operations')->attach([ 'fallback' => 'It is all broken, man', 'text' => 'It is _all_ broken, man', 'pretext' => 'From user: *JimBob*', 'color' => 'bad', 'mrkdwn_in' => ['pretext', 'text'] ])->send('New alert from the monitoring system');
Send an attachment with an author
$client->to('@regan')->attach([ 'fallback' => 'Things are looking good', 'text' => 'Things are looking good', 'author_name' => 'Bobby Tables', 'author_link' => 'http://flickr.com/bobby/', 'author_url' => 'http://flickr.com/icons/bobby.jpg' ])->send('New alert from the monitoring system');
Advanced usage
Explicit message creation
For convenience, message objects are created implicitly by calling message methods on the client. We can however do this explicitly to avoid hitting the magic method.
// Implicitly $client->to('@regan')->send('I am sending this implicitly'); // Explicitly $message = $client->createMessage(); $message->to('@regan')->setText('I am sending this explicitly'); $message->send();
Attachments
When using attachments, the easiest way is to provide an array of data as shown in the examples, which is actually converted to an Attachment object under the hood. You can also attach an Attachment object to the message:
$attachment = new Attachment([ 'fallback' => 'Some fallback text', 'text' => 'The attachment text' ]); // Explicitly create a message from the client // rather than using the magic passthrough methods $message = $client->createMessage(); $message->attach($attachment); // Explicitly set the message text rather than // implicitly through the send method $message->setText('Hello world')->send();
Each attachment field is also an object, an AttachmentField. They can be used as well instead of their data in array form:
$attachment = new Attachment([ 'fallback' => 'Some fallback text', 'text' => 'The attachment text', 'fields' => [ new AttachmentField([ 'title' => 'A title', 'value' => 'A value', 'short' => true ]) ] ]);
You can also set the attachments and fields directly if you have a whole lot of them:
// implicitly create a message and set the attachments $client->setAttachments($bigArrayOfAttachments); // or explicitly $client->createMessage()->setAttachments($bigArrayOfAttachments);
$attachment = new Attachment([]); $attachment->setFields($bigArrayOfFields);
Contributing
If you're having problems, spot a bug, or have a feature suggestion, please log and issue on Github. If you'd like to have a crack yourself, fork the package and make a pull request. Please include tests for any added or changed functionality. If it's a bug, include a regression test.
nguyenanhung/slack 适用场景与选型建议
nguyenanhung/slack 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.2k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 03 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「slack」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nguyenanhung/slack 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nguyenanhung/slack 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nguyenanhung/slack 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Highly Opinionated Symfony3/4 Deployer Recipe
This bundle provides integration with the Slack API library, allowing you to interact with the Slack API from within your Symfony projects
Wrapper for Slack Web API
Alfabank REST API integration
This is my package support-tickets-notifications
Console commands
统计信息
- 总下载量: 19.2k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-2-Clause
- 更新时间: 2020-03-06