PHP code example of codebuds / mattermost-publication-bundle

1. Go to this page and download the library: Download codebuds/mattermost-publication-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codebuds / mattermost-publication-bundle example snippets


/**
 * @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);
    }
}

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

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);
}

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

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

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