markenwerk / slack-client
此包已被弃用且不再维护。未建议替代包。
一个基本的Slack客户端库,提供使用webhook API向Slack频道简单发布的功能。
2.0.1
2016-07-15 15:32 UTC
Requires
- php: >=5.3
- markenwerk/basic-http-client: ~3.0
- markenwerk/common-exceptions: ~3.0
Requires (Dev)
- codeclimate/php-test-reporter: dev-master
- phpunit/phpunit: ~4.8
This package is not auto-updated.
Last update: 2021-01-18 11:55:24 UTC
README
一个基本的Slack客户端库,提供使用webhook API向Slack频道简单发布的功能。
安装
{
"require": {
"markenwerk/slack-client": "~2.0"
}
}
使用方法
自动加载和命名空间
require_once('path/to/vendor/autoload.php');
向频道或成员发布
以下示例将向如下所示的Slack频道或成员发布
设置API客户端
use Markenwerk\SlackClient\SlackClient;
$client = new SlackClient();
$client
->setSubdomainName('markenwerk')
->setToken('<YOUR_API_TOKEN>')
->setUsername('PHP SlackClient');
设置带附件的Slack消息
use Markenwerk\SlackClient\SlackAttachment;
use Markenwerk\SlackClient\SlackAttachmentField;
use Markenwerk\SlackClient\SlackMessage;
$message = new SlackMessage();
$message
->setText('A basic Slack client library providing simple posting to Slack channels using the webhook API.')
->setIconUrl('https://avatars2.githubusercontent.com/u/5921253?v=3&s=200')
->setUnfurlLinks(true)
->setUnfurlMedia(true);
$attachment = new SlackAttachment();
$attachment
->setText('A basic Slack client library providing simple posting to Slack channels using the webhook API.')
->setPretext('A basic Slack client library.')
->setFallback('A basic Slack client library providing simple posting to Slack channels using the webhook API.')
->setColor(SlackAttachment::COLOR_WARNING);
$shortAttachmentField = new SlackAttachmentField();
$shortAttachmentField
->setTitle('Short field')
->setValue('Some chars')
->setShort(true);
$anotherShortAttachmentField = new SlackAttachmentField();
$anotherShortAttachmentField
->setTitle('Short field')
->setValue('Some chars')
->setShort(true);
$attachmentField = new SlackAttachmentField();
$attachmentField
->setTitle('Regular field')
->setValue('Some more chars')
->setShort(false);
$anotherAttachmentField = new SlackAttachmentField();
$anotherAttachmentField
->setTitle('Regular field')
->setValue('Some more chars')
->setShort(false);
$attachment
->addField($shortAttachmentField)
->addField($anotherShortAttachmentField)
->addField($attachmentField)
->addField($anotherAttachmentField);
$message->addAttachment($attachment);
向频道发布
$client->postToChannel('#channel', $message);
向成员发布
$client->postToMember('@member', $message);
异常处理
PHP基本HTTP客户端提供了不同的异常——也由PHP Common Exceptions项目提供——以进行适当的处理。
您可以在GitHub上的PHP Common Exceptions找到更多关于信息。
预期异常
通常,您应该预期任何setter方法都可能抛出\InvalidArgumentException。在使用PHP Slack Client时,可能会抛出以下异常。
- 在向Slack发布时抛出
Markenwerk\CommonException\ParserException\StringifyException - 在向Slack发布时抛出
Markenwerk\CommonException\NetworkException\UnexpectedResponseException - 在向Slack发布时抛出
Markenwerk\CommonException\NetworkException\ConnectionTimeoutException - 在向Slack发布时抛出
Markenwerk\CommonException\NetworkException\CurlException
贡献
对我们项目的贡献总是非常受欢迎。
但:请遵循CONTRIBUTING.md文档中编写的贡献指南。
许可
PHP Slack Client在MIT许可下。
