一个简单的PHP包,用于向Slack发送消息。

v2.4.0 2020-12-21 09:04 UTC

This package is auto-updated.

Last update: 2024-09-21 16:41:48 UTC


README

Total Downloads Latest Stable Version Build Status StyleCI License: MIT

一个简单的PHP包,用于使用入站webhookSlack发送消息。

要求

  • PHP 7.3, 7.4+

安装

1. 在终端中从项目根目录运行
    composer require jeremykenedy/slack
2. 创建Webhook
* [Create an incoming webhook](https://my.slack.com/services/new/incoming-webhook) on your Slack account for the package to use. You'll need the webhook URL to instantiate the client (or for the configuration file if using Laravel).

基本使用

实例化客户端

// Instantiate without defaults
$client = new jeremykenedy\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 @jeremykenedy or #channel will also be linked.
$settings = [
	'username' => 'Cyril',
	'channel' => '#accounting',
	'link_names' => true
];

$client = new jeremykenedy\Slack\Client('https://hooks.slack.com/...', $settings);
设置

默认设置相当不错,但您可能希望为客户端设置默认行为,以便用于发送的所有消息。 所有设置都是可选的,您无需提供任何内容。未提供时,我们将回退到在webhook集成中配置的内容,这些内容在Slack上管理,或我们的合理默认设置。

发送消息

发送基本消息
$client->send('Hello world!');
发送到非默认频道的消息
$client->to('#accounting')->send('Are we rich yet?');
发送给用户的消息
$client->to('@jeremykenedy')->send('Yo!');
以不同的bot名称发送到频道的消息
$client->from('Jake the Dog')->to('@FinnTheHuman')->send('Adventure time!');
发送带有不同图标的消息
// Either with a Slack emoji
$client->to('@jeremykenedy')->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('@jeremykenedy')->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('@jeremykenedy')->send('I am sending this implicitly');

// Explicitly
$message = $client->createMessage();

$message->to('@jeremykenedy')->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上记录问题。如果您想自己尝试,请分支此包并提交拉取请求。请包括任何添加或更改功能的测试。如果是错误,请包括回归测试。

致谢

  • 全部开发致谢应归功于 maknz
  • 此包已被分支并改进。原始包声明它不再维护。
  • 此包已被分支并修改,以符合MIT许可标准,适用于生产使用。

许可

Slack for PHP根据MIT许可证许可,适用于个人和商业产品。祝您使用愉快!