Bundle to publish to mattermost webhooks

0.5.0 2022-09-03 20:58 UTC

This package is auto-updated.

Last update: 2023-05-31 00:20:31 UTC



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 :

  webhook_url: '%env(MATTERMOST_WEBHOOK_URL)%'
  username: 'My general username'
  channel: 'general-channel'
  icon_url: ''

and adding the webhook URL to your environment variables :

###> codebuds/mattermost-publication###
###< 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.


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 {
            "# 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 {
        "# New contact form submission : " .
        "\n " .
        "**Name : **" . $message->getName() .
        "\n " .
        "**Email : **" . $message->getEmail() .
        "\n" .
        "### Message" .
        "\n" .
} 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/ :

# New contact form submission

**Name : ** {{ }}

**Email : ** {{ }}

### Message

{{ message.message }}

You can render it and use it like the following :

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

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/', ['myVariable' => $variable]))

        } 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 !")