codebuds/mattermost-publication-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

codebuds/mattermost-publication-bundle

Composer 安装命令:

composer require codebuds/mattermost-publication-bundle

包简介

Bundle to publish to mattermost webhooks

README 文档

README

Configuration

This bundle allows you to easily publish text to a Mattermost webhook.

In order to do so you need to add configuration to your symfony by, for example, adding a mattermost_publication.yaml file to the config/packages containing :

mattermost_publication:
  webhook_url: '%env(MATTERMOST_WEBHOOK_URL)%'
  username: 'My general username'
  channel: 'general-channel'
  icon_url: 'https://mysite.com/build/static/my_logo.webp'

and adding the webhook URL to your environment variables :

###> codebuds/mattermost-publication###
MATTERMOST_WEBHOOK_URL="http://{your-mattermost-site}/hooks/xxx-generatedkey-xxx"
###< codebuds/mattermost-publication###

This will set the general configuration for all publications. If these are not set you have to make sure to at least add a webhook URL to the message element you want to publish.

Usage

Basic text publication

You can use the publisher directly inside a controller function by adding it as a parameter (if you have a general webhook_url configured):

/**
 * @Route("/submit-donation")
 * @param MattermostPublication $publication
 * @return JsonResponse
 */
public function submitDonationForm(MattermostPublication $publication)
{
    try {
        $publication->publish(
            "# donation made"
        );
    } catch (\Exception $e) {
        return new JsonResponse(['message' => $e->getMessage()], 500);
    }
}

In an eventSubscriber you can inject the publisher in the constructor :

public function __construct(MattermostPublication $publication)
{
    $this->publication = $publication;
}

and then, inside any function, you can publish a message thanks to the bundle :

try {
    $this->publication->publish(
        "# New contact form submission : " .
        "\n " .
        "**Name : **" . $message->getName() .
        "\n " .
        "**Email : **" . $message->getEmail() .
        "\n" .
        "### Message" .
        "\n" .
        $message->getMessage()
    );
} catch (\Exception $e) {
    return new JsonResponse(['message' => $e->getMessage()], 500);
}

You can also use twig to create message templates, if you create the following template file in templates/mattermost/message.md.twig :

# New contact form submission

**Name : ** {{ message.name }}

**Email : ** {{ message.email }}

### Message

{{ message.message }}

You can render it and use it like the following :

$messageText = $this->twig->render('mattermost/message.md.twig', ['message' => $message]);
$this->publication->publish($messageText);

Configured publication

As it was possible to publish simple text it is also possible to configure the message to all available Mattermost incoming webhook features.

In order to do so instead of just publishing text a CodeBuds\MattermostPublicationBundle\Model\Message must be created.

use CodeBuds\MattermostPublicationBundle\Model\Message as MMMessage;
use CodeBuds\MattermostPublicationBundle\MattermostPublication;
use Twig\Environment;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class MyController extends AbstractController
{
    public function submitDonationForm(MattermostPublication $publication, Environment $twig)
    {
        $variable = "I am a variable";
        try {
            $mmMessage = (new MMMessage())
                ->setText($twig->render('mattermost/mymessage.md.twig', ['myVariable' => $variable]))
                ->setUsername('MyUsername')
                ->setChannel('MyChannel')
                ->setIconUrl('https://mysite.com/build/static/my_logo.webp')
                ->setWebhookUrl('http://otherwebhookurl');

            $publication->publish($mmMessage);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }
}

Doing it this way will override the general settings from the configuration file. If something has been set in the configuration file and the setter is not used the general value will be in the message.

If you want to send a direct message and the incoming webhook is not locked to a channel in mattermost you can set the channel to the username preceded by @ :

$publication->publish((new Message())
    ->setText("Hello John !")
    ->setChannel("@john"));

统计信息

  • 总下载量: 737
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固