yuki777/slack

一个简单的PHP包,用于向Slack发送消息,注重易用性和优雅的语法。

v2.2.4 2021-03-13 07:02 UTC

README

一个简单的PHP包,用于通过Slack入站Webhook发送消息,注重易用性和优雅的语法。

Latest Stable Version Latest Unstable Version License

Total Downloads Monthly Downloads Daily Downloads

Build Status Scrutinizer Code Quality Code Climate Coverage Status

此存储库是从流行的maknz/slack库的分支开始的,该库已不再维护

1.x分支及其所有相关版本是原始存储库的精确副本,并受BSD 2-clause "Simplified" License的约束。

下一个版本将采用MIT许可。有关更多详细信息,请参阅当前的LICENSE文件。

需求

  • PHP 7.1+

安装

您可以使用Composer包管理器安装此包。您可以在项目根目录中运行以下命令进行安装

composer require nexylan/slack php-http/guzzle6-adapter

为什么选择 php-http/guzzle6-adapter? 我们通过HTTPlug与任何HTTP消息客户端解耦。

然后在您的Slack账户上为包创建入站Webhook。您将需要Webhook URL来实例化客户端(或在Laravel中使用配置文件)。

基本用法

实例化客户端

// Instantiate without defaults
$client = new Nexy\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 Nexy\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!');

以不同的bot名称向频道发送消息(《预览”)

$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((new \Nexy\Slack\Attachment())
    ->setFallback('Server health: good')
    ->setText('Server health: good')
    ->setColor('danger')
)->send('New alert from the monitoring system'); // no message, but can be provided if you'd like

带有字段的附件(《预览”)

$client->to('#operations')->attach((new \Nexy\Slack\Attachment())
    ->setFallback('Current server stats')
    ->setText('Current server stats')
    ->setColor('danger')
    ->setFields([
        new \Nexy\Slack\AttachmentField(
            'Cpu usage',
            '90%',
            true // whether the field is short enough to sit side-by-side other fields, defaults to false
        ),
        new \Nexy\Slack\AttachmentField('RAM usage', '2.5GB of 4GB', true),
    ])
)->send('New alert from the monitoring system'); // no message, but can be provided if you'd like

带有作者的附件(《预览”)

$client->to('@regan')->attach((new \Nexy\Slack\Attachment())
    ->setFallback('Keep up the great work! I really love how the app works.')
    ->setText('Keep up the great work! I really love how the app works.')
    ->setAuthorName('Jan Appleseed')
    ->setAuthorLink('https://yourapp.com/feedback/5874601')
    ->setAuthorIcon('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((new \Nexy\Slack\Attachment())
    ->setFallback('It is all broken, man')
    ->setText('It is _all_ broken, man')
    ->setPretext('From user: *JimBob*')
    ->setColor('danger')
    ->setMarkdownFields(['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())
    ->setFallback('Some fallback text')
    ->setText('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();

如果您有很多附件和字段,您也可以直接设置它们

// 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这个包并提交拉取请求。请包括任何新增或更改功能的测试。如果是错误,请包括回归测试。