mcrumm / phlack
PHP中的Slack API和WebHook集成
Requires
- php: >=5.4
- doctrine/collections: >=1.0
- guzzle/guzzle: ~3.8
- symfony/options-resolver: ~2.4|3.0.*
Requires (Dev)
- coduo/phpspec-data-provider-extension: ^1.0
- henrikbjorn/phpspec-code-coverage: ~0.2
- phpspec/phpspec: ^2.4.0
- symfony/console: ~2.4|3.0.*
- symfony/expression-language: ~2.4|3.0.*
- symfony/http-foundation: ~2.4|3.0.*
- symfony/http-kernel: ~2.4|3.0.*
Suggests
- symfony/console: Required to use the ConsoleAdapter
- symfony/expression-language: Required to use ExpressionBots
- symfony/http-foundation: Required to use the RequestAdapter
README
Phlack简化了在PHP中创建Slack集成的过程。
安装
通过 composer
composer require mcrumm/phlack
基本用法
发送消息
<?php $phlack = new Crummy\Phlack\Phlack('https://my.webhook.url'); $response = $phlack->send('Hello, from Phlack!'); if (200 === $response['status']) { echo 'Success!'; }
高级用法
旧版WebHook URL
早期版本的Incoming Webhooks使用了一个通用的webhook路径,适用于所有团队。如果你的webhook URL以类似 myteam.slack.com
的内容开始,请将你的团队名称和Incoming Webhook token提供给Phlack,它会完成剩下的工作。
<?php $phlack = new Crummy\Phlack\Phlack([ 'username' => 'myteam', 'token' => 'my_webhook_token' ]);
工厂方法
如果您愿意,可以通过其静态 factory()
方法实例化Phlack。
<?php $phlack = Crummy\Phlack\Phlack::factory($config);
创建新实例
除了webhook URL或数组配置之外,Phlack还会接受一个 PhlackClient
实例作为构造函数参数。
<?php $client = new Crummy\Phlack\Bridge\Guzzle\PhlackClient('https://my.webhook.url'); $phlack = new Crummy\Phlack\Phlack($client);
注意:构造函数和工厂方法接受相同类型的参数:表示webhook URL的字符串、客户端配置选项的数组,或
PhlackClient
对象。
❤️ 对于Guzzle
PhlackClient只是一个使用 Guzzle 实现的web服务客户端。查看其 服务描述 了解更多详情。
消息
消息代表了Slack的Incoming WebHook集成的有效负载。
创建消息
可以使用提供的构建器创建消息,或者直接实例化。
消息构建器
MessageBuilder 允许以编程方式创建消息对象。
<?php // ... $messageBuilder = $phlack->getMessageBuilder(); $messageBuilder ->setText('I was created in the MessageBuilder') ->setChannel('testing') ->setIconEmoji('ghost'); $message = $messageBuilder->create();
您还可以直接使用 MessageBuilder
来创建 Message
对象并添加附件。 MessageBuilder
支持方法链,允许将多个 Attachment
对象添加到单个消息中。
$messageBuilder = $phlack->getMessageBuilder(); // Get the MessageBuilder $messageBuilder ->setText('This message contains multiple attachments.') // Message text. ->createAttachment() // Returns the AttachmentBuilder. ->setTitle($title) ->setTitleLink($link) ->setPretext($pretext) ->setText($body) ->setColor($color) ->setFallback($title . ' ' . $pretext) ->end() // Creates the first attachment and returns the MessageBuilder ->setUsername($username) // Sets username on the Message object. ->createAttachment() // Returns the AttachmentBuilder. ->setTitle('Attachment #2') ->setFallback('Attachment #2 for example purposes') ->setText('Add multiple attachments to a Phlack Message via method chaining.') ->end() // Creates the second attachment and returns the MessageBuilder. ; $message = $messageBuilder->create();
注意: 以这种方式添加附件时,您必须为每个附件调用一次
end()
,这样MessageBuilder
就知道要创建Attachment
对象并返回自身以便进一步修改。
附件构建器
如果您愿意,可以使用 AttachmentBuilder 以独立的方式。
<?php // ... // Get the AttachmentBuilder $attachmentBuilder = $phlack->getAttachmentBuilder(); // Create the Attachment $attachment = $attachmentBuilder ->setTitle('My Attachment Title') ->setTitleLink('http://www.example.com') ->setPretext('Some optional pretext') ->setText('This is the body of my attachment') ->setColor($color) ->addField('Field 1', 'Some Value', true) ->setFallback($title . ' ' . $pretext) ->create() ; // Create a Message to contain the Attachment $message = new \Crummy\Phlack\Message\Message('This message contains an attachment.'); // Add the Attachment to the Message $message->addAttachment($attachment);
消息对象
消息 可以仅通过 text
值进行实例化。
<?php //... use Crummy\Phlack\Message\Message; $message = new Message('Hello, from phlack!'); echo 'The message payload: ' . PHP_EOL: echo $message; // Output: {"text": "Hello, from phlack!"}
但您也可以在构造消息时设置可选参数。
<?php //... use Crummy\Phlack\Message\Message; $message = new Message('Hello, from phlack!', '#random'); echo 'The message payload: ' . PHP_EOL: echo $message; // Output: {"text": "Hello, from phlack!", "channel": "#random"}
发送消息
使用 Phlack 的 send()
命令来发送消息。
<?php // ... $response = $phlack->send($message); if (200 != $response['status']) { die('FAIL! - ' . $response['text']); } echo 'The message was sent: ' . $message;
自定义消息
可以通过使用有效的参数数组来发送自定义消息。
<?php $phlack->send([ 'channel' => '#random', 'icon_emoji' => ':taco:', 'username' => 'Phlack', 'unfurl_links' => true, 'text' => 'I :heart: the <http://api.slack.com|Slack API>!', ]);
注意: 不会对自定义消息参数进行输入验证。您负责自行格式化通道、表情符号和文本数据。
消息响应
MessageResponse 哈希包含响应中的 status
、reason
和 text
。
来自 Incoming Webhooks 集成的响应非常简单。成功消息将简单地返回 status
为 200
。错误消息将在响应 text
和 reason
中包含更多详细信息。
更多示例
有关更多用例,请参阅 示例目录。
Slack API
通过 ApiClient 提供对 Slack API 的程序化访问。
注意: 目前,仅支持 Bearer Token 身份验证方法。对于 OAuth2 支持的贡献将非常受欢迎。
获取客户端
通过使用包含您的 API 令牌的哈希来实例化它,或通过向其 factory
方法传递配置哈希来获取 ApiClient 对象。
通过 factory()
<?php use Crummy\Phlack\Bridge\Guzzle\ApiClient; $slack = ApiClient::factory([ 'token' => 'my_bearer_token' ]);
通过 new ApiClient()
<?php use Crummy\Phlack\Bridge\Guzzle\ApiClient; $slack = new ApiClient([ 'token' => 'my_bearer_token' ]);
API 方法
目前实现的方法包括
请参阅客户的服务描述以获取API方法返回的信息。
示例:列出所有频道
<?php use Crummy\Phlack\Bridge\Guzzle\ApiClient; $config = [ 'token' => 'my_bearer_token' ]; $slack = new ApiClient($config); echo 'Fetching Channels List...' . PHP_EOL; $result = $slack->ListChannels(); if (!$result['ok']) { die('FAIL! Error was: ' . $result['error'] . PHP_EOL); } foreach ($result['channels'] as $channel) { printf('%s: %s' . PHP_EOL, $channel['name'], $channel['purpose']['value']); }
资源迭代器
示例:ListFilesIterator
ListFilesIterator简化了遍历Slack API多页数据的能力。使用迭代器可以避免手动多次调用API来检索结果集的所有页面。
<?php //... $iterator = $slack->getIterator('ListFiles'); $i = 0; foreach ($iterator as $file) { $i++; echo $file['title'] . PHP_EOL; } echo PHP_EOL . 'Retrieved ' . $i . ' files.' . PHP_EOL;
在examples目录中提供了完整的示例。
注意:ListFilesIterator在翻页文件结果时并非必需,但肯定比其他方法更容易。也提供了没有迭代器的示例。
更多API示例
请参阅API示例目录获取更多用法。
免责声明
本库的任何未记录部分应视为至多实验性的。请谨慎操作,并且,如常,欢迎pull请求。
致谢
- Michael Crumm mike@crumm.net
- 所有贡献者
LinkFormatter的正则表达式直接从StevenSloan和他的slack-notifier项目拉取。
贡献
请参阅CONTRIBUTING文件以获取详细信息。
许可证
Phlack在MIT许可证下发布。有关详细信息,请参阅捆绑的LICENSE文件。