timkippdev / slack-notifier
用于与Slack的Incoming Webhooks集成的PHP客户端
1.0.1
2019-07-28 06:26 UTC
Requires
- php: ^7.1.3
- guzzlehttp/guzzle: ~6.0
Requires (Dev)
- mockery/mockery: ^1.2
- phpunit/phpunit: ^7
This package is auto-updated.
Last update: 2024-09-28 18:58:33 UTC
README
Slack Notifier利用Slack的Incoming Webhooks功能,通过您的个人Slack应用Webhook URL直接将消息发布到您选择的Slack频道。
安装
以下说明假设您已在您的机器或正在工作的服务器上安装了Composer。
通过Composer安装
composer require timkippdev/slack-notifier
用法
创建Slack应用并启用Incoming Webhooks
如有需要,请参阅以下Slack提供的说明以创建您的应用并获取您的Webhook URL
https://api.slack.com/incoming-webhooks#getting-started
设置SlackNotifier实例
一旦您拥有了Webhook URL,您就可以创建一个SlackNotifier类的实例。
// your Webhook URL $webhookAPI = 'https://hooks.slack.com/services/Txxxxxx/Byyyyyyy/Zzzzzzzzz'; // new SlackNotifier instance $slackNotifier = new \TimKippDev\SlackNotifier\SlackNotifier($webhookAPI);
您还可以选择调用静态的
sendMessageToChannel方法,而不是创建自己的实例。
发送消息
现在您已经创建了SlackNotifier实例,您可以使用它向从您的Webhook URL配置的频道发送消息,并可选地添加附件。
使用实例方法发送简单消息
$slackNotifier->sendMessage('First message using Slack Notifier!');
使用静态方法发送简单消息
我们提供了一个静态方法来调用,以便轻松地将消息发送到不同的频道,这样您就不必局限于单个实例。
\TimKippDev\SlackNotifier\SlackNotifier::sendMessageToChannel($webhookAPI, 'First message using Slack Notifier!');
带附件的消息
有关完整的Slack消息附件文档,请参阅以下链接:https://api.slack.com/docs/message-attachments
$slackAttachment = new \TimKippDev\SlackNotifier\SlackAttachment(); $slackAttachment ->setAuthorName('Author name goes here') ->setAuthorLinkUrl('https://example.com/author') ->setAuthorIconUrl('https://img.icons8.com/ultraviolet/48/000000/soy.png') ->setColor('#222222') ->setFallbackText('Fallback goes here') ->setFooterIconUrl('https://img.icons8.com/color/48/000000/soy.png') ->setFooterText('Footer text goes here') ->setImageUrl('https://img.icons8.com/color/400/000000/soy.png') ->setPretext('Pretext goes here') ->setText('Attached using Slack Notifier!') ->setThumbnailUrl('https://img.icons8.com/ultraviolet/96/000000/soy.png') ->setTitle('Title goes here') ->setTitleLinkUrl('https://example.com/title'); // with instance method $slackNotifier->sendMessage('First message with attachments using Slack Notifier!', [ $slackAttachment ]); // with static method \TimKippDev\SlackNotifier\SlackNotifier::sendMessageToChannel($webhookAPI, 'First message using Slack Notifier!', [ $slackAttachment ]);
附件操作
$slackActionConfirmation = new \TimKippDev\SlackNotifier\SlackActionConfirmation(); $slackActionConfirmation->setDismissButtonText('Dismiss') ->setOkButtonText('Confirm') ->setText('Confirmation Text') ->setTitle('Confirmation Title'); $slackAction = new \TimKippDev\SlackNotifier\SlackAction(); $slackAction->setStyle('primary') ->setType('button') ->setText('Click Me with Confirmation') ->setUrl('https://example.com/action') ->setName('action-name') ->setConfirmation($slackActionConfirmation); $slackAttachment = new \TimKippDev\SlackNotifier\SlackAttachment(); $slackAttachment ->setText('Attached using Slack Notifier!') ->setActions([$slackAction]); // with instance method $slackNotifier->sendMessage('First message attachment containing actions using Slack Notifier!', [ $slackAttachment ]); // with static method \TimKippDev\SlackNotifier\SlackNotifier::sendMessageToChannel($webhookAPI, 'First message using Slack Notifier!', [ $slackAttachment ]);
附件字段
$slackField = new \TimKippDev\SlackNotifier\SlackField(); $slackField->setTitle('Field Title') ->setValue('Field Value'); $slackAttachment = new \TimKippDev\SlackNotifier\SlackAttachment(); $slackAttachment ->setText('Attached using Slack Notifier!') ->setFields([$slackField]); // with instance method $slackNotifier->sendMessage('First message attachment containing fields using Slack Notifier!', [ $slackAttachment ]); // with static method \TimKippDev\SlackNotifier\SlackNotifier::sendMessageToChannel($webhookAPI, 'First message using Slack Notifier!', [ $slackAttachment ]);