simonbackx/slack-php-webhook
Composer 安装命令:
composer require simonbackx/slack-php-webhook
包简介
Post messages to your Slack channels with this easy to use library.
README 文档
README
Easy to use PHP library to post messages in Slack using incoming webhook integrations.
Setup
Log in at slack.com with your team. Go to the page with all your integrations. Add a new incoming webhook.
Select a default channel to post your messages.
Confirm "Add Incoming WebHook integration" Next, you will find your WebHook URL which you need to use this library. Save it somewhere secure.
When you scroll all the way down, you get more options to change your default username, description and icon. You can overwrite these in your code.
Usage
Installation
Composer
Add Slack-PHP-Webhook to your composer.json file or run composer require simonbackx/slack-php-webhook
{
"require": {
"simonbackx/slack-php-webhook": "~1.0"
}
}
Alternative
Download slack.php and require/include it in your PHP file.
Simple message
// Use the url you got earlier $slack = new Slack('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'); // Create a new message $message = new SlackMessage($slack); $message->setText("Hello world!"); // Send it! if ($message->send()) { echo "Hurray 😄"; } else { echo "Failed 😢"; }
Send to a channel
// Use the url you got earlier $slack = new Slack('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'); // Create a new message $message = new SlackMessage($slack); $message->setText("Hello world!")->setChannel("#general"); // Send it! $message->send();
Send to a user
// Use the url you got earlier $slack = new Slack('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'); // Create a new message $message = new SlackMessage($slack); $message->setText("Hello world!")->setChannel("@simonbackx"); // Send it! $message->send();
Overwriting defaults
You can overwrite the defaults on two levels: in a Slack instance (defaults for all messages using this Slack instance) or SlackMessage instances (only for the current message). These methods will not modify your root defaults at Slack.com, but will overwrite them temporary in your code.
$slack = new Slack('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'); $slack->setDefaultUsername("SlackPHP robot"); $slack->setDefaultChannel("#general"); // Unfurl links: automatically fetch and create attachments for detected URLs $slack->setDefaultUnfurlLinks(true); // Set the default icon for messages to a custom image $slack->setDefaultIcon("http://www.domain.com/robot.png"); // Use a 👻 emoji as default icon for messages if it is not overwritten in messages $slack->setDefaultEmoji(":ghost:"); // Create a new message $message = new SlackMessage($slack); $message->setText("Hello world!"); $message->setChannel("#general"); // Unfurl links: automatically fetch and create attachments for detected URLs $message->setUnfurlLinks(false); // Set the icon for the message to a custom image $message->setIcon("http://www.domain.com/robot2.png"); // Overwrite the default Emoji (if any) with 😊 $message->setEmoji(":simple_smile:"); // Send it! $message->send();
Attachments
Create an attachment
Check out https://api.slack.com/docs/attachments for more details
// Use the url you got earlier $slack = new Slack('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'); $slack->setDefaultUsername('Fly company'); // Create a new message $message = new SlackMessage($slack); $attachment = new SlackAttachment("Required plain-text summary of the attachment."); $attachment->setColor("#36a64f"); $attachment->setText("*Optional text* that appears within the attachment"); $attachment->setPretext("Optional text that appears above the attachment block"); $attachment->setAuthor( "Author name", "http://flickr.com/bobby/", //Optional author link "http://flickr.com/bobby/picture.jpg" // Optional author icon ); $attachment->setTitle("Title", "Optional link e.g. http://www.google.com/"); $attachment->setImage("http://www.domain.com/picture.jpg"); /* Slack messages may be formatted using a simple markup language similar to Markdown. Supported formatting includes: ```pre```, `code`, _italic_, *bold*, and even ~strike~.; full details are available on the Slack help site. By default bot message text will be formatted, but attachments are not. To enable formatting on attachment fields, you can use enableMarkdownFor */ $attachment->enableMarkdownFor("text"); $attachment->enableMarkdownFor("pretext"); $attachment->enableMarkdownFor("fields"); // Add fields, last parameter stand for short (smaller field) and is optional $attachment->addField("Title", "Value"); $attachment->addField("Title2", "Value2", true); $attachment->addField("Title", "Value", false); // Add a footer $attachment->setFooterText('By Simon'); $attachment->setFooterIcon('https://www.simonbackx.com/favicon.png'); $attachment->setTimestamp(time()); // Add it to your message $message->addAttachment($attachment); // Send $message->send();
Add buttons
// Use the url you got earlier $slack = new Slack('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'); $slack->setDefaultUsername('Fly company'); // Create a new message $message = new SlackMessage($slack); $message->setText("<@W1A2BC3DD> approved your travel request. Book any airline you like by continuing below."); // Create a new Attachment with fallback text, a plain-text summary of the attachment. // This text will be used in clients that don't show formatted text (eg. IRC, mobile // notifications) and should not contain any markup. $attachment = new \SlackAttachment('Book your flights at https://flights.example.com/book/r123456'); $attachment->addButton('Book flights 🛫', 'https://flights.example.com/book/r123456'); $attachment->addButton('Unsubscribe', 'https://flights.example.com/unsubscribe', 'danger'); $message->addAttachment($attachment); $message->send();
Add (multiple) attachments
$message = new SlackMessage($slack); $message->addAttachment($attachment1); $message->addAttachment($attachment2); $message->send();
Short syntax
All methods support a short syntax. E.g.:
(new SlackMessage($slack)) ->addAttachment($attachment1) ->addAttachment($attachment2) ->send();
Warning
Each message initiates a new HTTPS request, which takes some time. Don't send too much messages at once if you are not running your script in a background task.
simonbackx/slack-php-webhook 适用场景与选型建议
simonbackx/slack-php-webhook 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 863.33k 次下载、GitHub Stars 达 42, 最近一次更新时间为 2018 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「logging」 「message」 「webhook」 「slack」 「channels」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 simonbackx/slack-php-webhook 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 simonbackx/slack-php-webhook 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 simonbackx/slack-php-webhook 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP AMQP Binding Library
A Zend Framework module that sets up Monolog for logging in applications.
Asynchronous Sentry for Symfony - Fire and forget
Inbox pattern process implementation for your Laravel Applications
Stackdriver handler for Monolog (codeinternetapplications/monolog-stackdriver Fork).
High-performance, zero-dependency PSR-3 logger for MonkeysLegion with structured logging, handlers, processors, and PHP 8.4 features.
统计信息
- 总下载量: 863.33k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 43
- 点击次数: 30
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-03-15