simonbackx/slack-php-webhook

使用这个易于使用的库向您的Slack频道发送消息。

1.0.3 2018-06-12 09:18 UTC

This package is not auto-updated.

Last update: 2024-09-13 12:51:42 UTC


README

Latest Stable Version License

易于使用的PHP库,用于通过传入Webhook集成在Slack中发送消息。

设置

在slack.com上用您的团队登录。转到所有集成所在的页面。添加一个新的传入Webhook。

选择一个默认频道来发送您的消息。Setup1

确认“添加传入Webhook集成”下一步,您将找到您需要使用此库的Webhook URL。请将其保存在安全的地方。

Setup2

当您滚动到页面底部时,您将获得更多选项来更改默认用户名、描述和图标。您可以在代码中覆盖这些内容。

用法

安装

Composer

将Slack-PHP-Webhook添加到您的composer.json文件,或者运行composer require simonbackx/slack-php-webhook

{
  "require": {
    "simonbackx/slack-php-webhook": "~1.0"
  }
}

替代方案

下载slack.php并将其包含在您的PHP文件中。

简单消息

// 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 😢";
}

发送到频道

// 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();

发送到用户

// 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();

覆盖默认值

您可以在两个级别上覆盖默认值:在Slack实例中(此Slack实例使用的所有消息的默认值)或SlackMessage实例中(仅针对当前消息)。这些方法不会修改Slack.com的根默认值,但在您的代码中临时覆盖它们。

$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();

附件

创建一个附件

有关更多信息,请参阅https://api.slack.com/docs/attachments

// 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();

查看结果

添加按钮

// 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();

查看结果

添加(多个)附件

$message = new SlackMessage($slack);
$message->addAttachment($attachment1);
$message->addAttachment($attachment2);
$message->send();

简短语法

所有方法都支持简短语法。例如。

(new SlackMessage($slack))
    ->addAttachment($attachment1)
    ->addAttachment($attachment2)
    ->send();

警告

每条消息都会启动一个新的HTTPS请求,这需要一些时间。如果您不是在后台任务中运行脚本,请不要一次发送太多消息。