mati-core/slack-messenger

适用于nette框架的Slack消息发送器

v1.0.0 2021-04-27 12:05 UTC

This package is auto-updated.

Last update: 2024-09-27 19:39:32 UTC


README

Latest Stable Version Total Downloads Integrity check Latest Unstable Version License

安装

Composer命令

composer require mati-core/slack-messenger

配置

Slack webhook文档: https://api.slack.com/messaging/webhooks

  1. 创建自己的Slack应用

https://api.slack.com/apps?new_app=1

  1. 创建webhook

在Slack应用仪表板上找到左侧菜单中的“Incoming Webhooks”并创建webhook。

  1. 将webhook URL复制到项目的common.neon文件中
parameters:
	slack:
		hook: YOUR_WEB_HOOK_HERE

Webhook链接示例

https://hooks.slack.com/services/abcd1235456/abcd1235456/abcd1235456abcd1235456

发送消息

将SlackMessenger服务添加到您的presenter中

use \MatiCore\SlackMessenger\SlackMessengerTrait

final class *Presenter extends BasePresenter
{
    use SlackMessengerTrait; 
}

所有对象均来自命名空间

\MatiCore\SlackMessenger

简单格式化消息示例

try{
    //Main message object with simple text (primary show on notification banner)
    $message = new Message('Hello World!');
    
    //You can set message title
    $message->setTitle(':star: Hello World!'); 
    
    // Add markdown section with formated text
    $message->addSection(new MarkdownSection(
        'This is my *FIRST* message from website!'
    ));
    
    // Add divider
    $message->addSection(new Divider());
    
    // Add link
    $message->addSection(new MarkdownSection('Odesláno z kontaktního formuláře na app-universe.cz | '. date('d.m.Y H:i:s')));
    
    // send message
    $this->slackMessenger->sendMessage($message);
}catch (\MatiCore\SlackMessenger\SlackMessengerException $e){
    // Send message error
}

部分列表

您可以通过实现MessageSection接口添加自定义部分。

Slack消息构建器:

https://app.slack.com/block-kit-builder/