nexylan/slack

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

v3.2.0 2021-03-13 16:26 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" 许可协议

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

需求

  • PHP 7.3+

安装

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

composer require nexylan/slack

我们还遵循PSR-7PSR-17PSR-18标准进行HTTP消息。它允许您使用遵循此约定的任何HTTP客户端,并使库的维护性对我们更好。

因此,您需要安装遵循这些标准的HTTP客户端。我们推荐使用流行的HTTPlug项目,但您可以根据自己的需求选择更方便的选项。

然后,在您的Slack帐户上创建一个入站webhook,以便包可以使用。您需要webhook URL来实例化客户端(或用于Laravel的配置文件)。

基本用法

实例化客户端

在这个例子中,我们使用HTTPlug发现组件来引入所需的PSR工具。

use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;

use Nexy\Slack\Client;

$client = new Client(
    Psr18ClientDiscovery::find(),
    Psr17FactoryDiscovery::findRequestFactory(),
    Psr17FactoryDiscovery::findStreamFactory(),
    'https://hooks.slack.com/...',
    [
        'username' => 'Cyril', // Default messages are sent from 'Cyril'
        'channel' => '#accounting', // Default messages are sent to '#accounting'
        'link_names' => true
    ]
);

注意:最后一个参数$option是可选的。

设置

默认设置相当不错,但您可能希望为您的客户端设置默认行为,以便用于所有发送的消息。**所有设置都是可选的,您无需提供任何设置**。如果未提供,我们将回退到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 包并提交拉取请求。请包括任何添加或更改的功能的测试。如果是错误,请包括回归测试。