massimo-filippi/slack-module

Slack通信的ZF3模块

v1.0 2018-03-21 15:47 UTC

This package is not auto-updated.

Last update: 2024-09-15 04:20:53 UTC


README

Slack通信的ZF3模块

Packagist License

介绍

更多信息将很快提供...

安装

1. 通过Composer安装

通过Composer安装最新稳定版本

composer require massimo-filippi/slack-module

通过Composer安装最新开发版本

composer require massimo-filippi/slack-module:dev-master

2. 在您的应用程序中启用模块

Composer应该在安装过程中自动在您的项目中启用MassimoFilippi\SlackModule

如果它没有启用,您可以通过在文件config/modules.config.php中添加值'MassimoFilippi\SlackModule'来手动启用模块。最后,它应该看起来像下面的PHP数组。

<?php

return [
    'Zend\Router',
    'Zend\Validator',
    'MassimoFilippi\SlackModule', // Add this line, ideally before Application module.
    'Application',
];

3. 设置您的配置

您必须为SlackService设置设置,否则您将无法使用它。

以下是我的config/autoload/local.php文件中的内容。

<?php

return [
    'massimo_filippi' => [
        'slack_module' => [
            'config' => [
                'webhook_url' => 'https://hooks.slack.com/services/#########/#########/########################',
                // Whether names like @regan should be converted into links by Slack, default: false
                'link_names' => false,
                // Whether Slack should unfurl links to text-based content, default: false
                'unfurl_links' => false,
                // Whether Slack should unfurl links to media content such as images and YouTube videos, default: true 
                'unfurl_media' => true,
                // Whether message text should be interpreted in Slack's Markdown-like language. For formatting options, see Slack's help article: http://goo.gl/r4fsdO, default: true
                'allow_markdown' => true,
                // Which attachment fields should be interpreted in Slack's Markdown-like language. By default, Slack assumes that no fields in an attachment should be formatted as Markdown. // default: []
                'markdown_in_attachments' => [],
                
                // Allow Markdown in just the text and title fields
                //// 'markdown_in_attachments' => ['text', 'title']
                // Allow Markdown in all fields
                //// 'markdown_in_attachments' => ['pretext', 'text', 'title', 'fields', 'fallback']
                
                'defaults' => [
                    // default username, set to null to use the default set on the Slack webhook, default: null
                    'username' => 'Slack module',
                    // default channel, channel: #general, user: @john.doe, set to null to use the default set on the Slack webhook, default: null
                    'channel' => '#general',
                    // URL to an image or Slack emoji like :ghost: or :+1:, set null to use the default set on the Slack webhook, default: null 
                    'icon' => null 
                ],
            ],
        ],
    ],
];

用法

在业务逻辑类中的某个地方。

<?php 

use Maknz\Slack\Message as SlackMessage;
use MassimoFilippi\SlackModule\Model\Attachment as SlackAttachment;

/** @var SlackMessage $slackMessage */
$slackMessage = $this->slackService->createMessage();
$slackMessage->to('#general')
    ->from('John Doe')
    ->withIcon(':ghost:')
    ->setText('This is an amazing message!');

/** @var SlackAttachment $slackAttachment */
$slackAttachment = $this->slackService->createAttachment([
        'fallback' => 'Some fallback text',
        'text' => 'The attachment text'
    ]);
$slackMessage->attach($slackAttachment);

try {
    // Injected MassimoFilippi\SlackModule\Service\SlackService.
    $this->slackService->sendMessage($slackMessage);
} catch (\Exception $exception) {
    var_dump($exception->getMessage());
}

方法

更多资源