nguyenanhung / slack
一个简单的PHP包,用于向Slack发送消息,注重易用性和优雅的语法。内置Laravel支持。
Requires
- php: >=7.0
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- guzzlehttp/guzzle: ^7.4.3 || ^6.5.6
Requires (Dev)
- roave/security-advisories: dev-latest
Suggests
- illuminate/support: Required for Laravel support
README
一个简单的PHP包,用于通过 Slack 的 接收Webhook 发送消息,注重易用性和优雅的语法。内置Laravel 4和5支持。
要求
- PHP 5.4或更高版本
版本
- V1.x支持所有PHP版本
>=5.4
- V2.x支持所有PHP版本
>=7.0
安装
您可以使用 Composer 包管理器安装此包。在项目根目录运行以下命令即可安装:
composer require nguyenanhung/slack
然后,在您的Slack帐户上为该包创建一个 接收Webhook。您需要该Webhook URL来实例化客户端(或使用Laravel时用于配置文件)。
Laravel
我们包括服务提供者和外观,以便轻松集成并拥有优雅的语法。
首先,将 nguyenanhung\Slack\SlackServiceProvider
提供者添加到 config/app.php
中的提供者数组(或Laravel 4中的 app/config.php
)
'providers' => [ ... 'nguyenanhung\Slack\SlackServiceProvider', ],
然后,将外观添加到您的 aliases
数组
'aliases' => [ ... 'Slack' => 'nguyenanhung\Slack\Facades\Slack', ],
配置
使用以下命令发布配置文件
// 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
进入文件并配置您希望包使用的默认值。如果任何值设置为 null
,则包将回退到Webhook上设置的默认值。
配置文件用于绕过客户端实例化过程,以便更容易使用该包。因此,您可以跳过下面的 实例化客户端 部分,直接使用该包。
基本用法
实例化客户端
// 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);
设置
所有设置都是可选的,但它们是指定客户端应如何行为(超出默认设置)的方便方式。
channel
: 消息将发送到的默认频道- 字符串
- 默认值: Webhook上的设置
username
: 消息将发送的默认用户名- 字符串
- 默认值: Webhook上的设置
icon
: 消息将发送的默认图标,可以是 :emoji: 或指向图像的URL- 字符串
- 默认值: Webhook上的设置
link_names
: 是否应链接类似于 @regan 或 #accounting 的名称- 布尔值
- 默认值:
false
unfurl_links
: Slack是否应展开基于文本的URL- 布尔值
- 默认值:
false
unfurl_media
: Slack是否应展开基于媒体的URL- 布尔值
- 默认值:
true
allow_markdown
: 是否应在消息中解析Markdown- 布尔值
- 默认值:
true
markdown_in_attachments
: 哪些附件字段应解析Markdown- 数组
- 默认值:
[]
发送消息
要发送消息,您将调用客户端实例上的方法,或者如果您在Laravel中使用该包,则使用 Slack
外观。
发送基本消息
// With an instantiated client $client->send('Hello world!'); // or the Laravel facade Slack::send('Hello world!');
向非默认频道发送消息
// With an instantiated client $client->to('#accounting')->send('Are we rich yet?'); // or the Laravel facade Slack::to('#accounting')->send('Are we rich yet?');
向用户发送消息
$client->to('@regan')->send('Yo!');
以不同的用户名向频道发送消息
$client->from('Jake the Dog')->to('@FinnTheHuman')->send('Adventure time!');
使用不同的图标发送消息
// 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');
发送附件
$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');
带有字段的发送附件
$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');
发送动态修改Markdown解析的消息
$client->to('#weird')->disableMarkdown()->send('Disable *markdown* just for this message'); $client->to('#general')->enableMarkdown()->send('Enable _markdown_ just for this message');
指定动态Markdown解析的发送附件
$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');
带有作者的发送附件
$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');
高级用法
显式消息创建
为了方便,客户端通过调用消息方法隐式创建消息对象。但是,我们可以显式地这样做,以避免触发魔法方法。
// 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();
附件
使用附件时,最简单的方法是提供如示例中所示的数据数组,实际上在底层被转换为附件对象。您也可以将附件对象附加到消息中
$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();
每个附件字段也是一个对象,即附件字段。它们也可以用来代替数组形式的数据
$attachment = new Attachment([ 'fallback' => 'Some fallback text', 'text' => 'The attachment text', 'fields' => [ new AttachmentField([ 'title' => 'A title', 'value' => 'A value', 'short' => true ]) ] ]);
如果您有很多附件和字段,也可以直接设置它们
// implicitly create a message and set the attachments $client->setAttachments($bigArrayOfAttachments); // or explicitly $client->createMessage()->setAttachments($bigArrayOfAttachments);
$attachment = new Attachment([]); $attachment->setFields($bigArrayOfFields);
贡献
如果您遇到问题、发现错误或有功能建议,请在Github上记录问题。如果您想亲自尝试,请fork该包并提交拉取请求。请为任何新增或更改的功能包含测试。如果是错误,请包含回归测试。