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

1.7.2 2015-08-24 16:17 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:54:43 UTC


README

Build Status Scrutinizer Code Quality

一个简单的PHP包,用于通过Slack入站Webhook发送消息,注重易用性和优雅的语法。开箱即支持Laravel 4和5。

需求

  • PHP 5.4或更高版本

安装

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

composer require sonnygauran/slack

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

Laravel

我们包括服务提供者和外观,以实现易于集成和优雅的语法。

首先,将 Maknz\Slack\SlackServiceProvider 提供者添加到 config/app.php 中的提供者数组(或Laravel 4中的 app/config.php

'providers' => [
  ...
  'Maknz\Slack\SlackServiceProvider',
],

然后将外观添加到您的 aliases 数组中

'aliases' => [
  ...
  'Slack' => 'Maknz\Slack\Facades\Slack',
],

配置

使用以下命令发布配置文件

对于Laravel 5

// Laravel 5, file will be at config/slack.php
php artisan vendor:publish --provider="Maknz\Slack\SlackServiceProviderLaravel5"

通过.env设置使用Slack外观自定义客户端

SLACK_ENDPOINT=https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
SLACK_CHANNEL=#general
SLACK_USERNAME=Slack-News
SLACK_ICON=:rocket:
SLACK_LINK_NAMES=true
SLACK_UNFURL_LINKS=false
SLACK_UNFURL_MEDIA=true
SLACK_MARKDOWN=true

对于Laravel 4

// Laravel 4, file will be at app/config/packages/maknz/slack/config.php
php artisan config:publish maknz/slack

进入文件并配置您希望包使用的默认值。如果任何设置为null,则包将回退到Webhook上的默认设置。

配置文件用于绕过客户端实例化过程,以便更容易使用该包。因此,您可以跳过下面的 实例化客户端 部分,直接开始使用该包。

基本用法

实例化客户端

// Instantiate without defaults
$client = new Maknz\Slack\Client('http://your.slack.endpoint');

// 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('http://your.slack.endpoint', $settings);

设置

所有设置都是可选的,但它们是指定客户端如何行为的一种方便方式。

  • channel:消息将发送到的默认频道
    • 字符串
    • 默认值:Webhook上的设置
  • username:消息将发送的默认用户名
    • 字符串
    • 默认值:Webhook上的设置
  • icon:消息将发送的默认图标,可以是 :emoji: 或图像的URL
    • 字符串
    • 默认值:Webhook上的设置
  • link_names:是否应链接名称,如 @regan 或 #accounting
    • 布尔值
    • 默认值:false
  • unfurl_links:Slack是否应展开基于文本的URL
    • 布尔值
    • 默认值:false
  • unfurl_media:Slack是否应展开基于媒体的URL
    • 布尔值
    • 默认值:true
  • allow_markdown:是否在消息中解析Markdown
    • 布尔值
    • 默认值:true
  • markdown_in_attachments:哪些附件字段应解析Markdown
    • 数组
    • 默认值:[]

发送消息

要发送消息,您将在客户端实例上调用方法,或者如果您在Laravel中使用该包,则使用Slack外观。

发送基本消息

// With an instantiated client
$client->send('Hello world!');

// or the Laravel facade
Slack::send('Hello world!');

向非默认频道发送消息

// With an instantiated client
$client->to('#accounting')->send('Are we rich yet?');

// or the Laravel facade
Slack::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('@regan')->attach([
	'fallback' => 'It is all broken, man', // Fallback text for plaintext clients, like IRC
	'text' => 'It is all broken, man', // The text for inside the attachment
	'pretext' => 'From user: JimBob', // Optional text to appear above the attachment and below the actual message
	'color' => 'bad', // Change the color of the attachment, default is 'good'
])->send('New alert from the monitoring system');

带有字段的附件

$client->to('#operations')->attach([
	'fallback' => 'It is all broken, man',
	'text' => 'It is all broken, man',
	'pretext' => 'From user: JimBob',
	'color' => 'bad',
	'fields' => [
		[
			'title' => 'Metric 1',
			'value' => 'Some value'
		],
		[
			'title' => 'Metric 2',
			'value' => 'Some value',
			'short' => true // whether the field is short enough to sit side-by-side other fields, defaults to false
		]
	]
])->send('New alert from the monitoring system');

动态修改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' => 'bad',
	'mrkdwn_in' => ['pretext', 'text']
])->send('New alert from the monitoring system');

发送带有作者的附件

$client->to('@regan')->attach([
	'fallback' => 'Things are looking good',
	'text' => 'Things are looking good',
	'author_name' => 'Bobby Tables',
	'author_link' => 'http://flickr.com/bobby/',
	'author_url' => 'http://flickr.com/icons/bobby.jpg'
])->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);

贡献

如果您遇到问题、发现错误或对功能有建议,请登录并在GitHub上提交一个问题。如果您想亲自尝试,请分叉包并提交一个pull request。请包括任何新增或更改功能的测试。如果是错误,请包括回归测试。