drbiko / slack
一个简单的PHP包(php-slack/slack的分支),用于向Slack发送消息,注重易用性和优雅的语法。
Requires
- php: ^7.0
- ext-json: *
- ext-mbstring: *
- guzzlehttp/guzzle: ~6.0|~5.0|~4.0
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^7.5
README
一个简单的PHP包,用于通过入站Webhook向Slack发送消息,注重易用性和优雅的语法。
支持:PHP 7.0
, 7.1
, 7.2
要求: guzzlehttp/guzzle
任何版本 ~6.0|~5.0|~4.0
这是对流行的、优秀的但已被遗弃的包
maknz/slack
的分支。
快速浏览
composer require alek13/slack
- 创建入站Webhook 并复制
hook_url
$client = new Client('http://hook_url')
$client->to('#general')->send('早上好');
完成!
安装
您可以使用 Composer 包管理器在项目根目录下运行来安装此包。
composer require alek13/slack
入站Webhook
然后,在您的Slack账户上为该包创建一个入站Webhook。您将需要Webhook URL来实例化客户端(或如果使用Laravel,则为配置文件)。
基本用法
实例化客户端
// Instantiate without defaults $client = new Maknz\Slack\Client('https://hooks.slack.com/...'); // 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 Maknz\Slack\Client('https://hooks.slack.com/...', $settings);
设置
默认设置已经相当不错,但您可能希望为客户端设置默认行为,以便用于所有发送的消息。 所有设置都是可选的,您无需提供任何。如果没有提供,我们将回退到Webhook集成上配置的内容,这些内容在Slack上管理,或者使用我们的合理默认值。
发送消息
发送基本消息(预览)
$client->send('Hello world!');
向非默认频道发送消息
$client->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('#operations')->attach([ 'fallback' => 'Server health: good', 'text' => 'Server health: good', 'color' => 'danger', ])->send('New alert from the monitoring system'); // no message, but can be provided if you'd like
发送带有字段的附件(预览)
$client->to('#operations')->attach([ 'fallback' => 'Current server stats', 'text' => 'Current server stats', 'color' => 'danger', 'fields' => [ [ 'title' => 'CPU usage', 'value' => '90%', 'short' => true // whether the field is short enough to sit side-by-side other fields, defaults to false ], [ 'title' => 'RAM usage', 'value' => '2.5GB of 4GB', 'short' => true ] ] ])->send('New alert from the monitoring system'); // no message, but can be provided if you'd like
发送带有作者的附件(预览)
$client->to('@regan')->attach([ 'fallback' => 'Keep up the great work! I really love how the app works.', 'text' => 'Keep up the great work! I really love how the app works.', 'author_name' => 'Jane Appleseed', 'author_link' => 'https://yourapp.com/feedback/5874601', 'author_icon' => 'https://static.pexels.com/photos/61120/pexels-photo-61120-large.jpeg' ])->send('New user feedback');
高级用法
Markdown
默认情况下,Markdown已启用用于消息文本,但禁用于附件字段。此行为可以在设置中或即时配置。
启用或禁用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' => 'danger', 'mrkdwn_in' => ['pretext', 'text'] ])->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);
游乐场
这里有一个简单的控制台脚本 php-slack/playground
,用于测试消息并查看消息的实际外观。
问题
如果您有任何关于如何使用或贡献的问题,
欢迎您加入我们的Slack工作区。
贡献
如果您遇到问题,发现错误,或有功能建议,请登录Github并创建一个问题。如果您想亲自尝试,请Fork这个包并提交一个pull request。请包括任何添加或更改的功能的测试。如果是错误,请包括回归测试。