seedboxtech / slack
一个简单的PHP包,用于向Slack发送消息,侧重于易用性和优雅的语法。
该包的官方仓库似乎已不存在,因此该包已被冻结。
Requires
- php: >=5.5.0
- ext-mbstring: *
- guzzlehttp/guzzle: ~6.0|~5.0|~4.0
Requires (Dev)
- illuminate/queue: 5.*
- mockery/mockery: 0.9.*
- phpunit/phpunit: 4.2.*
- satooshi/php-coveralls: ~1.0
Suggests
- nexylan/slack-bundle: Required for Symfony bundle support
- razorpay/slack-laravel: Required for Laravel support
- dev-master
- 2.0.0
- 1.9.9
- 1.9.5
- 1.9.4
- 1.9.3
- 1.9.2
- 1.9.1
- 1.9.0
- v1.8.1
- v1.8.0
- 1.7.0
- 1.6.0
- 1.5.0
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.0
- 0.3.0
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.1
- 0.1.0
- dev-laravel5.4
- dev-f/6
- dev-queue_job_class
- dev-laravel_5.4_queue_changes
- dev-f/queue-with-retry
- dev-queue
- dev-f/queue-with-connections
- dev-f/queue_configurable
This package is not auto-updated.
Last update: 2020-04-28 04:53:42 UTC
README
一个简单的PHP包,用于通过Slack的接收Webhook发送消息,侧重于易用性和优雅的语法。
- Laravel集成: Slack for Laravel
- Symfony集成: NexySlackBundle
要求
- PHP 5.5, 5.6, 7.0或HHVM
安装
您可以使用Composer包管理器安装此包。您可以在项目根目录中运行此命令进行安装
composer require seedboxtech/slack
然后,在您的Slack账户中为该包创建一个接收Webhook,供包使用。您需要Webhook URL来实例化客户端(或使用Laravel时的配置文件)。
基本用法
实例化客户端
// Instantiate without defaults $client = new Seedboxtech\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 Seedboxtech\Slack\Client('https://hooks.slack.com/...', $settings);
设置
默认设置相当不错,但您可能希望为客户端设置默认行为,以便用于发送的所有消息。 所有设置都是可选的,您不需要提供任何。未提供时,我们将回退到Webhook集成上的配置,这些配置在Slack上管理,或使用我们合理的默认值。
字段 | 类型 | 描述 |
---|---|---|
channel |
字符串 | 消息将被发送到的默认频道 |
username |
字符串 | 您的机器人的默认用户名 |
icon |
字符串 | 消息将发送的默认图标,可以是:emoji: 或指向图片的URL |
link_names |
布尔值 | 是否应在消息中链接如@regan 或#accounting 之类的名称(默认为false) |
unfurl_links |
布尔值 | Slack是否应展开基于文本的URL(默认为false) |
unfurl_media |
布尔值 | Slack是否应展开基于媒体的URL,如推文或YouTube视频(默认为true) |
allow_markdown |
布尔值 | 是否应在消息中解析markdown,或将其保留为纯文本(默认为true) |
markdown_in_attachments |
数组 | 哪些附件字段应解析markdown(默认为无) |
发送消息
发送基本消息(预览)
$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('#accounting')->withIcon('http://example.com/accounting.png')->queue('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();
通过队列发送
// $queue is an instance of Illuminate\Queue\QueueManager $client = new Seedboxtech\Slack\Client('http://your.slack.endpoint', $settings, $queue); $client->to('@regan')->queue('I am sending this implicitly');
如果您正在使用 Laravel 服务提供程序,则默认队列将自动使用。
附件
在使用附件时,最简单的方法是提供如示例中所示的数据数组,实际上这些数据在底层被转换为附件对象。您还可以将附件对象附加到消息中
$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该包并提交拉取请求。请为添加或更改的功能添加测试。如果是错误,请包含回归测试。